See my encryption and decryption respectively. Both are using the same key and Iv. This is working before now. I have tried many solutions online. Any help will be appreciacted.
public static string Encrypt(string plaintext, string secretkey, string iv)
{
try
{
using Aes myAes = Aes.Create();
myAes.Key = System.Text.Encoding.UTF8.GetBytes(secretkey);
myAes.IV = System.Text.Encoding.UTF8.GetBytes(iv);
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes_Aes(plaintext, myAes.Key, myAes.IV);
string ciphertext = ByteArrayToString(encrypted);
return ciphertext;
}
catch (Exception)
{
throw;
}
}
public static string DecryptBase64String(string cipherText, string secretkey, string secretiv)
{
byte[] buffer = Convert.FromBase64String(cipherText);
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(secretkey);
aes.IV = Encoding.UTF8.GetBytes(secretiv);
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))
{
using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))
{
return streamReader.ReadToEnd();
}
}
}
}
}