gpt4 book ai didi

c# - 从加密流读取到流的末尾

转载 作者:太空狗 更新时间:2023-10-30 00:05:39 25 4
gpt4 key购买 nike

我在使用下面的代码时遇到了一些问题。我在需要加密的临时位置有一个文件,此函数加密该数据,然后将其存储在“pathToSave”位置。

经检查似乎没有正确处理整个文件 - 我的输出中缺少一些位,我怀疑这与 while 循环没有在整个流中运行有关。

顺便说一句,如果我尝试在 while 循环之后调用 CryptStrm.Close(),我会收到一个异常。这意味着如果我尝试解密该文件,我会得到一个已在使用的文件错误!

尝试了所有常规方法,我在这里查看过类似的问题,任何帮助都会很棒。

谢谢

public void EncryptFile(String tempPath, String pathToSave)
{
try
{
FileStream InputFile = new FileStream(tempPath, FileMode.Open, FileAccess.Read);
FileStream OutputFile = new FileStream(pathToSave, FileMode.Create, FileAccess.Write);

RijndaelManaged RijCrypto = new RijndaelManaged();

//Key
byte[] Key = new byte[32] { ... };

//Initialisation Vector
byte[] IV = new byte[32] { ... };

RijCrypto.Padding = PaddingMode.None;
RijCrypto.KeySize = 256;
RijCrypto.BlockSize = 256;
RijCrypto.Key = Key;
RijCrypto.IV = IV;

ICryptoTransform Encryptor = RijCrypto.CreateEncryptor(Key, IV);

CryptoStream CryptStrm = new CryptoStream(OutputFile, Encryptor, CryptoStreamMode.Write);

int data;
while (-1 != (data = InputFile.ReadByte()))
{
CryptStrm.WriteByte((byte)data);
}
}
catch (Exception EncEx)
{
throw new Exception("Encoding Error: " + EncEx.Message);
}
}

编辑:

我假设我的问题出在加密上。我的解密可能是罪魁祸首

        public String DecryptFile(String encryptedFilePath)
{
FileStream InputFile = new FileStream(encryptedFilePath, FileMode.Open, FileAccess.Read);

RijndaelManaged RijCrypto = new RijndaelManaged();

//Key
byte[] Key = new byte[32] { ... };

//Initialisation Vector
byte[] IV = new byte[32] { ... };

RijCrypto.Padding = PaddingMode.None;
RijCrypto.KeySize = 256;
RijCrypto.BlockSize = 256;
RijCrypto.Key = Key;
RijCrypto.IV = IV;

ICryptoTransform Decryptor = RijCrypto.CreateDecryptor(Key, IV);

CryptoStream CryptStrm = new CryptoStream(InputFile, Decryptor, CryptoStreamMode.Read);

String OutputFilePath = Path.GetTempPath() + "myfile.name";
StreamWriter OutputFile = new StreamWriter(OutputFilePath);

OutputFile.Write(new StreamReader(CryptStrm).ReadToEnd());

CryptStrm.Close();
OutputFile.Close();

return OutputFilePath;
}

最佳答案

好吧,这里有几处地方出了问题。

<罢工>1。您设置的 IV 大小太大。如果 key 大小为 256,则 Rijndael 的 IV 为 16 字节。如果您尝试设置 key 的字节数多于该字节数,则会出现异常。 IV 大小应设置为 block 大小/8 see MSDN所以您的 IV 尺寸是正确的。

  1. 您的填充模式为无。除非您的数据恰好是正确的 block 大小,否则您将遇到问题。您可能想要选择一种填充模式。
  2. 您通常需要在加密时完成对加密流的写入后调用 FlushFInalBlock,以确保所有数据都写入流。

我正在发布一些使用内存流显示加密和解密的示例代码。

var tempData = "This is the text to encrypt. It's not much, but it's all there is.";

using (var rijCrypto = new RijndaelManaged())
{
byte[] encryptedData;
rijCrypto.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
rijCrypto.KeySize = 256;

using (var input = new MemoryStream(Encoding.Unicode.GetBytes(tempData)))
using (var output = new MemoryStream())
{
var encryptor = rijCrypto.CreateEncryptor();

using (var cryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write))
{
var buffer = new byte[1024];
var read = input.Read(buffer, 0, buffer.Length);
while (read > 0)
{
cryptStream.Write(buffer, 0, read);
read = input.Read(buffer, 0, buffer.Length);
}
cryptStream.FlushFinalBlock();
encryptedData = output.ToArray();
}
}

using (var input = new MemoryStream(encryptedData))
using (var output = new MemoryStream())
{
var decryptor = rijCrypto.CreateDecryptor();
using (var cryptStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read))
{
var buffer = new byte[1024];
var read = cryptStream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
output.Write(buffer, 0, read);
read = cryptStream.Read(buffer, 0, buffer.Length);
}
cryptStream.Flush();
var result = Encoding.Unicode.GetString(output.ToArray());
}
}
}

关于c# - 从加密流读取到流的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9724598/

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