Cryptography in C# (.NET): Encryption and Hashing

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Cryptography in C# (.NET): Encryption and Hashing



Data security is one of the key aspects of modern development. The .NET platform provides a powerful and flexible set of tools for cryptographic protection of information in the System.Security.Cryptography namespace. In this article, we will explore the main capabilities of cryptography in C# (.NET): symmetric and asymmetric encryption, password hashing, and creating digital signatures.



1. Symmetric Encryption (AES)



Symmetric encryption uses the same key to encrypt and decrypt data. The most common algorithm is AES (Advanced Encryption Standard). In .NET, it is implemented by the Aes class.



Example of encrypting and decrypting a string using AES:



using System;using System.IO;using System.Security.Cryptography;using System.Text;

public class AesExample{ public static byte[] EncryptStringToBytes_Aes(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 msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } return msEncrypt.ToArray(); } } } }

public static string DecryptStringFromBytes_Aes(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 msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { return srDecrypt.ReadToEnd(); } } } } }}


2. Asymmetric Encryption (RSA)



Asymmetric encryption uses a pair of keys: a public key for encryption and a private key for decryption. In .NET, this mechanism is implemented by the RSA class. RSA is often used for secure transmission of symmetric keys.



Example of key generation and encryption using RSA:



using System;using System.Security.Cryptography;using System.Text;

public class RsaExample{ public static void RsaEncryptDecrypt() { using (RSA rsa = RSA.Create()) { // Key generation (2048 bits) RSAParameters publicKey = rsa.ExportParameters(false); // only public key RSAParameters privateKey = rsa.ExportParameters(true); // both keys

string original = "Secret message for RSA";

// Encryption with public key byte[] encryptedData = rsa.Encrypt(Encoding.UTF8.GetBytes(original), RSAEncryptionPadding.OaepSHA256);

// Decryption with private key byte[] decryptedData = rsa.Decrypt(encryptedData, RSAEncryptionPadding.OaepSHA256); string decrypted = Encoding.UTF8.GetString(decryptedData);

Console.WriteLine($"Original: {original}"); Console.WriteLine($"Decrypted: {decrypted}"); } }}


3. Data Hashing (SHA-256, MD5)



Hashing

Recommendations