简体   繁体   中英

Error in Key Size I'm Using for AES Encryption C#

I'm trying to use the Key below given to me by my client to encrypt a string

   public static string EncryptKey()
        {
            var word = "9999";
            var key = "Z1UbeuBT7Uu3SZinrq0vzuDVXBU5FbiKksopJswQGk81";
            var iv = "KUNd9fhw48li2WUZ";
            byte[] result = null;
            byte[] wordBytes = Encoding.UTF8.GetBytes(word);
            using (MemoryStream ms = new MemoryStream())
            {
                using (RijndaelManaged AES = new RijndaelManaged())
                {
                    AES.Key = Convert.FromBase64String(key);
                    AES.IV = Encoding.UTF8.GetBytes(iv);

                    using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(wordBytes, 0, wordBytes.Length);
                        cs.Close();
                    }
                    byte[] encryptedBytes = ms.ToArray();
                    result = encryptedBytes;
                    return Convert.ToBase64String(result);
                }
            }
        }

but I get an error

System.Security.Cryptography.CryptographicException: 'Specified key is not a valid size for this algorithm.

The client has been using this Key.

What am I doing wrong?

My client uses

HttpServerUtility.UrlTokenDecode(string input);

for decoding the base64 string.

Hope this helps someone in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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