Skip to content

Instantly share code, notes, and snippets.

@mazhar-ansari-ardeh
Last active April 14, 2024 08:29
Show Gist options
  • Save mazhar-ansari-ardeh/d200d91fbafc1af03a0bc0588ef7ffd0 to your computer and use it in GitHub Desktop.
Save mazhar-ansari-ardeh/d200d91fbafc1af03a0bc0588ef7ffd0 to your computer and use it in GitHub Desktop.
A sample code that demonstrates a typical AES Encryption/Decryption with C#
static class SampleAESEncryptDecrypt
{
static void Main()
{
string original = "The data to encrypt.";
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
// Setting a key size disposes the previously-set key.
// Setting a key size is redundant if a key going to be set after this statement.
// According to https://en.wikipedia.org/wiki/Advanced_Encryption_Standard, Supported key sizes are 128, 192 and 256
aes.KeySize = 256;
// aes.BlockSize = 128; // According to https://en.wikipedia.org/wiki/Advanced_Encryption_Standard: Block size for AES is always 128
byte[] key = aes.Key;
byte[] encrypted = EncryptStringToBytes(original, key);
string decrypted = DecryptStringFromBytes(encrypted, key);
Console.WriteLine(decrypted);
}
}
static byte[] EncryptStringToBytes(string str, byte[] keys)
{
byte[] encrypted;
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.Key = keys;
aes.GenerateIV(); // The get method of the 'IV' property of the 'SymmetricAlgorithm' automatically generates an IV if it is has not been generate before.
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using (MemoryStream msEncrypt = new MemoryStream())
{
msEncrypt.Write(aes.IV, 0, aes.IV.Length);
ICryptoTransform encoder = aes.CreateEncryptor();
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encoder, CryptoStreamMode.Write))
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(str);
}
encrypted = msEncrypt.ToArray();
}
}
return encrypted;
}
static string DecryptStringFromBytes(byte[] cipherText, byte[] key)
{
string decrypted;
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
// Setting a key size disposes the previously-set key.
// Setting a key size will generate a new key.
// Setting a key size is redundant if a key going to be set after this statement.
// aes.KeySize = 256;
aes.Key = key;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using (MemoryStream msDecryptor = new MemoryStream(cipherText))
{
byte[] readIV = new byte[16];
msDecryptor.Read(readIV, 0, 16);
aes.IV = readIV;
ICryptoTransform decoder = aes.CreateDecryptor();
using (CryptoStream csDecryptor = new CryptoStream(msDecryptor, decoder, CryptoStreamMode.Read))
using (StreamReader srReader = new StreamReader(csDecryptor))
{
decrypted = srReader.ReadToEnd();
}
}
}
return decrypted;
}
}
@rajmondburgaj
Copy link

This does not work in Asp.Net core: System.Security.Cryptography.CryptographicException: 'Padding is invalid and cannot be removed.'

@martinlanza
Copy link

This does not work in Asp.Net core: System.Security.Cryptography.CryptographicException: 'Padding is invalid and cannot be removed.'

Change to key size 128

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment