如何解码和解压缩文本?

时间:2018-04-20 10:03:04

标签: c# .net stream

我想创建大文本,挤压它,编码并保存到文件中。然后我想解密并提取该文本并将其保存到简单的文本文件中。

我对第一步没有问题(创建加密和压缩文件),但我有第二步 - 获取信息(请阅读我的代码中的注释)。我做错了什么?

using System;
using System.IO;
using System.Text;
using System.IO.Compression;
using System.Security.Cryptography;

static class Program
{
    static void Main()
    {
        Console.WriteLine("Create encrypted archive...");
        var fileName = @"data.xgzip";

        byte[] key = null;
        byte[] iv = null;

        // I want to create big text, squeeze it, encode and save into file:
        // 1. Create big text.
        // 2. Squeeze text.
        // 3. Encode squeezed text.
        // 4. Write result to file.

        using (var stream = new FileStream(fileName, FileMode.Create, 
            FileAccess.Write, FileShare.None, 0x1000, FileOptions.None))
        {
            using (var rijn = Rijndael.Create())
            {
                key = rijn.Key;
                iv = rijn.IV;

                var encryptor = rijn.CreateEncryptor(key, iv);

                using (var encStream = new CryptoStream(stream, encryptor, 
                    CryptoStreamMode.Write))
                {
                    using (var zip = new DeflateStream(encStream, 
                        CompressionLevel.Optimal))
                    {
                        using (var writer = new StreamWriter(zip, new UTF8Encoding(
                        encoderShouldEmitUTF8Identifier: false, 
                        throwOnInvalidBytes: true), 0x1000, true))
                        {
                            var text = "One, two, three, four, five...";
                            for (int n = 0; n < 1000; n++)
                            {
                                writer.WriteLine(text);
                            }
                        }
                        Console.WriteLine("zip.Length = {0}", stream.Length);
                    }
                }
            }
        }

        var fi = new FileInfo(fileName);
        Console.WriteLine("File size: {0}", fi.Length);

        // Now I want to decipher and extract my file into simple text file.
        // 1. Decode squeezed data.
        // 2. Unpack decoded data.
        // 4. Write result to text file.

        Console.WriteLine("Extract encrypted archive...");
        var fileName2 = @"data.txt";

        using (var stream = new FileStream(fileName, FileMode.Open, 
            FileAccess.Read, FileShare.None, 0x1000, FileOptions.None))
        {
            using (var rijn = Rijndael.Create())
            {
                var encryptor = rijn.CreateEncryptor(key, iv);

                using (var cryptoStream = new CryptoStream(stream, encryptor, 
                    CryptoStreamMode.Read))
                {
                    using (var zip = new DeflateStream(cryptoStream, 
                        CompressionMode.Decompress))
                    {
                        using (var reader = new StreamReader(zip, 
                            new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, 
                            throwOnInvalidBytes: true)))
                        {
                            // System.IO.InvalidDataException: 
                            // "The archive entry was compressed using an unsupported 
                            // compression method."
                            var text = reader.ReadToEnd();

                            // Write the result into the simple text file.
                            using (var stream2 = new FileStream(fileName2, 
                                FileMode.Create, FileAccess.Write, FileShare.None,
                                0x1000, FileOptions.None))
                            {
                                using (var writer = new StreamWriter(stream2, 
                                    new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, 
                                    throwOnInvalidBytes: true), 0x1000, true))
                                {
                                    writer.Write(text);
                                }
                                Console.WriteLine("stream.Length = {0}", stream.Length);
                            }
                        }
                    }
                }
            }
        }

        var fi2 = new FileInfo(fileName2);
        Console.WriteLine("File size: {0}", fi2.Length);

        Console.WriteLine("Press any key for exit...");
        Console.ReadKey();
    }
}

1 个答案:

答案 0 :(得分:1)

在解码部分,您仍然使用加密器来解密内容,正确的方法应该是使用解密器。

更新解码部分中的以下行

var encryptor = rijn.CreateEncryptor(key, iv);

using (var cryptoStream = new CryptoStream(stream, encryptor, 
      CryptoStreamMode.Read))

var decryptor = rijn.CreateDecryptor(key, iv);

using (var cryptoStream = new CryptoStream(stream, decryptor, 
      CryptoStreamMode.Read))

然后你的代码应该工作