visual studio——c#中加密+压缩代码的对立面是什么?

本文关键字:代码 对立面 是什么 压缩 studio 加密 visual | 更新日期: 2023-09-27 17:55:06

我已经编写了加密然后压缩数据的代码,但是我很难写出所有事情都反过来做的代码。

无论我如何组合代码,我都会得到各种错误。

我的问题很基本:

不同的代码位应该以什么顺序使用?

下面是我加密和压缩的示例代码:
MemoryStream mS = new MemoryStream();
CryptoStream cryptS = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write));
DeflateStream defS = new DeflateStream(cryptS, CompressionMode.Compress)
BinaryWriter bW = new BinaryWriter(defS))
//bw.Write("write a string for example");
bW.Close();

现在,在解密例程中,是否应该在DeflateStream之后使用CryptoStream来正确地向后跟踪加密例程?还是应该在CryptoStream之后使用DeflateStream ?

就像下面的例子:

MemoryStream mS = new MemoryStream();
DeflateStream defS = new DeflateStream(ms, CompressionMode.Compress)
CryptoStream cryptS = new CryptoStream(defS, des.CreateEncryptor(), CryptoStreamMode.Write));
BinaryWriter bW = new BinaryWriter(cryptS))
//bw.Write("write a string for example");
bW.Close();

我得到的错误从Unknown block type. Stream might be corrupted.Bad data不等。

编辑:

这是我的加密和解密程序。我得到错误Unknown block type. Stream might be corrupted .

加密程序:

using (MemoryStream mS = new MemoryStream())
            {
                using (CryptoStream cryptS = new CryptoStream(mS, dES.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    using (DeflateStream defS = new DeflateStream(cryptS, CompressionMode.Compress))
                    {
                        using (BinaryWriter bW = new BinaryWriter(defS))
                        {
                            DateTime dT = new DateTime().Now;
                            bW.Write(dt.Ticks);
                        }
                    }
                }
            }

解密例程(假设mS中填充了加密的数据):

using (MemoryStream mS = new MemoryStream())
            {
                using (DeflateStream defS = new DeflateStream(mS, CompressionMode.Decompress))
                {
                    using (CryptoStream cryptS = new CryptoStream(defS, dES.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        using (BinaryReader bR = new BinaryReader(cryptS))
                        {
                            DateTime dT = new DateTime((long)bR.ReadInt64());
                        }
                    }
                }
            }
我得到错误Failed to construct a huffman tree using the length array. The stream might be corrupted.

错误可能是什么?

visual studio——c#中加密+压缩代码的对立面是什么?

数据被压缩,然后加密,所以你需要做的第一件事是解密它,然后你可以解压缩它。所以,是的,它是相反的……但不明显的是,数据流也是反向的,因此将数据流链接在一起的顺序保持不变。

如果你把你的流使用包装在using块中,以确保它们被正确刷新和关闭,这也会很有帮助。

下面的代码应该可以工作了:

using (var des = new AesCryptoServiceProvider())
{
    byte[] result;
    using (var ms = new MemoryStream())
    {
        using (var encryptor = des.CreateEncryptor())
        using (var cryptS = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
        using (var defS = new DeflateStream(cryptS, CompressionMode.Compress))
        using (var bw = new BinaryWriter(defS))
        {
            bw.Write("Hello, World.");
        }
        result = ms.ToArray();
    }            
    using (var ms = new MemoryStream(result))            
    using (var decryptor = des.CreateDecryptor())
    using (var cryptS2 = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
    using (var defS = new DeflateStream(cryptS2, CompressionMode.Decompress))
    using (var br = new BinaryReader(defS))
    {
        var x = br.ReadString();
    }
}