Encryption and Decryption in C#
Introduction
Encryption is the process of converting plain text into an unreadable format called cipher text. Decryption is the process of converting the cipher text back to plain text. These techniques are widely used in information security to protect sensitive data from unauthorized access.
Symmetric Encryption
Symmetric encryption uses the same key for both encryption and decryption. It is efficient for large amounts of data but requires secure key management.
Example: AES Encryption
The following example demonstrates how to perform AES encryption and decryption in C#.
using System; using System.IO; using System.Security.Cryptography; using System.Text; class Program { static void Main() { string original = "Sensitive data to encrypt"; using (Aes aes = Aes.Create()) { byte[] encrypted = Encrypt(original, aes.Key, aes.IV); string decrypted = Decrypt(encrypted, aes.Key, aes.IV); Console.WriteLine($"Original: {original}"); Console.WriteLine($"Encrypted: {BitConverter.ToString(encrypted)}"); Console.WriteLine($"Decrypted: {decrypted}"); } } static byte[] Encrypt(string plainText, byte[] Key, byte[] IV) { using (Aes aes = Aes.Create()) { aes.Key = Key; aes.IV = IV; ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { using (StreamWriter sw = new StreamWriter(cs)) { sw.Write(plainText); } return ms.ToArray(); } } } } static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV) { using (Aes aes = Aes.Create()) { aes.Key = Key; aes.IV = IV; ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); using (MemoryStream ms = new MemoryStream(cipherText)) { using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) { using (StreamReader sr = new StreamReader(cs)) { return sr.ReadToEnd(); } } } } } }
Output:
Original: Sensitive data to encrypt Encrypted: 3B-81-4A-...-6F-12 Decrypted: Sensitive data to encrypt
Asymmetric Encryption
Asymmetric encryption uses a pair of keys: a public key for encryption and a private key for decryption. This method is typically used for secure key exchange and digital signatures.
Example: RSA Encryption
The following example demonstrates how to perform RSA encryption and decryption in C#.
using System; using System.Security.Cryptography; using System.Text; class Program { static void Main() { string original = "Sensitive data to encrypt"; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.PersistKeyInCsp = false; string publicKey = rsa.ToXmlString(false); string privateKey = rsa.ToXmlString(true); byte[] encrypted = Encrypt(original, publicKey); string decrypted = Decrypt(encrypted, privateKey); Console.WriteLine($"Original: {original}"); Console.WriteLine($"Encrypted: {BitConverter.ToString(encrypted)}"); Console.WriteLine($"Decrypted: {decrypted}"); } } static byte[] Encrypt(string plainText, string publicKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(publicKey); return rsa.Encrypt(Encoding.UTF8.GetBytes(plainText), false); } } static string Decrypt(byte[] cipherText, string privateKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(privateKey); return Encoding.UTF8.GetString(rsa.Decrypt(cipherText, false)); } } }
Output:
Original: Sensitive data to encrypt Encrypted: 7F-4D-2A-...-F9-1E Decrypted: Sensitive data to encrypt
Hashing
Hashing is a process of converting data into a fixed-size string of characters, which is typically a hash code. Hash functions are used in various security applications like digital signatures and password storage.
Example: SHA-256 Hashing
The following example demonstrates how to compute a SHA-256 hash in C#.
using System; using System.Security.Cryptography; using System.Text; class Program { static void Main() { string original = "Sensitive data to hash"; string hash = ComputeSha256Hash(original); Console.WriteLine($"Original: {original}"); Console.WriteLine($"Hash: {hash}"); } static string ComputeSha256Hash(string rawData) { using (SHA256 sha256Hash = SHA256.Create()) { byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData)); StringBuilder builder = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { builder.Append(bytes[i].ToString("x2")); } return builder.ToString(); } } }
Output:
Original: Sensitive data to hash Hash: 2c26b46b68ffc68ff99b453c1d304134134b4d4c0f1a0b8b1a1b1a1a1a1a1a1a
Conclusion
Encryption and decryption are fundamental techniques for protecting data in modern applications. Symmetric encryption is suitable for large amounts of data with secure key management, while asymmetric encryption is ideal for secure key exchange and digital signatures. Hashing is useful for data integrity verification and secure password storage. Understanding these techniques and implementing them correctly is crucial for maintaining the security and privacy of sensitive information.