与C#中的加密+压缩代码相反的是什么?

时间:2016-07-04 15:36:49

标签: c# visual-studio encryption compression

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

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

我的问题非常基本:

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

以下是我的加密和压缩示例代码:

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();

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

如以下示例所示:

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.

可能是什么错误?

1 个答案:

答案 0 :(得分:1)

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

如果您将流使用包装在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();
    }
}

请参阅this fiddle了解演示。