简体   繁体   English

为什么我的 C# 代码在使用 AES CBC 128 位加密作为本网站时不产生相同的 output:https:///aes-com/pipes

[英]Why doesn't my C# code produce the same output when using AES CBC 128bit encryption as this website: https://cryptii.com/pipes/aes-encryption

This is what I want to achieve using AES 128bit CBC encryption converted to hexadecimal: "30487A117196A34DE5ADCD679BA0FE71".这就是我想要使用转换为十六进制的 AES 128 位 CBC 加密来实现的:“30487A117196A34DE5ADCD679BA0FE71”。 I can achieve this when I use the website: https://cryptii.com/pipes/aes-encryption我在使用网站时可以做到这一点: https://cryptii.com/pipes/aes-encryption

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

I am however not able to achieve this using C#.但是,我无法使用 C# 来实现这一点。 Here is the code I am using:这是我正在使用的代码:

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

Why doesn't the outputted encrypted string in hexadecimal match: "30487A117196A34DE5ADCD679BA0FE71" - it is outputted as: "72aa9bf0ee7d8e3db7e8c763d21371b3".为什么输出的十六进制加密字符串不匹配:“30487A117196A34DE5ADCD679BA0FE71” - 输出为:“72aa9bf0ee7d8e3db7e8c763d21371b3”。

The thing that's most strange here is that both the expected value: "30487A117196A34DE5ADCD679BA0FE71" and the C# generated value: "72aa9bf0ee7d8e3db7e8c763d21371b3" decrypt correctly when fed to the decryption method returning "raiden"..这里最奇怪的是预期值:“30487A117196A34DE5ADCD679BA0FE71”和 C# 生成的值:“72aa9bf0ee7d8e3db7e8c763d21371b3”在输入解密方法返回“raiden”时正确解密。

Any help on this one is greatly appreciated!非常感谢对此的任何帮助!

I used PKCS7 padding for the encryption (was using Zeros)我使用 PKCS7 填充进行加密(使用 Zeros)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM