本文介绍了"填充是无效的,不能删除" -Whats不对的code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次运行这个和加密,输出是可变的,当我试图解密我得到的填充是无效的不能删除。一直在争取与此一两天了,我很茫然。

 私有静态字符串strIV =abcdefghijklmnmo; //初始化向量。
    私人静态字符串strKey =abcdefghijklmnmoabcdefghijklmnmo; //用于文本进行加密的密钥。

    公共静态字符串解密(字符串TextToDecrypt)
    {
        返回解密(TextToDecrypt);
    }

    私人静态字符串加密器(串TextToEncrypt)
    {
        //开启明文到字节数组。
        byte []的PlainTextBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToEncrypt);

        //设置了AES providor我们的目的。
        AesCryptoServiceProvider aesProvider =新AesCryptoServiceProvider();
        aesProvider.Key = System.Text.Encoding.ASCII.GetBytes(strKey);
        aesProvider.IV = System.Text.Encoding.ASCII.GetBytes(strIV);
        aesProvider.BlockSize = 128;
        aesProvider.KeySize = 256;
        aesProvider.Padding = PaddingMode.PKCS7;
        aesProvider.Mode = CipherMode.CBC;

        ICryptoTransform的cryptoTransform = aesProvider.CreateEncryptor(aesProvider.Key,aesProvider.IV);
        byte []的EncryptedBytes = cryptoTransform.TransformFinalBlock(PlainTextBytes,0,PlainTextBytes.Length);
        返回Convert.ToBase64String(EncryptedBytes);
    }

    私人静态字符串解密(字符串TextToDecrypt)
    {
        byte []的EncryptedBytes = Convert.FromBase64String(TextToDecrypt);

        //设置的AES提供用于解密。
        AesCryptoServiceProvider aesProvider =新AesCryptoServiceProvider();
        aesProvider.Key = System.Text.Encoding.ASCII.GetBytes(strKey);
        aesProvider.IV = System.Text.Encoding.ASCII.GetBytes(strIV);
        aesProvider.BlockSize = 128;
        aesProvider.KeySize = 256;
        aesProvider.Padding = PaddingMode.PKCS7;
        aesProvider.Mode = CipherMode.CBC;

        ICryptoTransform的cryptoTransform = aesProvider.CreateDecryptor(aesProvider.Key,aesProvider.IV);
        byte []的D​​ecryptedBytes = cryptoTransform.TransformFinalBlock(EncryptedBytes,0,EncryptedBytes.Length);
        返回System.Text.Encoding.ASCII.GetString(DecryptedBytes);
    }
}
 

解决方案

您需要设置块大小密钥长度设置在 IV 。另外,你或许应该产生一个随机IV每封邮件,请注意 ICryptoTransform的工具的IDisposable 因此这些对象应释放

Every time I run this and encrypt, the output is variable, and when I attempt to decryptI get "Padding is invalid and cannot be removed." Been fighting with this for a day or two now and I am at a loss.

    private static string strIV = "abcdefghijklmnmo"; //The initialization vector.
    private static string strKey = "abcdefghijklmnmoabcdefghijklmnmo"; //The key used to encrypt the text.

    public static string Decrypt(string TextToDecrypt)
    {
        return Decryptor(TextToDecrypt);
    }

    private static string Encryptor(string TextToEncrypt)
    {
        //Turn the plaintext into a byte array.
        byte[] PlainTextBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToEncrypt);

        //Setup the AES providor for our purposes.
        AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider();
        aesProvider.Key = System.Text.Encoding.ASCII.GetBytes(strKey);
        aesProvider.IV = System.Text.Encoding.ASCII.GetBytes(strIV);
        aesProvider.BlockSize = 128;
        aesProvider.KeySize = 256;
        aesProvider.Padding = PaddingMode.PKCS7;
        aesProvider.Mode = CipherMode.CBC;

        ICryptoTransform cryptoTransform = aesProvider.CreateEncryptor(aesProvider.Key, aesProvider.IV);
        byte[] EncryptedBytes = cryptoTransform.TransformFinalBlock(PlainTextBytes, 0, PlainTextBytes.Length);
        return Convert.ToBase64String(EncryptedBytes);
    }

    private static string Decryptor(string TextToDecrypt)
    {
        byte[] EncryptedBytes = Convert.FromBase64String(TextToDecrypt);

        //Setup the AES provider for decrypting.
        AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider();
        aesProvider.Key = System.Text.Encoding.ASCII.GetBytes(strKey);
        aesProvider.IV = System.Text.Encoding.ASCII.GetBytes(strIV);
        aesProvider.BlockSize = 128;
        aesProvider.KeySize = 256;
        aesProvider.Padding = PaddingMode.PKCS7;
        aesProvider.Mode = CipherMode.CBC;

        ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor(aesProvider.Key, aesProvider.IV);
        byte[] DecryptedBytes = cryptoTransform.TransformFinalBlock(EncryptedBytes, 0, EncryptedBytes.Length);
        return System.Text.Encoding.ASCII.GetString(DecryptedBytes);
    }
}
解决方案

You need to set the BlockSize and the KeySize before you set the Key and the IV. Additionally you should probably be generating a random IV for each message and note that ICryptoTransform implements IDisposable so these objects should be disposed.

这篇关于"填充是无效的,不能删除" -Whats不对的code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:40