Using AES (Advanced Encryption Standard) encryption in C# involves several steps, including selecting the appropriate AES variant, handling keys, and implementing encryption and decryption routines. Below is a simple guide to get you started with AES encryption in C#.
1. Choose AES Variant:
Decide on the AES variant to use (AES-128, AES-192, or AES-256). This choice will determine the key size and the level of security.
2. Create a Key:
Generate a secure random key of the appropriate size using a cryptographic library. In C#, you can use RNGCryptoServiceProvider for this purpose.
csharp
using System.Security.Cryptography;
byte[] key = new byte[16]; // for AES-128
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(key);
}
3. Choose a Mode and Padding:
Decide on the mode of operation (e.g., CBC, GCM) and the padding scheme (e.g., PKCS7). These choices affect how the data is processed during encryption and decryption.
4. Implement Encryption:
Use the AesManaged class from the System.Security.Cryptography namespace for encryption. Heres a simple example:
csharp
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public class AesEncryption
{
public static byte[] Encrypt(string plainText, byte[] key, byte[] iv)
{
using (AesManaged aesAlg = new AesManaged())
{
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);
}
}
return msEncrypt.ToArray();
}
}
}
}
5. Implement Decryption:
Implement a similar decryption method using the AesManaged class.
csharp
public class AesEncryption
{
// ... (previous code)
public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv)
{
using (AesManaged aesAlg = new AesManaged())
{
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))
{
return srDecrypt.ReadToEnd();
}
}
}
}
}
}
6. Usage Example:
Now, you can use the Encrypt and Decrypt methods in your application.
csharp
byte[] iv = new byte[16]; // Initialization Vector, should be unique for each encryption
string plaintext = "Hello, AES!";
byte[] encrypted = AesEncryption.Encrypt(plaintext, key, iv);
string decrypted = AesEncryption.Decrypt(encrypted, key, iv);
Console.WriteLine($"Original: {plaintext}");
Console.WriteLine($"Encrypted: {Convert.ToBase64String(encrypted)}");
Console.WriteLine($"Decrypted: {decrypted}");
This is a basic guide, and real-world implementations should consider additional security aspects, such as key management and secure random number generation. Always handle encryption keys securely and follow best practices for cryptographic operations.
Comments
Post a Comment
Provide your valuable feedback, we would love to hear from you!! Follow our WhatsApp Channel at
https://whatsapp.com/channel/0029VaKapP65a23urLOUs40y