C# 中提供了多種加密解密算法和相關類庫,可以用來保護資料的安全性。下面将簡單介紹幾種常用的加密解密算法,并給出相應的執行個體。
- 對稱加密算法(Symmetric Encryption): 對稱加密算法使用相同的密鑰進行加密和解密,是最常見的加密算法之一。C# 中提供了 System.Security.Cryptography 命名空間,其中包含了一些常用的對稱加密算法,如 AES、DES 和 TripleDES。
下面是一個使用 AES 算法進行對稱加密和解密的示例:
using System;
using System.Text;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
string plainText = "Hello, World!"; // 待加密的明文
using (Aes aesAlg = Aes.Create())
{
aesAlg.GenerateKey();
aesAlg.GenerateIV();
byte[] encryptedData = EncryptStringToBytes_Aes(plainText, aesAlg.Key, aesAlg.IV);
string decryptedText = DecryptStringFromBytes_Aes(encryptedData, aesAlg.Key, aesAlg.IV);
Console.WriteLine("Plaintext: " + plainText);
Console.WriteLine("Encrypted data: " + Convert.ToBase64String(encryptedData));
Console.WriteLine("Decrypted text: " + decryptedText);
}
}
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] key, byte[] iv)
{
byte[] encrypted;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (var msEncrypt = new System.IO.MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var swEncrypt = new System.IO.StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
return encrypted;
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] key, byte[] iv)
{
string plaintext = null;
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (var msDecrypt = new System.IO.MemoryStream(cipherText))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var srDecrypt = new System.IO.StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
- 非對稱加密算法(Asymmetric Encryption): 非對稱加密算法使用一對密鑰,即公鑰和私鑰,其中公鑰用于加密資料,私鑰用于解密資料。C# 中的 System.Security.Cryptography 命名空間中提供了 RSA 算法的實作。
下面是一個使用 RSA 算法進行非對稱加密和解密的示例:
using System;
using System.Text;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
string plainText = "Hello, World!"; // 待加密的明文
using (RSA rsa = RSA.Create())
{
byte[] encryptedData = EncryptStringToBytes_RSA(plainText, rsa.ExportRSAPublicKey());
string decryptedText = DecryptStringFromBytes_RSA(encryptedData, rsa.ExportRSAPrivateKey());
Console.WriteLine("Plaintext: " + plainText);
Console.WriteLine("Encrypted data: " + Convert.ToBase64String(encryptedData));
Console.WriteLine("Decrypted text: " + decryptedText);
}
}
static byte[] EncryptStringToBytes_RSA(string plainText, RSAParameters publicKey)
{
byte[] encrypted;
using (RSA rsa = RSA.Create())
{
rsa.ImportRSAPublicKey(publicKey, out _);
encrypted = rsa.Encrypt(Encoding.UTF8.GetBytes(plainText), RSAEncryptionPadding.OaepSHA256);
}
return encrypted;
}
static string DecryptStringFromBytes_RSA(byte[] cipherText, RSAParameters privateKey)
{
string plaintext = null;
using (RSA rsa = RSA.Create())
{
rsa.ImportRSAPrivateKey(privateKey, out _);
byte[] decryptedBytes = rsa.Decrypt(cipherText, RSAEncryptionPadding.OaepSHA256);
plaintext = Encoding.UTF8.GetString(decryptedBytes);
}
return plaintext;
}
}
- 雜湊演算法(Hashing): 雜湊演算法将任意長度的資料映射為固定長度的哈希值,不可逆且唯一。在 C# 中,System.Security.Cryptography 命名空間提供了多種雜湊演算法的實作,如 MD5、SHA1、SHA256 等。
下面是一個使用 MD5 算法生成哈希值的示例:
using System;
using System.Text;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
string plainText = "Hello, World!"; // 待加密的明文
string hashValue = GetMD5Hash(plainText);
Console.WriteLine("Plaintext: " + plainText);
Console.WriteLine("Hash value: " + hashValue);
}
static string GetMD5Hash(string input)
{
using (MD5 md5Hash = MD5.Create())
{
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
}
}
以上是 C# 中常用的加密解密算法的解釋和示例,你可以根據自己的需求選擇合适的算法來保護資料的安全性。
創作不易,如果您喜歡還請幫忙點贊關注,謝謝![作揖]