I am trying to decrypt a string using a fairly standard algorithm.
public static string DecryptString(string cipherText)
{
string keyString = string.Empty;
keyString = Environment.GetEnvironmentVariable("EncryptKey");
if (keyString == null)
{
keyString = "E546C8DF278CD5931069B522E695D4F2";
}
var fullCipher = Convert.FromBase64String(cipherText);
using (var aesAlg = Aes.Create())
{
byte[] iv = new byte[aesAlg.BlockSize / 8];
var cipher = new byte[16];
Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, iv.Length);
var key = Encoding.UTF8.GetBytes(keyString);
string result;
using (var decryptor = aesAlg.CreateDecryptor(key, iv))
using (var msDecrypt = new MemoryStream(cipher))
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (var srDecrypt = new StreamReader(csDecrypt))
{
result = srDecrypt.ReadToEnd();
}
return result;
}
}
I get an error message:
System.Security.Cryptography.CryptographicException: The specified fill mode is not valid for this algorithm.
I tried several ways like this
var iv = new byte[16];
var cipher = new byte[16];
or
var iv = aesAlg.IV;
I am still getting an error at this point. What am I doing wrong?
source
share