簡體   English   中英

在ASP.NET C#中使用AES加密時,要解密的數據長度無效

[英]Length of the data to decrypt is invalid when using AES encryption in ASP.NET C#

大家好,請幫我解決這個問題,我不斷收到此錯誤消息:

要解密的數據長度無效。

我究竟做錯了什么?

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

您已經注釋掉了加密功能的填充模式。

當我在解密功能上將其注釋掉時,一切都會按預期進行。

假設您沒有將base64字符串轉換回byte []的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM