If you have been using the RijndaelManaged
.NET crypto class for your AES encryptions, you'll soon start seeing deprecation warnings like this:
RijndaelManaged is obsolete: The Rijndael and RijndaelManaged types are obsolete. Use Aes instead.
Here are some working (and tested) Encrypt()
and Decrypt()
samples to replace what you have.
Assuming you want constant encryption keys (that don't randomly change out from under you each time your methods execute), first generate a KEY and an IV like so:
- Go here: https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes
- Create a simple console app out of that. Run it in debug mode.
- Put a breakpoint where you can examine
myAes.Key
andmyAes.IV
- Place those in the
Watch
window and wrap them each inSystem.Text.Encoding.Default.GetString()
so you convert the byte array to a string that can be used as your key. Place those strings in a private const within your class. Each time you run the console app you'll get different keys, so that's an easy way to get new keys, and you'll never get the same keys as someone else running this sample, because it's randomized each time.
Here's what I got:
private const string _aesKey = "MjMgEO5A2Q...5OxRw=";
private const string _aesIV = "Z/ENPB...zl5A==";
And here are the methods for you:
public static string Encrypt(string rawText) {
if (!string.IsNullOrEmpty(rawText)) {
using(Aes aes = Aes.Create()) {
aes.Key = Convert.FromBase64String(_aesKey);
aes.IV = Convert.FromBase64String(_aesIV);
byte[] encryptedBytes = EncryptStringToBytes_Aes(rawText, aes.Key, aes.IV);
return Convert.ToBase64String(encryptedBytes);
}
} else {
return string.Empty;
}
}
public static string Decrypt(string encryption) {
if (!string.IsNullOrEmpty(encryption)) {
using(Aes aes = Aes.Create()) {
aes.Key = Convert.FromBase64String(_aesKey);
aes.IV = Convert.FromBase64String(_aesIV);
byte[] data = Convert.FromBase64String(encryption);
return DecryptStringFromBytes_Aes(data, aes.Key, aes.IV);
}
} else {
return string.Empty;
}
}
public static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV) {
if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV");
byte[] encrypted;
using(Aes aesAlg = Aes.Create()) {
aesAlg.Key = Key;
aesAlg.IV = IV;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using(MemoryStream msEncrypt = new MemoryStream()) {
using(CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
using(StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
return encrypted;
}
public static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV) {
if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV");
string plaintext = null;
using(Aes aesAlg = Aes.Create()) {
aesAlg.Key = Key;
aesAlg.IV = IV;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using(MemoryStream msDecrypt = new MemoryStream(cipherText)) {
using(CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
using(StreamReader srDecrypt = new StreamReader(csDecrypt)) {
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
If you're performing large numbers of encryptions per-run, consider moving the Aes.Create()
constructions up into a constructor or single-use method for better efficiency.