Search code examples
c#encryption.net-coreaes

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". I can achieve this when I use the website: https://cryptii.com/pipes/aes-encryption

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

I am however not able to achieve this using 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".

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"..

Any help on this one is greatly appreciated!


Solution

  • I used PKCS7 padding for the encryption (was using Zeros)