本文介绍了要解密的数据长度无效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我写了一个加密和解密数据的代码,但是我一直遇到这个令人沮丧的错误

要解密的数据长度无效。

Hi guys, i was wrote a code to encrypt and decrypt data, but i keep getting this frustrating error
Length of the data to decrypt is invalid.

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

namespace inChargeAES
{
    /// <summary>
    /// Summary description for inChargeCrypto
    /// </summary>
    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;
        }

    }
}

推荐答案


这篇关于要解密的数据长度无效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 10:26