Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AES decryption fails in chinese locale with invalid character error

I have the following code:

private static string AesDecrypt(string text, string key)
    {
        var encoding = Encoding.Default;
        var aes = new RijndaelManaged
        {
            KeySize = 256,
            BlockSize = 256,
            Padding = PaddingMode.Zeros,
            Mode = CipherMode.CBC,
            Key = encoding.GetBytes(key)
        };

        text = encoding.GetString(Convert.FromBase64String(text));

        var iv = text;
        iv = iv.Substring(iv.IndexOf("-[--IV-[-", StringComparison.Ordinal) + 9);
        text = text.Replace("-[--IV-[-" + iv, "");

        text = Convert.ToBase64String(encoding.GetBytes(text));
        aes.IV = encoding.GetBytes(iv);

        using (var aesDecrypt = aes.CreateDecryptor(aes.Key, aes.IV))
        {
            var buffer = Convert.FromBase64String(text);

            var result = encoding.GetString(aesDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
            result = result.Substring(0, result.IndexOf(Convert.ToChar(0)));

            return result;
        }
    }

    private static string AesEncrypt(string text, string key)
    {
        var encoding = Encoding.Default;
        var aes = new RijndaelManaged
        {
            KeySize = 256,
            BlockSize = 256,
            Padding = PaddingMode.Zeros,
            Mode = CipherMode.CBC,
            Key = (encoding.GetBytes(key))
        };

        aes.GenerateIV();
        var iv = ("-[--IV-[-" + encoding.GetString(aes.IV));

        using (var aesEncrypt = aes.CreateEncryptor(aes.Key, aes.IV))
        {
            var buffer = encoding.GetBytes(text);

            return Convert.ToBase64String(encoding.GetBytes(encoding.GetString(aesEncrypt.TransformFinalBlock(buffer, 0, buffer.Length)) + iv));
        }
    }

It works fine when it is accessed through a system with English (United States) Locale. However, once I tried it in a system with Chinese locale, it throws the following errors:

Specified initialization vector (IV) does not match the block size for this algorithm.

or

Invalid character in a Base-64 string.

Some sources say that I should use Encoding.UTF8 or Encoding.Unicode to solve this problem. I tried it but the same errors show up.

like image 804
CudoX Avatar asked Dec 05 '25 07:12

CudoX


1 Answers

It seems it was really more of a string-decoding issue than an encryption issue like Rawling said. Thank you for pointing that out.

Anyways, instead of using Encoding.Default, Encoding.UTF or Encoding.Unicode, I tried to use a United States-based encoding (OEM United States) which resolved my issue:

Encoding.GetEncoding(437);

https://msdn.microsoft.com/en-us/library/system.text.encoding(v=vs.110).aspx

Thanks again!

like image 142
CudoX Avatar answered Dec 08 '25 10:12

CudoX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!