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.