我压缩然后加密文件。没问题。当我尝试解密文件时出现问题,最后,我尝试解压缩解密的文件。在那一刻,我在ReadEndOfCentralDirectory
中得到了一个例外:(更新)
Offset to Central Directory cannot be held in an Int64.
en System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory()
en System.IO.Compression.ZipArchive.Init(Stream stream, ZipArchiveMode mode, Boolean leaveOpen)
en System.IO.Compression.ZipArchive..ctor(Stream stream, ZipArchiveMode mode, Boolean leaveOpen, Encoding entryNameEncoding)
en System.IO.Compression.ZipFile.Open(String archiveFileName, ZipArchiveMode mode, Encoding entryNameEncoding)
en System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName, Encoding entryNameEncoding)
en System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName)`
对于我使用此
的zip文件夹 ZipFile.CreateFromDirectory(sourcePath, pathZipFile, CompressionLevel.Fastest, false);
用于加密:
RijndaelManaged key = new RijndaelManaged();
key.KeySize = 256;
key.BlockSize = 256;
key.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
key.Key = Convert.FromBase64String(aes);
key.IV = Convert.FromBase64String(aes);
ICryptoTransform encryptor = key.CreateEncryptor();
using (FileStream InputFile = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
using (FileStream OutputFile = new FileStream(targetPath, FileMode.Create, FileAccess.Write))
{
using (CryptoStream csEncrypt = new CryptoStream(OutputFile, encryptor, CryptoStreamMode.Write))
{
long total = InputFile.Length;
var buffer = new byte[total];
var read = InputFile.Read(buffer, 0, buffer.Length);
csEncrypt.Write(buffer, 0, read);
}
}
解密:
RijndaelManaged key = new RijndaelManaged();
key.KeySize = 256;
key.BlockSize = 256;
key.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
key.Key = Convert.FromBase64String(aes);
key.IV = Convert.FromBase64String(aes);
ICryptoTransform cryptor = key.CreateDecryptor();
using (FileStream InputFile = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
{
CryptoStream csEncrypt = new CryptoStream(InputFile, cryptor, CryptoStreamMode.Read);
StreamWriter fsDecrypted = new StreamWriter(targetPath);
fsDecrypted.Write(new StreamReader(csEncrypt).ReadToEnd());
fsDecrypted.Close();
}
解压缩:
ZipFile.ExtractToDirectory(zipPath, extractPath);
如果我改变整个过程,首先我加密文件,然后压缩它,我可以解压缩它,最后解密它没有问题。可能是加密/解密阶段的问题(可能与编码,字节或类似...有关)?提前谢谢。
答案 0 :(得分:1)
StreamWriter
您正在撰写文字。你应该写字节。转换不是无损的。使用FileStream
。