C#和PHP具有不同的AES加密结果

时间:2017-06-23 14:32:47

标签: c# php encryption aes

我有相同的数据和加密密钥,相同的算法,相同的模式但不同的结果。
C#代码:


        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

我无法理解这两个代码的区别。我认为他们必须有相同的输出,但不是。 输出。

2 个答案:

答案 0 :(得分:1)

使用base64_encode($encryptedData)代替implode

答案 1 :(得分: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;