在计算机中,TXT文件是一种常见的文本文件格式,其内容可以比较容易地被其他人读取和修改。如果您需要保护文件的私密性或保密性,您可以考虑使用.NET C#对TXT文件进行加密。本文将为您介绍一种简单的加密方法。
下面是详细的步骤:
1. 准备工作
在开始之前,请确保您已经安装了Visual Studio和.NET框架。
2. 创建加密方法
在C#中,您可以使用Rijndael加密算法对TXT文件进行加密。以下代码演示如何创建一个加密方法:
public static void EncryptFile(string inputFile, string outputFile, string password)
{
byte[] salt = new byte[8];
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(salt);
using (var aes = new RijndaelManaged())
{
aes.KeySize = 256;
aes.BlockSize = 128;
var key = new Rfc2898DeriveBytes(password, salt, 1000);
aes.Key = key.GetBytes(aes.KeySize / 8);
aes.IV = key.GetBytes(aes.BlockSize / 8);
aes.Mode = CipherMode.CBC;
using (var fsIn = new FileStream(inputFile, FileMode.Open))
{
using (var fsOut = new FileStream(outputFile, FileMode.Create))
{
fsOut.Write(salt, 0, salt.Length);
using (var cs = new CryptoStream(fsOut, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[1048576];
int read;
while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
{
cs.Write(buffer, 0, read);
}
cs.FlushFinalBlock();
fsOut.Flush();
}
}
}
}
}
}
3. 创建解密方法
同样,您也可以使用Rijndael解密算法对加密的TXT文件进行解密。以下是如何创建一个解密方法的示例代码:
public static void DecryptFile(string inputFile, string outputFile, string password)
{
byte[] salt = new byte[8];
using (var fsIn = new FileStream(inputFile, FileMode.Open))
{
fsIn.Read(salt, 0, salt.Length);
using (var aes = new RijndaelManaged())
{
aes.KeySize = 256;
aes.BlockSize = 128;
var key = new Rfc2898DeriveBytes(password, salt, 1000);
aes.Key = key.GetBytes(aes.KeySize / 8);
aes.IV = key.GetBytes(aes.BlockSize / 8);
aes.Mode = CipherMode.CBC;
using (var fsOut = new FileStream(outputFile, FileMode.Create))
{
using (var cs = new CryptoStream(fsIn, aes.CreateDecryptor(), CryptoStreamMode.Read))
{
byte[] buffer = new byte[1048576];
int read;
while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
{
fsOut.Write(buffer, 0, read);
}
fsOut.Flush();
}
}
}
}
}
4. 运行并测试
通过以上的方法,您就可以轻松地对TXT文件进行加密和解密了。您可以使用Visual Studio创建一个简单的C#应用程序,并嵌入上述代码进行测试。
当然也可以不使用密码来进行加密
public static string EncryptFile(string filepath)
{
//加密后的文件名及路径
string filepathnew = Path.Combine(Path.GetDirectoryName(filepath), Path.GetFileNameWithoutExtension(filepath) + "-加密" + Path.GetExtension(filepath));
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
RijndaelManaged myRijndael = new RijndaelManaged();
using (FileStream fsOut = File.Open(filepathnew, FileMode.Create, FileAccess.Write))
{
using (FileStream fsIn = File.Open(filepath, FileMode.Open, FileAccess.Read))
{
//写入加密文本文件
using (CryptoStream csDecrypt = new CryptoStream(fsOut, myRijndael.CreateEncryptor(key, IV), CryptoStreamMode.Write))
{
//读加密文本
using (BinaryReader br = new BinaryReader(fsIn))
{
csDecrypt.Write(br.ReadBytes((int)fsIn.Length), 0, (int)fsIn.Length);
csDecrypt.FlushFinalBlock();
csDecrypt.Close();
}
}
}
}
return filepathnew;
}
public static string DecipFile(string filepath)
{
//加密后的文件名及路径
string filepathnew = Path.Combine(Path.GetDirectoryName(filepath), Path.GetFileNameWithoutExtension(filepath).Replace("-加密", "") + "-解密" + Path.GetExtension(filepath));
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
RijndaelManaged myRijndael = new RijndaelManaged();
using (FileStream fsOut = File.Open(filepath, FileMode.Open, FileAccess.Read))
{
using (CryptoStream csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key, IV), CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(csDecrypt))//把文件读出来
{
using (StreamWriter sw = new StreamWriter(filepathnew))
{
//解密后文件写入一个新的文件
sw.Write(sr.ReadToEnd());
sw.Flush();
sw.Close();
sr.Close();
fsOut.Close();
}
}
}
}
return filepathnew;
}