在C#中的块中将CryptoStream编码为Base64字符串

时间:2016-12-04 20:27:00

标签: c# asp.net base64 chunks cryptostream

有一种方法( Version1 )对输入流进行编码,并且有一个功能Decrypt()可以成功解码编码数据。但是当输入数据很大时,可能会出现错误 OutOfMemory (在线" 字符串textEncrypted = Convert.ToBase64String(ms.ToArray())&# 34。)

版本1

    private static Stream EncryptRijndael1(byte[] key, byte[] iv, Stream plainText)
    {
        if (plainText == null)
            return null;

        byte[] bytesEncrypted;

        RijndaelManaged rjndl = RijndaelManagedWithConfig(key, iv);

        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, rjndl.CreateEncryptor(key, iv), CryptoStreamMode.Write))
            {
                byte[] buffer = new byte[16 * 1024];
                int readed;
                while ((readed = (plainText.Read(buffer, 0, buffer.Length))) > 0)
                {
                    cs.Write(buffer, 0, readed);
                }
            }
            string textEncrypted = Convert.ToBase64String(ms.ToArray());
            bytesEncrypted = Encoding.ASCII.GetBytes(textEncrypted);
        }
        return new MemoryStream(bytesEncrypted);
    }

所以我修改了方法来逐个部分地处理数组(块)。 这是版本2 。它会导致错误&#34;偏移量和长度必须引用字符串中的位置&#34; Convert.ToBase64String(ms.ToArray(),offset,read)< /强>

版本2

    private static Stream EncryptRijndael2(byte[] key, byte[] iv, Stream plainText)
    {
        if (plainText == null)
            return null;

        byte[] bytesEncrypted;

        RijndaelManaged rjndl = RijndaelManagedWithConfig(key, iv);

        string textEncrypted = String.Empty;

        using (MemoryStream ms = new MemoryStream())
        {
            using (CryptoStream cs = new CryptoStream(ms, rjndl.CreateEncryptor(key, iv), CryptoStreamMode.Write))
            {
                byte[] buffer = new byte[16 * 1024];
                int readed;
                int offset = 0;
                while ((readed = (plainText.Read(buffer, 0, buffer.Length))) > 0)
                {
                    cs.Write(buffer, 0, readed);
                    textEncrypted += Convert.ToBase64String(ms.ToArray(), offset, readed);
                    offset += readed;
                }
            }

            bytesEncrypted = Encoding.ASCII.GetBytes(textEncrypted);

        }

        return new MemoryStream(bytesEncrypted);
    }

然后我做了 Version3 。它的工作没有错误,但输出数据长度现在大于具有相同输入数据的 Version1 。 Decryt()函数抛出错误&#34;输入不是有效的Base-64字符串,因为它包含非基本64字符,两个以上的填充字符或填充字符中的非法字符。 #34;

版本3

    private static Stream EncryptRijndael3(byte[] key, byte[] iv, Stream plainText)
    {
        if (plainText == null)
            return null;

        byte[] bytesEncrypted;

        RijndaelManaged rjndl = RijndaelManagedWithConfig(key, iv);

        using (MemoryStream ms = new MemoryStream())
        {
            string textEncrypted = String.Empty;
            using (CryptoStream cs = new CryptoStream(ms, rjndl.CreateEncryptor(key, iv), CryptoStreamMode.Write))
            {
                byte[] buffer = new byte[16*1024];
                int readed;
                while ((readed = (plainText.Read(buffer, 0, buffer.Length))) > 0)
                {
                    cs.Write(buffer, 0, readed);
                }
             }


                byte[] buffer1 = new byte[16*1024];
                int readed1;
                using (MemoryStream ms1 = new MemoryStream(ms.ToArray()))
                {
                    while ((readed1 = (ms1.Read(buffer1, 0, buffer1.Length))) > 0)
                    {
                        if (readed1 < buffer1.Length)
                        {
                            var lastBuf = new List<Byte>();
                            for (int i = 0; i < readed1; i++)
                            {
                                lastBuf.Add(buffer1[i]);
                            }
                            textEncrypted += Convert.ToBase64String(lastBuf.ToArray());
                            continue;
                        }
                        textEncrypted += Convert.ToBase64String(buffer1);
                    }
                 }

            bytesEncrypted = Encoding.ASCII.GetBytes(textEncrypted);
        }

        return new MemoryStream(bytesEncrypted);
    }

我的RijndaelManaged

 private static RijndaelManaged RijndaelManagedWithConfig(byte[] key, byte[] iv)
    {
        RijndaelManaged rjndl = new RijndaelManaged();
        rjndl.KeySize = 256;
        rjndl.BlockSize = 128;

        rjndl.Key = key;
        rjndl.IV = iv;

        rjndl.Mode = CipherMode.CBC;
        rjndl.Padding = PaddingMode.PKCS7;

        return rjndl;
    }

请帮我摆脱错误或告诉我如何使版本1进程转换.ToBase64String数据部分

1 个答案:

答案 0 :(得分:0)

我已经做出了决定

         private static Stream EncryptRijndael(byte[] key, byte[] iv, Stream plainText)
    {
        if (plainText == null)
            return null;

        byte[] buffer = new byte[5120 * 1024];

        RijndaelManaged rjndl = RijndaelManagedWithConfig(key, iv);

        using (var memoryStream = new MemoryStream())
        {
            int readedBytes;
            using (var cs = new CryptoStream(memoryStream, rjndl.CreateEncryptor(key, iv), CryptoStreamMode.Write))
            {
                while ((readedBytes = (plainText.Read(buffer, 0, buffer.Length))) > 0)
                {
                    cs.Write(buffer, 0, readedBytes);
                }
            }


            using (var cryptoMemoryStream = new MemoryStream(memoryStream.ToArray()))
            {
                using (var base64MemoryStream = new MemoryStream())
                {
                    using (ICryptoTransform transform = new ToBase64Transform())
                    {
                        using (var cryptStream = new CryptoStream(base64MemoryStream, transform, CryptoStreamMode.Write))
                        {
                            while ((readedBytes = cryptoMemoryStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                cryptStream.Write(buffer, 0, readedBytes);
                            }
                            cryptStream.FlushFinalBlock();
                        }

                        return new MemoryStream(base64MemoryStream.ToArray());
                    }
                }
            }
        }
    }