C#实现简单的文件加密与解密方式攻略
介绍
本文详细讲解如何使用C#语言实现简单的文件加密与解密方式。文件加密是数据保护的一种方法,通过对敏感文件进行加密,确保在未授权的情况下无法访问和解读文件内容。本文讲解将使用对称加密算法 AES 实现文件加密与解密。
实现过程
- 导入 System.Security.Cryptography 命名空间。该命名空间提供了对称加密算法的支持。
using System.Security.Cryptography;
- 创建一个加解密类。
public class AesEncryption
{
// 加密密钥
private static readonly byte[] key = Encoding.UTF8.GetBytes("0123456789abcdefghijklmnopqrstuvwxyz");
// 初始化向量
private static readonly byte[] iv = Encoding.UTF8.GetBytes("9876543210zyxwvutsrqponmlkjihgfedcba");
// 加密方法
public static void EncryptFile(string inputFile, string outputFile)
{
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.Key = key;
aes.IV = iv;
using (FileStream fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
{
using (FileStream fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
{
using (CryptoStream cryptoStream = new CryptoStream(fsOutput, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int read = 0;
while ((read = fsInput.Read(buffer, 0, buffer.Length)) > 0)
{
cryptoStream.Write(buffer, 0, read);
}
cryptoStream.FlushFinalBlock();
}
}
}
}
}
// 解密方法
public static void DecryptFile(string inputFile, string outputFile)
{
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.Key = key;
aes.IV = iv;
using (FileStream fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
{
using (FileStream fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
{
using (CryptoStream cryptoStream = new CryptoStream(fsOutput, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int read = 0;
while ((read = fsInput.Read(buffer, 0, buffer.Length)) > 0)
{
cryptoStream.Write(buffer, 0, read);
}
cryptoStream.FlushFinalBlock();
}
}
}
}
}
}
- 调用加密与解密方法。
string inputFile = "path\\to\\input\\file.txt";
string encryptedFile = "path\\to\\encrypted\\file.enc";
string decryptedFile = "path\\to\\decrypted\\file.txt";
// 文件加密
AesEncryption.EncryptFile(inputFile, encryptedFile);
// 文件解密
AesEncryption.DecryptFile(encryptedFile, decryptedFile);
示例
示例1
以下示例将读取 inputfile.txt 中的文本内容,使用 AES 对称加密算法对文本内容进行加密,将加密结果输出到 encryptedfile.txt 中,然后再将 encryptedfile.txt 文件进行解密,并输出到 decryptedfile.txt 中。
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public class AesEncryption
{
// 加密密钥
private static readonly byte[] key = Encoding.UTF8.GetBytes("0123456789abcdefghijklmnopqrstuvwxyz");
// 初始化向量
private static readonly byte[] iv = Encoding.UTF8.GetBytes("9876543210zyxwvutsrqponmlkjihgfedcba");
// 加密方法
public static void EncryptFile(string inputFile, string outputFile)
{
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.Key = key;
aes.IV = iv;
using (FileStream fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
{
using (FileStream fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
{
using (CryptoStream cryptoStream = new CryptoStream(fsOutput, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int read = 0;
while ((read = fsInput.Read(buffer, 0, buffer.Length)) > 0)
{
cryptoStream.Write(buffer, 0, read);
}
cryptoStream.FlushFinalBlock();
}
}
}
}
}
// 解密方法
public static void DecryptFile(string inputFile, string outputFile)
{
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.Key = key;
aes.IV = iv;
using (FileStream fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
{
using (FileStream fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
{
using (CryptoStream cryptoStream = new CryptoStream(fsOutput, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int read = 0;
while ((read = fsInput.Read(buffer, 0, buffer.Length)) > 0)
{
cryptoStream.Write(buffer, 0, read);
}
cryptoStream.FlushFinalBlock();
}
}
}
}
}
}
public class Program
{
static void Main(string[] args)
{
string inputFile = @"C:\data\inputfile.txt";
string encryptedFile = @"C:\data\encryptedfile.enc";
string decryptedFile = @"C:\data\decryptedfile.txt";
// 文件加密
AesEncryption.EncryptFile(inputFile, encryptedFile);
// 文件解密
AesEncryption.DecryptFile(encryptedFile, decryptedFile);
Console.WriteLine("File encryption and decryption has been completed.");
}
}
示例2
以下示例将读取指定路径下的 inputfile.jpg图片,使用 AES 对称加密算法对图片进行加密,并将其输出到 encryptedfile.jpg 中,再将 encryptedfile.jpg 文件进行解密,并输出到 decryptedfile.jpg 中。
public class AesEncryption
{
// 加密密钥
private static readonly byte[] key = Encoding.UTF8.GetBytes("0123456789abcdefghijklmnopqrstuvwxyz");
// 初始化向量
private static readonly byte[] iv = Encoding.UTF8.GetBytes("9876543210zyxwvutsrqponmlkjihgfedcba");
// 加密方法
public static void EncryptFile(string inputFile, string outputFile)
{
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.Key = key;
aes.IV = iv;
using (FileStream fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
{
using (FileStream fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
{
using (CryptoStream cryptoStream = new CryptoStream(fsOutput, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int read = 0;
while ((read = fsInput.Read(buffer, 0, buffer.Length)) > 0)
{
cryptoStream.Write(buffer, 0, read);
}
cryptoStream.FlushFinalBlock();
}
}
}
}
}
// 解密方法
public static void DecryptFile(string inputFile, string outputFile)
{
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.Key = key;
aes.IV = iv;
using (FileStream fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
{
using (FileStream fsOutput = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
{
using (CryptoStream cryptoStream = new CryptoStream(fsOutput, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[4096];
int read = 0;
while ((read = fsInput.Read(buffer, 0, buffer.Length)) > 0)
{
cryptoStream.Write(buffer, 0, read);
}
cryptoStream.FlushFinalBlock();
}
}
}
}
}
}
public class Program
{
static void Main(string[] args)
{
string inputFile = @"C:\data\inputfile.jpg";
string encryptedFile = @"C:\data\encryptedfile.jpg";
string decryptedFile = @"C:\data\decryptedfile.jpg";
// 文件加密
AesEncryption.EncryptFile(inputFile, encryptedFile);
// 文件解密
AesEncryption.DecryptFile(encryptedFile, decryptedFile);
Console.WriteLine("File encryption and decryption has been completed.");
}
}
结论
本文以 AES 对称加密算法为例,讲解了 C# 实现简单的文件加密与解密方式攻略。读者可以在实际应用中根据需要,选择不同的加密算法实现更加安全的文件加密功能。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#实现简单的文件加密与解密方式 - Python技术站