C#实现常见加密算法的示例代码
本篇攻略将会针对C#语言来实现几种常见的加密算法,包括MD5哈希、SHA-1哈希、AES对称加密、RSA非对称加密和Base64编码等。我们将会给出具体的实现代码,并接合实例说明,方便大家在实际开发中使用。
MD5哈希
MD5消息摘要算法常用于数据传输过程中的完整性校验,或者用于保证敏感数据的安全。
示例1:字符串的MD5哈希值
using System;
using System.Security.Cryptography;
using System.Text;
class Program {
static void Main(string[] args) {
string input = "hello, world!";
Console.WriteLine("输入字符串:" + input);
var md5 = MD5.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
string output = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
Console.WriteLine("MD5哈希值:" + output);
Console.ReadKey();
}
}
示例2:文件的MD5哈希值
using System;
using System.IO;
using System.Security.Cryptography;
class Program {
static void Main(string[] args) {
string fileName = "example.txt";
Console.WriteLine("文件名:" + fileName);
var md5 = MD5.Create();
using (var stream = File.OpenRead(fileName)) {
byte[] hashBytes = md5.ComputeHash(stream);
string output = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
Console.WriteLine("MD5哈希值:" + output);
}
Console.ReadKey();
}
}
SHA-1哈希
SHA-1哈希算法也常用于数据传输过程中的完整性校验,但相对于MD5,它的安全性更高。
示例:字符串的SHA-1哈希值
using System;
using System.Security.Cryptography;
using System.Text;
class Program {
static void Main(string[] args) {
string input = "hello, world!";
Console.WriteLine("输入字符串:" + input);
var sha1 = SHA1.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hashBytes = sha1.ComputeHash(inputBytes);
string output = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
Console.WriteLine("SHA-1哈希值:" + output);
Console.ReadKey();
}
}
AES对称加密
AES对称加密算法常用于数据的加密和解密过程中,相对于DES,它的安全性更高。
示例:字符串的AES对称加密和解密
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class AesEncryptor {
private readonly byte[] key;
private readonly byte[] iv;
public AesEncryptor(string key, string iv) {
this.key = Encoding.ASCII.GetBytes(key);
this.iv = Encoding.ASCII.GetBytes(iv);
}
public byte[] Encrypt(byte[] input) {
using (var aes = Aes.Create()) {
aes.Key = key;
aes.IV = iv;
var encryptor = aes.CreateEncryptor();
using (var stream = new MemoryStream()) {
using (var cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write)) {
cryptoStream.Write(input, 0, input.Length);
cryptoStream.FlushFinalBlock();
return stream.ToArray();
}
}
}
}
public byte[] Decrypt(byte[] input) {
using (var aes = Aes.Create()) {
aes.Key = key;
aes.IV = iv;
var decryptor = aes.CreateDecryptor();
using (var stream = new MemoryStream()) {
using (var cryptoStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Write)) {
cryptoStream.Write(input, 0, input.Length);
cryptoStream.FlushFinalBlock();
return stream.ToArray();
}
}
}
}
}
class Program {
static void Main(string[] args) {
string input = "hello, world!";
Console.WriteLine("输入字符串:" + input);
var encryptor = new AesEncryptor("my_secret_key_123", "my_secret_iv_456");
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] encryptedBytes = encryptor.Encrypt(inputBytes);
Console.WriteLine("加密结果:" + Convert.ToBase64String(encryptedBytes));
byte[] decryptedBytes = encryptor.Decrypt(encryptedBytes);
string output = Encoding.UTF8.GetString(decryptedBytes);
Console.WriteLine("解密结果:" + output);
Console.ReadKey();
}
}
RSA非对称加密
RSA非对称加密算法常用于数字签名和数据的加密和解密过程中,它采用公钥进行加密,采用私钥进行解密。
示例:字符串的RSA非对称加密和解密
using System;
using System.Security.Cryptography;
using System.Text;
class RsaEncryptor {
private readonly RSA rsa;
public RsaEncryptor(int keyLength) {
rsa = RSA.Create(keyLength);
}
public byte[] Encrypt(byte[] input, RSAParameters publicKey = new RSAParameters()) {
rsa.ImportParameters(publicKey);
return rsa.Encrypt(input, RSAEncryptionPadding.Pkcs1);
}
public byte[] Decrypt(byte[] input) {
return rsa.Decrypt(input, RSAEncryptionPadding.Pkcs1);
}
public RSAParameters GetPublicKey() {
return rsa.ExportParameters(false);
}
public RSAParameters GetPrivateKey() {
return rsa.ExportParameters(true);
}
}
class Program {
static void Main(string[] args) {
string input = "hello, world!";
Console.WriteLine("输入字符串:" + input);
var encryptor = new RsaEncryptor(2048);
RSAParameters publicKey = encryptor.GetPublicKey();
RSAParameters privateKey = encryptor.GetPrivateKey();
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] encryptedBytes = encryptor.Encrypt(inputBytes, publicKey);
Console.WriteLine("加密结果:" + Convert.ToBase64String(encryptedBytes));
byte[] decryptedBytes = encryptor.Decrypt(encryptedBytes);
string output = Encoding.UTF8.GetString(decryptedBytes);
Console.WriteLine("解密结果:" + output);
Console.ReadKey();
}
}
Base64编码
Base64编码算法常用于数据的传输过程中,将二进制数据转换为可打印的ASCII字符串。
示例:字符串的Base64编码和解码
using System;
using System.Text;
class Base64Encoder {
public string Encode(byte[] input) {
return Convert.ToBase64String(input);
}
public byte[] Decode(string input) {
return Convert.FromBase64String(input);
}
}
class Program {
static void Main(string[] args) {
string input = "hello, world!";
Console.WriteLine("输入字符串:" + input);
var encoder = new Base64Encoder();
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
string encodedString = encoder.Encode(inputBytes);
Console.WriteLine("Base64编码结果:" + encodedString);
byte[] decodedBytes = encoder.Decode(encodedString);
string output = Encoding.UTF8.GetString(decodedBytes);
Console.WriteLine("Base64解码结果:" + output);
Console.ReadKey();
}
}
以上就是C#实现常见加密算法的示例代码的完整攻略,希望能够对读者有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#实现常见加密算法的示例代码 - Python技术站