2017-08-08 12 views
4

私はC#関数をPHP関数に変換したいと思います。 https://stackoverflow.com/a/19441805/3581428C#関数をPHP関数に暗号化と復号化を変換します。

public static string Encrypt(string clearText) 
{ 
    string EncryptionKey = "MAKV2SPBNI99212"; 
    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); 
    using (Aes encryptor = Aes.Create()) 
    { 
     Rfc2898DeriveBytes pdb = new 
      Rfc2898DeriveBytes(EncryptionKey, new byte[] 
      { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); 
     encryptor.Key = pdb.GetBytes(32); 
     encryptor.IV = pdb.GetBytes(16); 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) 
      { 
       cs.Write(clearBytes, 0, clearBytes.Length); 
       cs.Close(); 
      } 
      clearText = Convert.ToBase64String(ms.ToArray()); 
     } 
    } 
    return clearText; 
} 

public static string Decrypt(string cipherText) 
{ 
    string EncryptionKey = "MAKV2SPBNI99212"; 
    byte[] cipherBytes = Convert.FromBase64String(cipherText); 
    using (Aes encryptor = Aes.Create()) 
    { 
     Rfc2898DeriveBytes pdb = new 
      Rfc2898DeriveBytes(EncryptionKey, new byte[] 
      { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); 
     encryptor.Key = pdb.GetBytes(32); 
     encryptor.IV = pdb.GetBytes(16); 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) 
      { 
       cs.Write(cipherBytes, 0, cipherBytes.Length); 
       cs.Close(); 
      } 
      cipherText = Encoding.Unicode.GetString(ms.ToArray()); 
     } 
    } 
    return cipherText; 
} 

私は下の機能で試してみましたが、それは別の暗号化された文字列を作成:

thkTvpUmSWV9lKAOfWNIIu9n7jHcku7C6WDD/hsvll+xjqOWdk3fyI+eRhBbvJlX 

:C#メソッドを使用して

function encrypt_decrypt($action, $string) { 
    $output = false; 

$encrypt_method = "AES-256-CBC"; 
$secret_key = '3sc3RLrpd17'; 

$secret_iv=chr(0x49).chr(0x76).chr(0x61).chr(0x6e).chr(0x20).chr(0x4d).chr(0x65).chr(0x64).chr(0x76).chr(0x65).chr(0x64).chr(0x65).chr(0x76); 


$key = substr(hash('sha1', $secret_key), 0, 32);; 
//echo "key: ".$key."<br>"; 

$iv = substr(hash('sha1', $secret_iv), 0, 16); 
//echo "iv key: ".$iv."<br>"; 
if($action == 'encrypt') { 
    $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv); 
    $output = base64_encode($output); 
} 
else if($action == 'decrypt'){ 
    $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv); 
} 

return $output; 
} 

$plain_txt = "1122334411223344"; 
echo "Plain Text = $plain_txt\n"."<br/>"; 

$encrypted_txt = encrypt_decrypt('encrypt', $plain_txt); 
echo "Encrypted Text = $encrypted_txt\n"."<br/>"; 

$decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt); 
echo "Decrypted Text = $decrypted_txt\n"."<br/>"; 

if($plain_txt === $decrypted_txt) echo "SUCCESS"."<br/>"; 
else echo "FAILED"."<br/>"; 

echo "\n"."<br/>"; 
exit; 

暗号化された文字列をここで は、C#で機能するためのリンクです長さは64

暗号化された文字列Usi NG PHP方法:

OTlIYXJDcTl0SVpKRlhaV0l3dFk1ZjFYM3FPcHB3ckdTRERITGhHVEVoTT0= 

長さは、私はC#の機能と同じ暗号化された文字列を生成することができますどのように60

のですか? PHPのC#関数をどのように複製できますか?

この例では、リンク使用して64の暗号化されたテキストの長さを作成することができる:AES ENCRYPTION 64 length encrypted string

+0

私は思います'Rfc2898DeriveBytes'はSHA-1を使用し、SHA-256は使用しません。 – DanielO

+1

AES-256-CBCの暗号IV長は、13でなく13でなければなりません。 – DanielO

+0

@DanielO。 $ iv = substr(ハッシュ( 'sha1'、$ secret_iv)、0,16); IVの長さは16であり、sha1を使用するコードは更新されましたが、暗号化されたテキストは同じ – user3581428

答えて

2

をこの(ベータ版)を試してみてください:

PHP

<?php 

$plaintext = 'My secret message 1234'; 
$password = '3sc3RLrpd17'; 
$method = 'aes-256-cbc'; 

// Must be exact 32 chars (256 bit) 
$password = substr(hash('sha256', $password, true), 0, 32); 
echo "Password:" . $password . "\n"; 

// IV must be exact 16 chars (128 bit) 
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0); 

// av3DYGLkwBsErphcyYp+imUW4QKs19hUnFyyYcXwURU= 
$encrypted = base64_encode(openssl_encrypt($plaintext, $method, $password, OPENSSL_RAW_DATA, $iv)); 

// My secret message 1234 
$decrypted = openssl_decrypt(base64_decode($encrypted), $method, $password, OPENSSL_RAW_DATA, $iv); 

echo 'plaintext=' . $plaintext . "\n"; 
echo 'cipher=' . $method . "\n"; 
echo 'encrypted to: ' . $encrypted . "\n"; 
echo 'decrypted to: ' . $decrypted . "\n\n"; 

exit; 

C#

public string EncryptString(string plainText, byte[] key, byte[] iv) 
    { 
     // Instantiate a new Aes object to perform string symmetric encryption 
     Aes encryptor = Aes.Create(); 

     encryptor.Mode = CipherMode.CBC; 
     //encryptor.KeySize = 256; 
     //encryptor.BlockSize = 128; 
     //encryptor.Padding = PaddingMode.Zeros; 

     // Set key and IV 
     encryptor.Key = key; 
     encryptor.IV = iv; 

     // Instantiate a new MemoryStream object to contain the encrypted bytes 
     MemoryStream memoryStream = new MemoryStream(); 

     // Instantiate a new encryptor from our Aes object 
     ICryptoTransform aesEncryptor = encryptor.CreateEncryptor(); 

     // Instantiate a new CryptoStream object to process the data and write it to the 
     // memory stream 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write); 

     // Convert the plainText string into a byte array 
     byte[] plainBytes = Encoding.ASCII.GetBytes(plainText); 

     // Encrypt the input plaintext string 
     cryptoStream.Write(plainBytes, 0, plainBytes.Length); 

     // Complete the encryption process 
     cryptoStream.FlushFinalBlock(); 

     // Convert the encrypted data from a MemoryStream to a byte array 
     byte[] cipherBytes = memoryStream.ToArray(); 

     // Close both the MemoryStream and the CryptoStream 
     memoryStream.Close(); 
     cryptoStream.Close(); 

     // Convert the encrypted byte array to a base64 encoded string 
     string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length); 

     // Return the encrypted data as a string 
     return cipherText; 
    } 

    public string DecryptString(string cipherText, byte[] key, byte[] iv) 
    { 
     // Instantiate a new Aes object to perform string symmetric encryption 
     Aes encryptor = Aes.Create(); 

     encryptor.Mode = CipherMode.CBC; 
     //encryptor.KeySize = 256; 
     //encryptor.BlockSize = 128; 
     //encryptor.Padding = PaddingMode.Zeros; 

     // Set key and IV 
     encryptor.Key = key; 
     encryptor.IV = iv; 

     // Instantiate a new MemoryStream object to contain the encrypted bytes 
     MemoryStream memoryStream = new MemoryStream(); 

     // Instantiate a new encryptor from our Aes object 
     ICryptoTransform aesDecryptor = encryptor.CreateDecryptor(); 

     // Instantiate a new CryptoStream object to process the data and write it to the 
     // memory stream 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write); 

     // Will contain decrypted plaintext 
     string plainText = String.Empty; 

     try 
     { 
      // Convert the ciphertext string into a byte array 
      byte[] cipherBytes = Convert.FromBase64String(cipherText); 

      // Decrypt the input ciphertext string 
      cryptoStream.Write(cipherBytes, 0, cipherBytes.Length); 

      // Complete the decryption process 
      cryptoStream.FlushFinalBlock(); 

      // Convert the decrypted data from a MemoryStream to a byte array 
      byte[] plainBytes = memoryStream.ToArray(); 

      // Convert the decrypted byte array to string 
      plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length); 
     } 
     finally 
     { 
      // Close both the MemoryStream and the CryptoStream 
      memoryStream.Close(); 
      cryptoStream.Close(); 
     } 

     // Return the decrypted data as a string 
     return plainText; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     string message = "My secret message 1234"; 
     string password = "3sc3RLrpd17"; 

     // Create sha256 hash 
     SHA256 mySHA256 = SHA256Managed.Create(); 
     byte[] key = mySHA256.ComputeHash(Encoding.ASCII.GetBytes(password)); 

     // Create secret IV 
     byte[] iv = new byte[16] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; 

     string encrypted = this.EncryptString(message, key, iv); 
     string decrypted = this.DecryptString(encrypted, key, iv); 

     Console.WriteLine(encrypted); 
     Console.WriteLine(decrypted); 
    } 
+0

Danielありがとうございました...あなたの提供するC#とPHP関数を試してみましょう...私はこれを短時間で更新します。 – user3581428

関連する問題