C# and PHP have different AES encryption results

梦想与她 提交于 2019-12-12 22:28:35

问题


I have same data and encryption key, same algorithm, same mode but different result.
C# Code:


        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();

Result : 13A6DAD3119F29A8C4BF6D5BD11564E4E1A93F85B7F2AD9E8E97756688754DE32A23ADE41DFD9F76186D8EB25E66D0DCF458ECAA026F16463811C48FC814E50B10FF57FDFDB0C0761088D1AC4DDDAE749CC77FD402A2B8E005A43AEEC914E6F9


PHP Code:


    $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;


Result: 99d84f4a728affe97e05b5153cb5d4842d7396cc9b26d807afd08e0f1e904a4e9f43b7d2c35151c6e609230879d120ae180c18bb461b071e79afd98ffec09e29addf9cddeaafaabf6bdef174a7781b538dd7f67e577810c261f5e6e07cb1b5be2416b80d7a59fadbf66f960968614191

I can not understand the difference of these two codes. I think they must have same output, but not. output.


回答1:


You should pack the 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;



回答2:


Use base64_encode($encryptedData) instead of implode.



来源:https://stackoverflow.com/questions/44723974/c-sharp-and-php-have-different-aes-encryption-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!