从cryptostream读取到流的末尾

时间:2012-03-15 17:03:16

标签: c# encryption cryptography aes rijndaelmanaged

我在使用下面的代码时遇到了一些问题。我在临时位置有一个需要加密的文件,这个函数会加密那些存储在“pathToSave”位置的数据。

在检查时似乎没有正确处理整个文件 - 我的输出中缺少一些内容,我怀疑它与while循环没有在整个流中运行有关。

顺便说一句,如果我尝试在while循环后调用CryptStrm.Close(),我会收到异常。这意味着如果我尝试解密文件,我会得到一个已经在使用的文件错误!

尝试了所有平常和我在这里看到类似的问题,任何帮助都会很棒。

由于

public void EncryptFile(String tempPath, String pathToSave)
    {
        try
        {
            FileStream InputFile = new FileStream(tempPath, FileMode.Open, FileAccess.Read);
            FileStream OutputFile = new FileStream(pathToSave, FileMode.Create, FileAccess.Write);

            RijndaelManaged RijCrypto = new RijndaelManaged();

            //Key
            byte[] Key = new byte[32] { ... };

            //Initialisation Vector
            byte[] IV = new byte[32] { ... };

            RijCrypto.Padding = PaddingMode.None;
            RijCrypto.KeySize = 256;
            RijCrypto.BlockSize = 256;
            RijCrypto.Key = Key;
            RijCrypto.IV = IV;

            ICryptoTransform Encryptor = RijCrypto.CreateEncryptor(Key, IV);

            CryptoStream CryptStrm = new CryptoStream(OutputFile, Encryptor, CryptoStreamMode.Write);

            int data;
            while (-1 != (data = InputFile.ReadByte()))
            {
                CryptStrm.WriteByte((byte)data);
            }
        }
        catch (Exception EncEx)
        {
            throw new Exception("Encoding Error: " + EncEx.Message);
        }
    }

编辑:

我假设我的问题在于加密。我的解密可能是罪魁祸首

        public String DecryptFile(String encryptedFilePath)
    {
        FileStream InputFile = new FileStream(encryptedFilePath, FileMode.Open, FileAccess.Read);

        RijndaelManaged RijCrypto = new RijndaelManaged();

        //Key
        byte[] Key = new byte[32] { ... };

        //Initialisation Vector
        byte[] IV = new byte[32] { ... };

        RijCrypto.Padding = PaddingMode.None;
        RijCrypto.KeySize = 256;
        RijCrypto.BlockSize = 256;
        RijCrypto.Key = Key;
        RijCrypto.IV = IV;

        ICryptoTransform Decryptor = RijCrypto.CreateDecryptor(Key, IV);

        CryptoStream CryptStrm = new CryptoStream(InputFile, Decryptor, CryptoStreamMode.Read);

        String OutputFilePath = Path.GetTempPath() + "myfile.name";
        StreamWriter OutputFile = new StreamWriter(OutputFilePath);

        OutputFile.Write(new StreamReader(CryptStrm).ReadToEnd());

        CryptStrm.Close();
        OutputFile.Close();

        return OutputFilePath;
    }

3 个答案:

答案 0 :(得分:14)

嗯,这里有几个问题。

<击> 1。你设置IV的大小太大了。如果密钥大小为256,则Rijndael的IV为16字节。如果您尝试设置的字符数多于字节数,则会出现异常。 IV大小应设置为Block size / 8 see MSDN,因此您的IV大小是正确的。

  1. 您的填充模式为None。除非您的数据完全是正确的块大小,否则您将遇到问题。您可能想要选择填充模式。
  2. 在加密时,通常需要在写入加密流时调用FlushFInalBlock,以确保所有数据都写入流中。
  3. 我发布了一些使用内存流的示例代码来显示加密和解密。

    var tempData = "This is the text to encrypt. It's not much, but it's all there is.";
    
    using (var rijCrypto = new RijndaelManaged())
    {
        byte[] encryptedData;
        rijCrypto.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
        rijCrypto.KeySize = 256;
    
        using (var input = new MemoryStream(Encoding.Unicode.GetBytes(tempData)))
        using (var output = new MemoryStream())
        {
            var encryptor = rijCrypto.CreateEncryptor();
    
            using (var cryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write))
            {
                var buffer = new byte[1024];
                var read = input.Read(buffer, 0, buffer.Length);
                while (read > 0)
                {
                    cryptStream.Write(buffer, 0, read);
                    read = input.Read(buffer, 0, buffer.Length);
                }
                cryptStream.FlushFinalBlock();
                encryptedData = output.ToArray();
            }
        }
    
        using (var input = new MemoryStream(encryptedData))
        using (var output = new MemoryStream())
        {
            var decryptor = rijCrypto.CreateDecryptor();
            using (var cryptStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read))
            {
                var buffer = new byte[1024];
                var read = cryptStream.Read(buffer, 0, buffer.Length);
                while (read > 0)
                {
                     output.Write(buffer, 0, read);
                     read = cryptStream.Read(buffer, 0, buffer.Length);
                }
                cryptStream.Flush();
                var result = Encoding.Unicode.GetString(output.ToArray());
            }
        }
    }
    

答案 1 :(得分:2)

读取CryptoStream的另一种方法是使用CopyTo方法:

@Service

答案 2 :(得分:0)

必须首先指定KeySize。这看起来像一个错误可行

            using (var aes = new AesManaged())
            {
                aes.KeySize = 256;
                aes.Mode = CipherMode.CBC;
                aes.IV = iv;
                aes.Key = passwordHash;
                aes.Padding = PaddingMode.PKCS7;

但这不是

            using (var aes = new AesManaged())
            {
                aes.Mode = CipherMode.CBC;
                aes.IV = iv;
                aes.Key = passwordHash;
                aes.Padding = PaddingMode.PKCS7;
                aes.KeySize = 256;