rijndael解密文件"要解密的数据长度无效"

时间:2016-02-01 12:07:50

标签: c# encryption

我尝试为基于rijndael和CBC的文件构建一个简单的De / En-cryption类。加密就像一个魅力,但当我尝试读取数据时,它总是给我"要解密的数据长度无效"。关于Stackoverflow的大多数问题都得到了回答#34;你忘了刷新最后的块"或者在解密或加密时使用正确的填充,这主要是解决方案,但不是在我的情况下。 (我希望)。

    public int BlockSize { get; set; } = 128;
    public int IVSize { get; set; } = 256;

public Stream Decrypt(KeyPair pair, string file)
    {
        if (pair?.IV == null || pair.Key == null)
        {
            throw new ArgumentException("Key missing");
        }

        var returningStream = new MemoryStream();
        using (var myRij = new RijndaelManaged())
        {
            myRij.IV = pair.IV;
            myRij.Key = pair.Key;
            myRij.Mode = CipherMode.CBC;
            myRij.Padding = PaddingMode.PKCS7;
            var decrypt = myRij.CreateDecryptor();

            using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
            using (var cryptoStream = new CryptoStream(fileStream, decrypt, CryptoStreamMode.Read))
            {
                var buffer = new byte[64];
                var read = cryptoStream.Read(buffer, 0, buffer.Length);
                while (read > 0)
                {
                    returningStream.Write(buffer, 0, read);
                    // I think the last Iteration is failing... 
                    // Also cryptoStream.CopyTo(returningStream) throws the exception
                    read = cryptoStream.Read(buffer, 0, buffer.Length);
                }
            }
        }

        returningStream.Position = 0;
        return returningStream;
    }

    public KeyPair Encrypt(string file, string outfile)
    {
        using (RijndaelManaged myRijndael = new RijndaelManaged())
        { 
            // Generate new IV and KEY
            myRijndael.KeySize = IVSize;
            myRijndael.BlockSize = BlockSize;
            myRijndael.Padding = PaddingMode.PKCS7;
            myRijndael.Mode = CipherMode.CBC;
            myRijndael.GenerateIV();
            myRijndael.GenerateKey();

            var encrypter = myRijndael.CreateEncryptor();

            // Create memoryspace for crypto
            using (var memory = new MemoryStream())
            {
                // Create cryptostream
                using (var cryptoStream = new CryptoStream(memory, encrypter, CryptoStreamMode.Write))
                {
                    // read file to encrypt
                    using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
                    {
                        // copy content to cryptostream
                        var buffer = new byte[64];
                        var read = fileStream.Read(buffer, 0, buffer.Length);
                        while (read > 0)
                        {
                            cryptoStream.Write(buffer, 0, read);
                            read = fileStream.Read(buffer, 0, buffer.Length);
                        }
                        cryptoStream.FlushFinalBlock();
                    }

                    using (var encryptedFile = new FileStream(outfile, FileMode.CreateNew, FileAccess.Write))
                    {
                        // Reset the stream position for reading
                        memory.Position = 0;

                        // read and copy content to file
                        memory.CopyTo(encryptedFile);
                    }                         
                }
            }

            return new KeyPair {IV = myRijndael.IV, Key = myRijndael.Key};
        }
    }

我优雅地接受我的代码的每个解决方案或优化。

0 个答案:

没有答案