为什么我的C#代码在使用AES CBC 128位加密时不产生与以下网站相同的输出:https://cryptii.com/pipes/aes-encryption

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我的C#代码在使用AES CBC 128位加密时不产生与以下网站相同的输出:https://cryptii.com/pipes/aes-encryption相关的知识,希望对你有一定的参考价值。

这是我想使用转换为十六进制的AES 128位CBC加密实现的:“ 30487A117196A34DE5ADCD679BA0FE71”。当我使用以下网站时,可以实现此目标:https://cryptii.com/pipes/aes-encryption

https://cryptii.com/pipes/aes-encryption

但是,我无法使用C#实现这一目标。这是我正在使用的代码:

public static void Main(string[] args)
    {
        var key = "123456789012345678901234567890af";
        var text = "raiden";
        var iv = "01C2191CFA1B33D47246E8C76EB3A824";

        var value = EncryptValues(key, iv, text);
        DecryptValues(key, iv, value);

        Console.ReadLine();
    }

    public static string EncryptValues(string keyText, string ivText, string plainText)
    {
        var key = ParseBytes(keyText);
        var text = Encoding.Default.GetBytes(plainText);
        var iv = ParseBytes(ivText);

        var raw = SimpleEncrypt(new RijndaelManaged(), CipherMode.CBC, key, iv, text);

        var hexadecimalCipher = BytesToHex(raw);
        Console.WriteLine(hexadecimalCipher);
        return hexadecimalCipher.Replace(" ", string.Empty);
    }

    public static void DecryptValues(string keyText, string ivText, string cipherText)
    {
        var key = ParseBytes(keyText);
        var text = ParseBytes(cipherText);
        //var expectedText = ParseBytes("30487A117196A34DE5ADCD679BA0FE71"); // <<<<< this is the expected value
        var iv = ParseBytes(ivText);

        var dec = SimpleDecrypt(new RijndaelManaged(), CipherMode.CBC, key, iv, text);
        Console.WriteLine(Encoding.UTF8.GetString(dec));
    }
    public static byte[] ParseBytes(string strToParse, bool removeSeparator = false, string separator = " ")
    {
        // Basic check
        if (string.IsNullOrEmpty(strToParse))
            throw new ArgumentNullException();

        // Check from separator
        if (removeSeparator)
            strToParse = strToParse.Replace(separator, string.Empty);

        // Parse
        var bytes = new List<byte>();
        var counter = 0;
        var characterArray = strToParse.ToCharArray();
        for (int i = 0; i < strToParse.Length / 2; i++)
        {
            string byteString = $"{characterArray[counter]}{characterArray[counter + 1]}";
            var byteToAdd = byte.Parse(byteString, NumberStyles.HexNumber);
            bytes.Add(byteToAdd);
            counter += 2;
        }

        return bytes.ToArray();
    }

    public static byte[] HexToBytes(string str, string separator = " ")
    {
        if (str == null)
        {
            throw new ArgumentNullException();
        }

        if (separator == null)
        {
            separator = string.Empty;
        }

        if (str == string.Empty)
        {
            return new byte[0];
        }

        int stride = 2 + separator.Length;

        if ((str.Length + separator.Length) % stride != 0)
        {
            throw new FormatException();
        }

        var bytes = new byte[(str.Length + separator.Length) / stride];

        for (int i = 0, j = 0; i < str.Length; i += stride)
        {
            bytes[j] = byte.Parse(str.Substring(i, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
            j++;

            // There is no separator at the end!
            if (j != bytes.Length && separator != string.Empty)
            {
                if (string.CompareOrdinal(str, i + 2, separator, 0, separator.Length) != 0)
                {
                    throw new FormatException();
                }
            }
        }

        return bytes;
    }

    public static string BytesToHex(byte[] bytes, string separator = " ")
    {
        if (bytes == null)
        {
            throw new ArgumentNullException();
        }

        if (separator == null)
        {
            separator = string.Empty;
        }

        if (bytes.Length == 0)
        {
            return string.Empty;
        }

        var sb = new StringBuilder((bytes.Length * (2 + separator.Length)) - 1);

        for (int i = 0; i < bytes.Length; i++)
        {
            if (i != 0)
            {
                sb.Append(separator);
            }

            sb.Append(bytes[i].ToString("x2"));
        }

        return sb.ToString();
    }

    public static byte[] SimpleEncrypt(SymmetricAlgorithm algorithm, CipherMode cipherMode, byte[] key, byte[] iv, byte[] bytes)
    {
        algorithm.Mode = cipherMode;
        algorithm.Padding = PaddingMode.Zeros;
        algorithm.Key = key;
        algorithm.IV = iv;

        using (var encryptor = algorithm.CreateEncryptor())
        {
            return encryptor.TransformFinalBlock(bytes, 0, bytes.Length);
        }
    }

    public static byte[] SimpleDecrypt(SymmetricAlgorithm algorithm, CipherMode cipherMode, byte[] key, byte[] iv, byte[] bytes)
    {
        algorithm.Mode = cipherMode;
        algorithm.Padding = PaddingMode.Zeros;
        algorithm.Key = key;
        algorithm.IV = iv;

        using (var encryptor = algorithm.CreateDecryptor())
        {
            return encryptor.TransformFinalBlock(bytes, 0, bytes.Length);
        }
    }

为什么输出的加密字符串不以十六进制匹配:“ 30487A117196A34DE5ADCD679BA0FE71”-输出为:“ 72aa9bf0ee7d8e3db7e8c763d21371b3”。

这里最奇怪的是,预期值:“ 30487A117196A34DE5ADCD679BA0FE71”和C#生成的值:“ 72aa9bf0ee7d8e3db7e8c763d21371b3”在送入返回“ raiden”的解密方法时正确解密。.

对此上的任何帮助,我们将不胜感激!

答案

我使用PKCS7填充进行加密(使用零)

以上是关于为什么我的C#代码在使用AES CBC 128位加密时不产生与以下网站相同的输出:https://cryptii.com/pipes/aes-encryption的主要内容,如果未能解决你的问题,请参考以下文章

使用 OpenSSL/C++ 和 PHP/Mcrypt 的 AES-128-CBC 加密:仅解密第一个块

AES-128-CBC加密过程中,我想随机产生16位的向量,希望各位能给我一下C语言代码的实现。

nodejs中aes-128-cbc加密和解密

无法使用来自 AES-256-CBC 的 pgcrypto 解密,但 AES-128-CBC 可以

php openssl aes-256-cbc key长度自动匹配了128的长度,为啥

javascript 与 PHP 通信加密,使用AES 128 CBC no padding,以及ios,java,c#文章例子