gpt4 book ai didi

c# - 逐行加密/解密文件?

转载 作者:太空狗 更新时间:2023-10-30 01:22:43 27 4
gpt4 key购买 nike

我对加密还很陌生,我正在尝试让逐行加密器工作;我需要能够在应用程序运行期间将加密的行附加到文件,而不是只是一个大的加密所有内容并保存。不过,我玩得很开心。这是我的加密器,在我自己尝试多次失败后无耻地被盗:


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);            /// Two methods that I've attempted here:            /// 1.  The desired method: encrypt line by line - I assumed I'd be able to generate            ///     multiple blocks of data and decrypt them later.  This isn't working            //string[] lines = File.ReadAllLines(filepath);            /// 2.  Just read the whole thing and encrypt and write it in one swoop.            string line = File.ReadAllText(filepath);            //foreach(string line in lines)            {                byte[] bytes = Encoding.ASCII.GetBytes(line);                byte[] encoded = Encryption.Encrypt(bytes, password);                writer.Write(encoded);                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);            ///  Here is the method that's working at the moment for decrypting; just            ///  grab all the data and decrypt it on one swoop.            byte[] decrypted = Encryption.Decrypt(bytes, password);            string s = Encoding.ASCII.GetString(decrypted);            writer.Write(s);            writer.Flush();            ///  I've tried a number of things here to decrypt line by line,            ///  none of which work.  This crashes with an issue about the padding            ///  being invalid.              /*            int index = 0;            int count = 32;            while (index 

我不完全确定我应该做什么了。我一直在四处寻找事物并阅读在线示例,但它们似乎都是如何加密整个文件或仅加密一段数据并且除了立即再次解密之外什么都不做。逐行写应该怎么处理?

最佳答案

我不会为您编程,而是给您一个您可以实现的方案。

如果您有逐行加密,我想您也希望能够逐行解密。请注意,“线”对于计算机来说是一个相当不便的术语。它只是一堆以某种行终止符结尾的字符。字符本身使用特定的 编码。 .

此外,我将做出以下假设:

  • 加密文本应以行形式显示,这意味着需要将字节从二进制转换为字符(使用 );
  • 您希望使用单个 key ,同时保持安全;
  • 您不需要密文的完整性保护或身份验证,只需要 secret 性。

现在思路很简单了:

  1. 使用基于密码的 key 派生函数创建单个 key ;
  2. 打开文本文件进行阅读(使用正确的 );
  3. 读取一行并将该行再次转换为字节(例如使用 UTF-8);
  4. 创建一个输出流,在下面创建一个字节数组;
  5. 创建一个随机 IV 并将其写入字节数组(IV 始终是单个 block 大小);
  6. 创建一个加密流并将其连接到相同的输出流;
  7. 加密编码行;
  8. base 64对加密后的字节进行编码,并保证在一行中;
  9. 将经过编码的加密行写入新的文本文件。

反之,逆向过程,虽然 IV 应该从 base 64 解码后的字节中检索,并且当然应该使用与加密期间相同的方法计算 key 。

关于c# - 逐行加密/解密文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12999999/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com