我对加密相当陌生,我正在尝试让逐行加密器工作;在应用程序运行期间,我需要能够将加密的行附加到文件中,而不仅仅是一个大型的加密所有内容并保存。不过,我玩得很开心。这是我的加密器,在我自己的几次失败尝试后无耻地被盗:
class Encryption
{
private static readonly byte[] SALT = new byte[] { 0x26, 0xdc, 0xff, 0x00, 0xad, 0xed, 0x7a, 0xee, 0xc5, 0xfe, 0x07, 0xaf, 0x4d, 0x08, 0x22, 0x3c };
public static byte[] Encrypt(byte[] plain, string password)
{
MemoryStream memoryStream;
CryptoStream cryptoStream;
Rijndael rijndael = Rijndael.Create();
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, SALT);
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write);
cryptoStream.Write(plain, 0, plain.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
return memoryStream.ToArray();
}
public static byte[] Decrypt(byte[] cipher, string password)
{
MemoryStream memoryStream;
CryptoStream cryptoStream;
Rijndael rijndael = Rijndael.Create();
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, SALT);
rijndael.Key = pdb.GetBytes(32);
rijndael.IV = pdb.GetBytes(16);
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write);
cryptoStream.Write(cipher, 0, cipher.Length);
cryptoStream.FlushFinalBlock();
cryptoStream.Close();
return memoryStream.ToArray();
}
}
这是一个虚拟函数,显示了我如何尝试它:
private void EncryptFile(string filepath, string outputPath, string password) { FileInfo fileInfo = new FileInfo(filepath); 字符串文件名 = fileInfo.Name; 字符串完整路径 = 输出路径 + "\\" + 文件名; BinaryWriter writer = new BinaryWriter(File.OpenWrite(fullpath), Encoding.ASCII); /// 我在这里尝试过的两种方法: /// 1.所需的方法:逐行加密 - 我假设我能够生成 /// 多个数据块,稍后解密。这不起作用 //string[] 行 = File.ReadAllLines(filepath); /// 2. 只需阅读整个内容,然后一举加密并写入。 字符串行 = File.ReadAllText(filepath); //foreach(行中的字符串行) { byte[] bytes = Encoding.ASCII.GetBytes(line); byte[] 编码 = Encryption.Encrypt(bytes, password); writer.Write(编码); writer.Flush(); } writer.Close(); } private void DecryptFile(string filepath, string outputPath, string password) { FileInfo fileInfo = new FileInfo(filepath); 字符串文件名 = fileInfo.Name; 字符串完整路径 = 输出路径 + "\\" + 文件名; StreamWriter writer = new StreamWriter(fullpath, false, Encoding.UTF8); byte[] bytes = File.ReadAllBytes(filepath); /// 这是目前解密的方法;只是 /// 抓取所有数据并一举解密。 byte[] 解密 = Encryption.Decrypt(bytes, password); string s = Encoding.ASCII.GetString(decrypted); writer.Write(s); writer.Flush(); /// 我在这里尝试了很多东西来逐行解密, /// 这些都不起作用。这会因有关填充的问题而崩溃 /// 无效。 /* 整数索引 = 0; 整数计数 = 32; 而(索引
我不完全确定我应该做什么了。我一直在四处寻找东西并阅读在线示例,但它们似乎都是如何加密整个文件或只是加密一段数据并且除了立即再次解密之外什么都不做。我应该如何处理逐行写作?