C#经常用的加密解密算法
作者:野牛程序员:2024-01-06 21:57:52C#阅读 2675
常见的C#加密解密算法包括AES(高级加密标准)、DES(数据加密标准)、RSA(非对称加密算法)等。这些算法可用于保护数据的机密性和完整性。AES是对称加密算法,适用于对大量数据进行加密,而RSA则常用于加密小段数据,如密钥交换等。另外,还有哈希算法如SHA-256,用于生成数据的摘要,用于验证数据的完整性。这些算法在C#中通过.NET Framework提供的加密类库来实现。
以下是一个简单的C#示例,演示了使用AES进行对称加密和解密的过程:
using System; using System.Security.Cryptography; using System.Text; class EncryptionExample { static void Main() { // 原始数据 string originalData = "Hello, World!"; // 生成随机的密钥和初始化向量 byte[] key = GenerateRandomBytes(32); // 256 bits byte[] iv = GenerateRandomBytes(16); // 128 bits // 加密数据 byte[] encryptedData = EncryptData(originalData, key, iv); // 解密数据 string decryptedData = DecryptData(encryptedData, key, iv); // 输出结果 Console.WriteLine("Original Data: " + originalData); Console.WriteLine("Encrypted Data: " + Convert.ToBase64String(encryptedData)); Console.WriteLine("Decrypted Data: " + decryptedData); } static byte[] EncryptData(string data, byte[] key, byte[] iv) { using (Aes aesAlg = Aes.Create()) { 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(data); } } return msEncrypt.ToArray(); } } } static string DecryptData(byte[] encryptedData, byte[] key, byte[] iv) { using (Aes aesAlg = Aes.Create()) { aesAlg.Key = key; aesAlg.IV = iv; ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (MemoryStream msDecrypt = new MemoryStream(encryptedData)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { return srDecrypt.ReadToEnd(); } } } } } static byte[] GenerateRandomBytes(int length) { byte[] randomBytes = new byte[length]; using (RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider()) { rngCsp.GetBytes(randomBytes); } return randomBytes; } }
以下是一个简单的C#示例,演示了使用DES进行对称加密和解密的过程:
using System; using System.IO; using System.Security.Cryptography; using System.Text; class DESEncryptionExample { static void Main() { // 原始数据 string originalData = "Hello, World!"; // 生成随机的密钥和初始化向量 byte[] key = Encoding.UTF8.GetBytes("12345678"); byte[] iv = Encoding.UTF8.GetBytes("abcdefgh"); // 加密数据 byte[] encryptedData = EncryptData(originalData, key, iv); // 解密数据 string decryptedData = DecryptData(encryptedData, key, iv); // 输出结果 Console.WriteLine("Original Data: " + originalData); Console.WriteLine("Encrypted Data: " + Convert.ToBase64String(encryptedData)); Console.WriteLine("Decrypted Data: " + decryptedData); } static byte[] EncryptData(string data, byte[] key, byte[] iv) { using (DESCryptoServiceProvider desAlg = new DESCryptoServiceProvider()) { desAlg.Key = key; desAlg.IV = iv; ICryptoTransform encryptor = desAlg.CreateEncryptor(desAlg.Key, desAlg.IV); using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(data); } } return msEncrypt.ToArray(); } } } static string DecryptData(byte[] encryptedData, byte[] key, byte[] iv) { using (DESCryptoServiceProvider desAlg = new DESCryptoServiceProvider()) { desAlg.Key = key; desAlg.IV = iv; ICryptoTransform decryptor = desAlg.CreateDecryptor(desAlg.Key, desAlg.IV); using (MemoryStream msDecrypt = new MemoryStream(encryptedData)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { return srDecrypt.ReadToEnd(); } } } } } }
RSA(非对称加密算法)
以下是一个简单的C#示例,演示了如何使用RSA进行非对称加密和解密的过程:
using System; using System.Security.Cryptography; using System.Text; class RSAEncryptionExample { static void Main() { // 原始数据 string originalData = "Hello, World!"; // 创建RSA密钥对 using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { // 获取公钥和私钥 string publicKey = rsa.ToXmlString(false); string privateKey = rsa.ToXmlString(true); // 加密数据 byte[] encryptedData = EncryptData(originalData, publicKey); // 解密数据 string decryptedData = DecryptData(encryptedData, privateKey); // 输出结果 Console.WriteLine("Original Data: " + originalData); Console.WriteLine("Encrypted Data: " + Convert.ToBase64String(encryptedData)); Console.WriteLine("Decrypted Data: " + decryptedData); } } static byte[] EncryptData(string data, string publicKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { // 设置公钥 rsa.FromXmlString(publicKey); // 加密数据 byte[] encryptedData = rsa.Encrypt(Encoding.UTF8.GetBytes(data), false); return encryptedData; } } static string DecryptData(byte[] encryptedData, string privateKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { // 设置私钥 rsa.FromXmlString(privateKey); // 解密数据 byte[] decryptedData = rsa.Decrypt(encryptedData, false); return Encoding.UTF8.GetString(decryptedData); } } }
RSA适用于加密较小的数据块,因此通常用于加密对称加密算法中的密钥而不是整个消息。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
