RijndaelManaged-用C#加密/解密流

时间:2018-11-05 07:49:22

标签: c# pdf encryption stream rijndaelmanaged

我正在尝试加密和解密流(基本为PDF文档),但是与此有关。解密并下载后尝试打开文档时,出现错误无法加载PDF文档

你知道为什么会这样吗?

以下是加密代码:

public EncryptResult EncryptStream(Stream dataStream, bool reuseIV = false)
    {
        RijndaelManaged crypto = new RijndaelManaged();
        crypto.Key = _key;

        if (!reuseIV || _iv == null)
        {
            // make a copy of the current IV
            _iv = crypto.IV;
        }
        else
        {
            // reuse the previous IV
            crypto.IV = _iv;
        }

        var result = new EncryptResult() { IV = crypto.IV };

        using (var encryptor = crypto.CreateEncryptor())
        {
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
               {
                    var byteArrayInput = new byte[dataStream.Length];
                    dataStream.Read(byteArrayInput, 0, byteArrayInput.Length);
                    csEncrypt.Write(byteArrayInput, 0, byteArrayInput.Length);
                    dataStream.Close();

                    result.Cipher = msEncrypt.ToArray();

                    msEncrypt.Flush();
                    msEncrypt.Position = 0;

                    return result;
                }
            }
        }
    }

和解密:

public Stream DecryptStream(byte[] cipher, byte[] iv)
    {
        RijndaelManaged crypto = new RijndaelManaged();
        crypto.Key = _key;
        crypto.IV = iv;

        crypto.Padding = PaddingMode.Zeros;

        using (var decryptor = crypto.CreateDecryptor())
        {
            using (MemoryStream msDecrypt = new MemoryStream(cipher))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    var sOutputFilename = new MemoryStream();
                    var fsDecrypted = new StreamWriter(sOutputFilename);
                    fsDecrypted.Write(new StreamReader(csDecrypt).ReadToEnd());

                    sOutputFilename.Position = 0;
                    return sOutputFilename;
                }
            }
        }
    }

谢谢。

2 个答案:

答案 0 :(得分:1)

using (MemoryStream msEncrypt = new MemoryStream())
{
   using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
   {
        //var byteArrayInput = new byte[dataStream.Length];
        //dataStream.Read(byteArrayInput, 0, byteArrayInput.Length);
        //csEncrypt.Write(byteArrayInput, 0, byteArrayInput.Length);
        dataStream.CopyTo(csEncrypt);
        dataStream.Close();

        //result.Cipher = msEncrypt.ToArray();  // not here - not flushed yet
        //msEncrypt.Flush();                    // don't need this
        //msEncrypt.Position = 0;            
    }
    result.Cipher = msEncrypt.ToArray();  
    return result;
}

并在解密器中,删除所有StreamReader / StreamWriter内容。 PDF文件已压缩,即二进制。但这是在解密之后执行的,因此不会是您的错误。

using (var decryptor = crypto.CreateDecryptor())
{
    using (MemoryStream msDecrypt = new MemoryStream(cipher))
    {
       var outputStream = new MemoryStream();

        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
        {               
            csDecrypt.CopyTo(outputStream );
        }
        outputStream .Position = 0;
        return outputStream ;
    }
}

答案 1 :(得分:0)

一个问题是,您可能在流的末尾加密了多余的字节,因此需要确定要读取或使用多少个字节

Stream.CopyTo Method

  

从当前流中读取字节并将其写入另一个   流。