逐行加密/解密文件?
我是加密的新手,我正在努力让逐行加密器工作; 我需要能够在应用程序运行期间将加密的行附加到文件中,而不是仅仅进行一次大规模加密 – 所有内容和保存。 虽然我有一个时间的野兽。 这是我的加密器,经过多次尝试失败后无耻地被盗:
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); string filename = fileInfo.Name; string fullpath = outputPath +“\\”+ filename; BinaryWriter writer = new BinaryWriter(File.OpenWrite(fullpath),Encoding.ASCII); ///我在这里尝试过两种方法: /// 1.所需的方法:逐行加密 - 我假设我能够生成 ///多个数据块,稍后解密。 这不起作用 // string [] lines = File.ReadAllLines(filepath); /// 2.只需阅读整个内容并加密并一次性写入。 string line = File.ReadAllText(filepath); // foreach(行中的字符串行) { byte [] bytes = Encoding.ASCII.GetBytes(line); byte [] encoded = Encryption.Encrypt(bytes,password); writer.Write(编码); writer.Flush(); } writer.Close(); } private void DecryptFile(string filepath,string outputPath,string password) { FileInfo fileInfo = new FileInfo(filepath); string filename = fileInfo.Name; string fullpath = outputPath +“\\”+ filename; StreamWriter writer = new StreamWriter(fullpath,false,Encoding.UTF8); byte [] bytes = File.ReadAllBytes(filepath); ///这是目前正在解密的方法; 只是 ///抓住所有数据并一举解密。 byte [] decrypted = Encryption.Decrypt(bytes,password); string s = Encoding.ASCII.GetString(decrypted); writer.Write(一个或多个); writer.Flush(); ///我在这里尝试了很多东西来逐行解密, ///没有哪个工作。 这与填充问题崩溃 ///无效 / * int index = 0; int count = 32; 而(索引
我不完全确定我应该做什么了。 我一直在四处寻找东西并通过在线阅读示例,但它们似乎都是如何加密整个文件或只加密一段数据而不对它做任何事情,除了立即再次解密。 我应该如何处理逐行写作?
我会给你一个你可以实现的方案,而不是为你编程。
如果你有每行加密,我认为你也希望能够在每行解密。 注意,“线”对于计算机来说是相当不方便的术语。 它只是一堆以某种行终结符结尾的字符。 它们本身的字符使用特定的字符编码进行编码 。
此外,我将做出以下假设:
- 加密文本应以行显示,这意味着字节需要从二进制转换为字符(使用编码 );
- 你想要一把钥匙使用,同时保持安全;
- 您不需要完整性保护或密文validation,只需要保密。
现在想法很简单:
- 使用基于密码的密钥派生函数创建单个密钥;
- 打开文本文件进行读取(使用正确的字符编码 );
- 读取一行并再次将该行转换为字节(例如使用UTF-8);
- 创建一个输出流,在下面创建一个字节数组;
- 创建一个随机IV并将其写入字节数组(IV始终是单个块大小);
- 创建加密流并将其连接到同一输出流;
- 加密编码线;
- base 64编码加密的字节,并确保它保持在一行;
- 将编码的加密行写入新的文本文件。
相反,反过来,尽管应该在base 64解码后从字节中检索IV,并且当然应该使用加密期间使用的相同方法来计算密钥。