2017-06-23 191 views
1

我有相同的數據和加密密鑰,算法相同,模式相同但結果不同。
C#代碼:C#和PHP有不同的AES加密結果

 

     string encKey = "0F777D55FDB154E7D8754C3C0E660A65"; 
     string dataToEncrypt = "FF01083131323233333434FF020102FF030E3230313630313230313635353032FF040C313132323333343435353636FF05083131323233333434FF060F6D6173746572706173735F75736572FF070101FF080104800000000000000000000000"; 
     using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) 
     { 
      using (System.Security.Cryptography.AesManaged AES = new System.Security.Cryptography.AesManaged()) 
      { 
       AES.KeySize = 128; 
       AES.BlockSize = 128; 
       AES.Key = StringToByteArray(encKey); 
       AES.IV = StringToByteArray("00000000000000000000000000000000"); 
       AES.Padding = System.Security.Cryptography.PaddingMode.None; 
       AES.Mode = System.Security.Cryptography.CipherMode.CBC; 

       byte[] bytesToBeEncrypted = StringToByteArray(dataToEncrypt); 
       using (var cs = new System.Security.Cryptography.CryptoStream(ms, AES.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)) 
       { 
        cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length); 
        cs.Close(); 
      } 
      encryptedData = ByteArrayToString(ms.ToArray()); 
     } 
    } 
    Console.WriteLine(encryptedData); 
    Console.ReadLine(); 

結果: 13A6DAD3119F29A8C4BF6D5BD11564E4E1A93F85B7F2AD9E8E97756688754DE32A23ADE41DFD9F76186D8EB25E66D0DCF458ECAA026F16463811C48FC814E50B10FF57FDFDB0C0761088D1AC4DDDAE749CC77FD402A2B8E005A43AEEC914E6F9


PHP代碼:

 

    $inputText = "FF01083131323233333434FF020102FF030E3230313630313230313635353032FF040C313132323333343435353636FF05083131323233333434FF060F6D6173746572706173735F75736572FF070101FF080104800000000000000000000000"; 
    $inputKey = "0F777D55FDB154E7D8754C3C0E660A65"; 
    $inputText = pack("H*", $inputText); 
    $inputKey = pack("H*", $inputKey); 
    $iv = "0000000000000000"; 
    $encryptedData = openssl_encrypt($inputText, "aes-128-cbc", $inputKey, OPENSSL_RAW_DATA, $iv); 
    $encryptedData = implode("", unpack("H*", $encryptedData)); 
    print $encryptedData . PHP_EOL; 


結果: 99d84f4a728affe97e05b5153cb5d4842d7396cc9b26d807afd08e0f1e904a4e9f43b7d2c35151c6e609230879d120ae180c18bb461b071e79afd98ffec09e29addf9cddeaafaabf6bdef174a7781b538dd7f67e577810c261f5e6e07cb1b5be2416b80d7a59fadbf66f960968614191

我無法理解這兩個碼的區別。我認爲他們必須有相同的產出,但不是。輸出爲 。

+0

也許你有這樣的問題:http://php.net/manual/en/function.pack.php#refsect1-function.pack-notes – Edwin

回答

1

你應該收拾iv

<?php 
    $inputText = "FF01083131323233333434FF020102FF030E3230313630313230313635353032FF040C313132323333343435353636FF05083131323233333434FF060F6D6173746572706173735F75736572FF070101FF080104800000000000000000000000"; 
    $inputKey = "0F777D55FDB154E7D8754C3C0E660A65"; 
    $inputText = pack("H*", $inputText); 
    $inputKey = pack("H*", $inputKey); 
    $iv = pack("H*", "00000000000000000000000000000000"); 
    $encryptedData = openssl_encrypt($inputText, "aes-128-cbc", $inputKey, OPENSSL_RAW_DATA, $iv); 
    $encryptedData = substr(implode("", unpack("H*", $encryptedData)),0,192); 
    print $encryptedData . PHP_EOL; 
1

使用base64_encode($encryptedData)而不是implode