文件解密后无法解压
Unable to decompress file after decrypt it
我正在压缩然后加密一个文件。这没问题。当我尝试解密文件时出现问题,最后,我尝试解压缩解密的文件。在那一刻,我在 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)`
为了压缩我正在使用的文件夹
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);
如果我改变整个过程,首先我加密文件,然后压缩它,我可以解压它,最后解密它没有问题。可能是 encryption/decryption 阶段的问题(可能与编码、字节或类似...有关)?提前致谢。
StreamWriter
您正在写文字。你应该写字节。转换不是无损的。使用 FileStream
.
我正在压缩然后加密一个文件。这没问题。当我尝试解密文件时出现问题,最后,我尝试解压缩解密的文件。在那一刻,我在 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)`
为了压缩我正在使用的文件夹
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);
如果我改变整个过程,首先我加密文件,然后压缩它,我可以解压它,最后解密它没有问题。可能是 encryption/decryption 阶段的问题(可能与编码、字节或类似...有关)?提前致谢。
StreamWriter
您正在写文字。你应该写字节。转换不是无损的。使用 FileStream
.