简体   繁体   English

RijndaelManaged-用C#加密/解密流

[英]RijndaelManaged - Encrypt/Decrypt a Stream in C#

I am trying to encrypt and decrypt a stream (a PDF document at base), but I am having issues with this. 我正在尝试加密和解密流(基本为PDF文档),但是与此有关。 When I try to open the document after decryption and download, I get the error Failed to load the PDF document . 解密并下载后尝试打开文档时,出现错误无法加载PDF文档

Do you know why this might be happening? 您知道为什么会这样吗?

Here is the code for encryption: 这是加密代码:

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;
                }
            }
        }
    }

and decryption: 和解密:

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;
                }
            }
        }
    }

Thanks in advance. 提前致谢。

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;
}

and in the decryptor, get rid of all the StreamReader/StreamWriter stuff. 在解密器中,删除所有StreamReader / StreamWriter的内容。 A PDF file is compressed, ie binary. PDF文件已压缩,即二进制。 But this is after the decryption so it can't be your error. 但这是在解密之后执行的,因此不会是您的错误。

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 ;
    }
}

One issue is, you are likely encrypting excess bytes at the end of your stream, you need to work out how many bytes are read or use 一个问题是,您可能在流的末尾加密了多余的字节,因此需要确定要读取或使用多少个字节

Stream.CopyTo Method Stream.CopyTo方法

Reads the bytes from the current stream and writes them to another stream. 从当前流中读取字节,并将其写入另一个流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM