填充是无效的不能删除? [英] Padding is invalid and cannot be removed?

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

问题描述

我在网上找了一下这个异常意味着相对于我的计划,但似乎无法找到解决的办法,或者为什么它发生在我的具体方案的原因。我一直用我提供MSDN用于加密和使用Rijndael算法解密一个如下的例子。加密工作正常,但是当我尝试解密,我得到了以下异常:填充无效,不能删除

谁能告诉我,我能做些什么来解决这个问题,请。我跑出来的资源。我似乎无法在别处找到了答案。我的code以下是我拿到钥匙等,如果cryptoMode是假的,它会调用解密方法,这是在发生异常:

 公共无效加密(DOC的XmlDocument,布尔cryptographyMode)
{
    RijndaelManaged的关键= NULL;
    尝试
    {
    //创建一个新的Rijndael密钥。
    关键=新RijndaelManaged的();
    常量字符串passwordBytes =Password1234; //这里密码    字节[] = saltBytes Encoding.UTF8.GetBytes(SaltBytes);
    Rfc2898DeriveBytes P =新Rfc2898DeriveBytes(passwordBytes,saltBytes);
    //大小由8 devided因为[1字节= 8比特]
    key.IV = p.GetBytes(key.BlockSize / 8);
    key.Key = p.GetBytes(key.KeySize / 8);    如果(cryptographyMode)
    {
        ECRYPT(文件,内容,键);
    }
    其他
    {
        解密(DOC,键);
    }    }
    赶上(异常前)
    {
    MessageBox.Show(ex.Message);
    }
    最后
    {
    //清除键。
    如果(关键!= NULL)
    {
        key.Clear();
    }
    }}私人无效解密(DOC的XmlDocument,SymmetricAlgorithm ALG)
{
    //检查的参数。
    如果(文件== NULL)
    抛出新的ArgumentNullException(文件);
    如果(ALG == NULL)
    抛出新的ArgumentNullException(ALG);    //查找XmlDocument的EncryptedData元素。
    的XmlElement encryptedElement = doc.GetElementsByTagName(的EncryptedData)[0]为XmlElement的;    //如果没有被发现EncryptedData元素,抛出异常。
    如果(encryptedElement == NULL)
    {
    抛出新XmlException(以下简称EncryptedData元素没有被发现。);
    }
    //创建一个EncryptedData对象,并填充它。
    的EncryptedData edElement =新的EncryptedData();
    edElement.LoadXml(encryptedElement);    //创建一个新EncryptedXml对象。
    EncryptedXml EXML =新EncryptedXml();
    //解密使用对称密钥的元素。
    字节[] = rgbOutput exml.DecryptData(edElement,ALG); < ----我得到的异常HERE
    //使用明文XML元素替换EncryptedData元素。
    exml.ReplaceData(encryptedElement,rgbOutput);}


解决方案

的Rijndael / AES是一种分组密码。它在128位(16个字符)的块加密的数据。 加密填充是用来确保消息的最后一块始终是正确的大小。

您解密方法期望无论其默认填充是,并没有找到它。作为@NetSquirrel说,你需要明确设置加密和解密的填充。除非你有一个理由不这样做,使用PKCS#7填充。

I have looked online for what this exception means in relation to my program but can't seem to find a solution or the reason why its happening to my specific program. I have been using the example provided my msdn for encrypting and decrypting an xmldocument using the rijndael algorithm. The encryption works fine but when i try to decrypt, I get the following exception: padding is invalid and cannot be removed?

Can anyone tell me what I can do to solve this issue please. I'm running out of resources. I can't seem to find the answer anywhere. My code below is where i get the key etc and if the cryptoMode is false it will call the decrypt method, which is where the exception occurs:

public void Cryptography(XmlDocument doc, bool cryptographyMode)
{
    RijndaelManaged key = null;
    try
    {
    // Create a new Rijndael key.
    key = new RijndaelManaged();
    const string passwordBytes = "Password1234"; //password here 

    byte[] saltBytes = Encoding.UTF8.GetBytes("SaltBytes");
    Rfc2898DeriveBytes p = new Rfc2898DeriveBytes(passwordBytes, saltBytes);
    // sizes are devided by 8 because [ 1 byte = 8 bits ] 
    key.IV = p.GetBytes(key.BlockSize/8);
    key.Key = p.GetBytes(key.KeySize/8);

    if (cryptographyMode)
    {
        Ecrypt(doc, "Content", key);
    }
    else
    {
        Decrypt(doc, key);
    }

    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    // Clear the key.
    if (key != null)
    {
        key.Clear();
    }
    }

}

private void Decrypt(XmlDocument doc, SymmetricAlgorithm alg)
{
    // Check the arguments.  
    if (doc == null)
    throw new ArgumentNullException("Doc");
    if (alg == null)
    throw new ArgumentNullException("alg");

    // Find the EncryptedData element in the XmlDocument.
    XmlElement encryptedElement = doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;

    // If the EncryptedData element was not found, throw an exception.
    if (encryptedElement == null)
    {
    throw new XmlException("The EncryptedData element was not found.");
    }


    // Create an EncryptedData object and populate it.
    EncryptedData edElement = new EncryptedData();
    edElement.LoadXml(encryptedElement);

    // Create a new EncryptedXml object.
    EncryptedXml exml = new EncryptedXml();


    // Decrypt the element using the symmetric key.
    byte[] rgbOutput = exml.DecryptData(edElement, alg); <----  I GET THE EXCEPTION HERE
    // Replace the encryptedData element with the plaintext XML element.
    exml.ReplaceData(encryptedElement, rgbOutput);

}

解决方案

Rijndael/AES is a block cypher. It encrypts data in 128 bit (16 character) blocks. Cryptographic padding is used to make sure that last block of the message is always the correct size.

Your decryption method is expecting whatever its default padding is, and is not finding it. As @NetSquirrel says, you need to explicitly set the padding for both encryption and decryption. Unless you have a reason to do otherwise, use PKCS#7 padding.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆