Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET C中使用AES加密时,要解密的数据长度无效#_C#_Asp.net_Cryptography - Fatal编程技术网

C# 在ASP.NET C中使用AES加密时,要解密的数据长度无效#

C# 在ASP.NET C中使用AES加密时,要解密的数据长度无效#,c#,asp.net,cryptography,C#,Asp.net,Cryptography,嗨,伙计们,请帮我解决这个问题,我一直遇到这个错误: 要解密的数据长度无效 我做错了什么 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Security.Cryptography; using System.IO; namespace inChargeAES { public class inChargeCrypto : IinCharg

嗨,伙计们,请帮我解决这个问题,我一直遇到这个错误:

要解密的数据长度无效

我做错了什么

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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Security.Cryptography;
使用System.IO;
名称空间inChargeAES
{
ChargeCrypto中的公共类:IinChargeAES
{
公共inChargeCrypto(){}
公共字符串inChargeEncrypt(字符串明文,字节[]加密密钥,字节[]初始化向量)
{

如果(plaintext==null | | plaintext.Length您已经注释掉了encrypt函数的填充模式

当我在decrypt函数上注释它时,一切都按预期工作


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

加密和解密时,填充值必须相同。您的方法对我来说很好,在加密或解密时没有填充。我不理解您所说的“假设您没有错误地将base64字符串转换回字节[]”的意思您的加密方法使用
return Convert.ToBase64String(encryptedText);
以字符串形式返回,但您的解密方法需要一个
byte[]
数组作为输入。如何将加密字符串转换回
byte[]