简体   繁体   English

错误AES-标准SymmetricAlgorithm,“填充无效且无法删除”

[英]Error AES - Standard SymmetricAlgorithm, “Padding is invalid and cannot be removed”

I am using AES - Standard SymmetricAlgorithm, for Encrypting and Decrypting string. 我正在使用AES - Standard SymmetricAlgorithm,来加密和解密字符串。 String is encrypting successfully but when it comes to decrypt the compiler gives me the exception ie 字符串正在成功加密,但是在解密时,编译器给了我一个例外,即

“Padding is invalid and cannot be removed”. “填充无效,无法删除”。

I have created a demo console application for testing, please have a look on below code. 我已经创建了一个用于测试的演示控制台应用程序,请查看下面的代码。

The Main Method: 主要方法:

static void Main(string[] args)
{
    var content = "5466160057107706";
    var key = "E546C8DF278CD5931069B522E695D4F2";

    var encrypted = EncryptString(content, key);
    Console.WriteLine(encrypted);
    var decrypted = DecryptString(encrypted, key);
    Console.WriteLine(decrypted);
    Console.ReadLine();
}

Method added for Encryption: 添加的加密方法:

public static string EncryptString(string text, string keyString)
{
    var key = Encoding.UTF8.GetBytes(keyString);
    using (var aesAlg = Aes.Create())
    {
        using (var encryptor = aesAlg.CreateEncryptor(key, aesAlg.IV))
        {
            using (var msEncrypt = new MemoryStream())
            {
                using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                using (var swEncrypt = new StreamWriter(csEncrypt))
                {
                    swEncrypt.Write(text);
                }
                var iv = aesAlg.IV;
                var decryptedContent = msEncrypt.ToArray();
                var result = new byte[iv.Length + decryptedContent.Length];
                Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
                Buffer.BlockCopy(decryptedContent, 0, result, iv.Length, decryptedContent.Length);
                return Convert.ToBase64String(result);
            }
        }
    }
}

Method added for Decryption: 添加的解密方法:

public static string DecryptString(string cipherText, string keyString)
{
    var fullCipher = Convert.FromBase64String(cipherText);
    var iv = new byte[16];
    var cipher = new byte[16];
    Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
    Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, iv.Length);
    var key = Encoding.UTF8.GetBytes(keyString);
    using (var aesAlg = Aes.Create())
    {
        using (var decryptor = aesAlg.CreateDecryptor(key, iv))
        {
            string result;
            using (var msDecrypt = new MemoryStream(cipher))
            {
                using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (var srDecrypt = new StreamReader(csDecrypt))
                    {
                        result = srDecrypt.ReadToEnd();
                    }
                }
            }
            return result;
        }
    }
}

Here is the Error Snap which occurs in the DecryptString() method: 这是在DecryptString()方法中发生的错误捕捉:

在此处输入图片说明

Any advice, how to solve this issue? 有什么建议,如何解决这个问题?

Clue: var cipher = new byte[16]; 提示: var cipher = new byte[16];

Why are you assuming your cipher would be ONLY 16 bytes ? 为什么要假设密码只有16个字节? What if it is more than this ? 如果还不止如此呢?

In fact, if I run this program and debug, I see that your cipher is 32 bytes. 实际上,如果我运行该程序并进行调试,则会看到您的密码为32字节。

So, the following 2 line changes makes it work: 因此,以下两行更改使其起作用:

var cipher = new byte[32];

Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, cipher.Length);

In any case, you would need to dynamically determine your size of cipher. 无论如何,您都需要动态确定密码的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM