c#给定一个文本文件,给定一个数字密钥 (例如5),逐字符进行加密。最后输出一个加密后的文件。再次运行同样的程序,给定已经加密的文件和刚才数字密钥的相反数(例如-5),则自动生成解密后的文件。要求,解密后的文件和原文件内容相同。
时间: 2023-06-03 18:01:54 浏览: 49
这是一个技术问题,我可以回答。您可以使用C#中的StreamReader和StreamWriter来读取和写入文件,然后使用循环逐个字符进行加密和解密。加密时将每个字符的ASCII码值加上密钥,解密时将每个字符的ASCII码值减去相反数即可。
相关问题
用C# 写一个用于文本文件加密和解密的方法
以下是一个简单的C#文本文件加密和解密的方法,使用了AES加密算法:
```csharp
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public static class FileEncryption
{
private static readonly byte[] Salt = Encoding.ASCII.GetBytes("This is a salt phrase");
public static void EncryptFile(string inputFile, string outputFile, string password)
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
using (FileStream fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
{
using (FileStream fsEncrypted = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
{
using (Aes aes = Aes.Create())
{
Rfc2898DeriveBytes keyDerivationFunction = new Rfc2898DeriveBytes(passwordBytes, Salt, 1000);
aes.Key = keyDerivationFunction.GetBytes(aes.KeySize / 8);
aes.IV = keyDerivationFunction.GetBytes(aes.BlockSize / 8);
fsEncrypted.Write(Salt, 0, Salt.Length);
using (CryptoStream cs = new CryptoStream(fsEncrypted, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = fsInput.Read(buffer, 0, buffer.Length)) > 0)
{
cs.Write(buffer, 0, bytesRead);
}
}
}
}
}
}
public static void DecryptFile(string inputFile, string outputFile, string password)
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
using (FileStream fsInput = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
{
byte[] salt = new byte[Salt.Length];
fsInput.Read(salt, 0, salt.Length);
using (FileStream fsDecrypted = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
{
using (Aes aes = Aes.Create())
{
Rfc2898DeriveBytes keyDerivationFunction = new Rfc2898DeriveBytes(passwordBytes, salt, 1000);
aes.Key = keyDerivationFunction.GetBytes(aes.KeySize / 8);
aes.IV = keyDerivationFunction.GetBytes(aes.BlockSize / 8);
using (CryptoStream cs = new CryptoStream(fsInput, aes.CreateDecryptor(), CryptoStreamMode.Read))
{
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = cs.Read(buffer, 0, buffer.Length)) > 0)
{
fsDecrypted.Write(buffer, 0, bytesRead);
}
}
}
}
}
}
}
```
使用示例:
```csharp
string inputFile = @"C:\plaintext.txt";
string encryptedFile = @"C:\encrypted.dat";
string decryptedFile = @"C:\decrypted.txt";
string password = "MySecretPassword";
FileEncryption.EncryptFile(inputFile, encryptedFile, password);
FileEncryption.DecryptFile(encryptedFile, decryptedFile, password);
```
注意:这只是一个简单的示例,实际应用中需要更复杂的加密和解密方法来确保数据的安全。
C#给定一个不确定长度字符 ,截取一个相同之处后面的字符
你可以尝试使用字符串的 `Substring` 方法来实现这个功能。具体的步骤如下:
1. 找到相同之处,可以使用循环遍历两个字符串的字符进行比较,找到第一个不同的字符的位置。
2. 使用 `Substring` 方法截取第二个字符串中相同之处后面的字符,方法的参数为相同之处的位置和第二个字符串的长度减去相同之处的位置。
示例代码如下:
```csharp
string str1 = "abcdefg";
string str2 = "abcfghk";
int i = 0;
while (i < str1.Length && i < str2.Length && str1[i] == str2[i])
{
i++;
}
if (i < str2.Length)
{
string result = str2.Substring(i);
}
```
在上面的代码中,我们首先定义了两个字符串 `str1` 和 `str2`,然后使用一个循环遍历这两个字符串的字符,找到第一个不同的字符的位置,最后使用 `Substring` 方法截取第二个字符串中相同之处后面的字符并将结果保存在 `result` 变量中。