Certainly! Here’s an example of AES encryption in C#:
using System; using System.Security.Cryptography; using System.Text; class Program { static void Main() { string plaintext = "Hello, World!"; string key = "0123456789ABCDEF"; string iv = "FEDCBA9876543210"; string encryptedText = Encrypt(plaintext, key, iv); Console.WriteLine("Encrypted Text: " + encryptedText); string decryptedText = Decrypt(encryptedText, key, iv); Console.WriteLine("Decrypted Text: " + decryptedText); } static string Encrypt(string plaintext, string key, string iv) { byte[] encryptedBytes; using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = Encoding.UTF8.GetBytes(iv); ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); using (var ms = new System.IO.MemoryStream()) { using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); cs.Write(plaintextBytes, 0, plaintextBytes.Length); } encryptedBytes = ms.ToArray(); } } return Convert.ToBase64String(encryptedBytes); } static string Decrypt(string encryptedText, string key, string iv) { byte[] encryptedBytes = Convert.FromBase64String(encryptedText); string decryptedText; using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = Encoding.UTF8.GetBytes(iv); ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); using (var ms = new System.IO.MemoryStream(encryptedBytes)) { using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) { using (var sr = new System.IO.StreamReader(cs)) { decryptedText = sr.ReadToEnd(); } } } } return decryptedText; } }
In this example, we use the Aes
class from the System.Security.Cryptography
namespace to perform AES encryption and decryption. The Encrypt
method takes a plaintext string, a key, and an initialization vector (IV) as input and returns the encrypted text as a Base64-encoded string. The Decrypt
method takes the encrypted text, key, and IV as input and returns the decrypted plaintext.
Make sure to replace the key and IV values in the example with your own secure values. Additionally, it’s worth noting that this example uses UTF-8 encoding for the string conversions. You may need to adjust the encoding depending on your specific requirements.
Remember to handle exceptions appropriately and ensure the secure management of keys and encrypted data in your production code.
What is AES Encryption?
AES (Advanced Encryption Standard) is a widely used symmetric encryption algorithm. It is a block cipher that operates on fixed-size blocks of data. AES was selected as the encryption standard by the U.S. National Institute of Standards and Technology (NIST) in 2001 to replace the older Data Encryption Standard (DES).
AES supports key sizes of 128 bits, 192 bits, and 256 bits. It uses a substitution-permutation network (SPN) structure, which involves several rounds of operations on the plaintext to produce the ciphertext. During each round, AES performs byte substitution, shifting, mixing, and XOR operations.
The strength of AES lies in its key length and the number of rounds performed during encryption and decryption. AES has been extensively studied and analyzed by cryptographers, and it has withstood numerous cryptographic attacks.
AES encryption is widely used for securing sensitive data, such as in financial transactions, secure communication protocols (e.g., HTTPS), and data storage. It provides confidentiality, ensuring that only authorized parties can access the encrypted information.
It’s important to note that AES is a symmetric encryption algorithm, which means that the same key is used for both encryption and decryption. This key must be kept confidential and shared securely between the parties involved.
Overall, AES is a widely recognized and trusted encryption standard that provides strong security for protecting sensitive information.
Implementing AES Encryption in C#:
Certainly! Here’s an example of how you can implement AES encryption and decryption in C#:
using System; using System.Security.Cryptography; using System.Text; class Program { static void Main() { string plaintext = "Hello, World!"; string key = "0123456789ABCDEF"; string iv = "FEDCBA9876543210"; string encryptedText = Encrypt(plaintext, key, iv); Console.WriteLine("Encrypted Text: " + encryptedText); string decryptedText = Decrypt(encryptedText, key, iv); Console.WriteLine("Decrypted Text: " + decryptedText); } static string Encrypt(string plaintext, string key, string iv) { using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = Encoding.UTF8.GetBytes(iv); ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext); byte[] encryptedBytes = encryptor.TransformFinalBlock(plaintextBytes, 0, plaintextBytes.Length); return Convert.ToBase64String(encryptedBytes); } } static string Decrypt(string encryptedText, string key, string iv) { byte[] encryptedBytes = Convert.FromBase64String(encryptedText); using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = Encoding.UTF8.GetBytes(iv); ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); return Encoding.UTF8.GetString(decryptedBytes); } } }
In this example, the Encrypt
method takes a plaintext string, a key, and an initialization vector (IV) as input and returns the encrypted text as a Base64-encoded string. The Decrypt
method takes the encrypted text, key, and IV as input and returns the decrypted plaintext.
Make sure to replace the key and IV values in the example with your own secure values. Additionally, this example uses UTF-8 encoding for the string conversions. You may need to adjust the encoding depending on your specific requirements.
Remember to handle exceptions appropriately and ensure the secure management of keys and encrypted data in your production code.