0

大家好,请帮我解决这个问题,我一直收到这个错误:

要解密的数据长度无效。

我究竟做错了什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.IO;

namespace inChargeAES
{
    public class inChargeCrypto : IinChargeAES
    {
        public inChargeCrypto() {}

        public String inChargeEncrypt(String plaintext, byte[] encryptionKey, byte[] initializationVector)
        {
            if (plaintext == null || plaintext.Length <= 0)
            {
                throw new ArgumentNullException("plaintext");
            }
            if(encryptionKey == null || encryptionKey.Length <= 0){
                throw new ArgumentNullException("encryptionKey");
            }
            if(initializationVector == null || initializationVector.Length <= 0){
                throw new ArgumentNullException("initializationVector");
            }
            byte[] encryptedText;
            using(RijndaelManaged rjManage = new RijndaelManaged())
            {
                rjManage.Key = encryptionKey;
                rjManage.IV = initializationVector;
                rjManage.Mode = CipherMode.CBC;
                //rjManage.Padding = PaddingMode.None;

                ICryptoTransform iTransformer = rjManage.CreateEncryptor(rjManage.Key, rjManage.IV);
                using(MemoryStream memStream = new MemoryStream())
                {
                    using(CryptoStream cEncryptStream = new CryptoStream(memStream, iTransformer, CryptoStreamMode.Write))
                    {
                        using(StreamWriter encryptStreamWriter = new StreamWriter(cEncryptStream))
                        {
                            encryptStreamWriter.Write(plaintext);
                        }
                        encryptedText = memStream.ToArray();
                    }
                }
            }
            return Convert.ToBase64String(encryptedText);
        }

        public String inChargeDecrypt(byte[] cipher, byte[] encryptionKey, byte[] initializationVector)
        {
            if (cipher == null || cipher.Length <= 0){
                throw new ArgumentNullException("cipher");
            }
            if (encryptionKey == null || encryptionKey.Length <= 0){
                throw new ArgumentNullException("encryptionKey");
            }
            if (initializationVector == null || initializationVector.Length <= 0){
                throw new ArgumentNullException("initializationVector");
            }

            String decryptedText = null;
            using (RijndaelManaged rijManage = new RijndaelManaged())
            {
                rijManage.Key = encryptionKey;
                rijManage.IV = initializationVector;
                rijManage.Mode = CipherMode.CBC;
                rijManage.Padding = PaddingMode.None;

                ICryptoTransform iTranformation = rijManage.CreateDecryptor(rijManage.Key, rijManage.IV);
                using(MemoryStream memStream = new MemoryStream(cipher))
                {
                    using(CryptoStream cDecryptorStream = new CryptoStream(memStream, iTranformation, CryptoStreamMode.Read))
                    {
                        using (StreamReader decryptReader = new StreamReader(cDecryptorStream))
                        {
                            decryptedText = decryptReader.ReadToEnd(); //Exception Is Thrown
                        }
                        //memStream.Read(cipher, 0, cipher.Length);
                    }
                }
            }
            return decryptedText;
        }
    }
}
4

1 回答 1

0

您已经在 encrypt 函数上注释掉了填充模式。

当我在解密功能上评论它时,一切都按预期工作。

假设您没有错误地将 base64 字符串转换回 byte[]

于 2013-10-31T16:36:06.040 回答