坏PKCS7填充错误:无效长度106 [英] Bad PKCS7 Padding error: Invalid length 106

查看:2366
本文介绍了坏PKCS7填充错误:无效长度106的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加密和具有以下功能解密,但是,它导致不良填充错误。



如果我设置 PaddingMode 则返回登录一些字母字符和随机符号。



我M可能失踪设置正确的结构,这是一件好事如下:




  • 密码Rijndael算法(AES)

  • 块大小为128位(16字节)

  • 模式CBC(密码块链接)键

  • MD5哈希密码

  • IV相同键

  • 数据编码Base64编码字符

  • UTF-8编码



任何修正这个错误,并保证上述结构满足任何援助,帮助将不胜感激!谢谢



错误



  CryptographicException:坏PKCS7填充。无效长度106 
Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException(PaddingMode填充,长的Int32,的Int32位置)(在/Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/Mono.Security。加密/ SymmetricTransform.cs:363)
Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt(System.Byte [] INPUTBUFFER,的Int32 inputOffset,的Int32 inputCount)(在/用户/ builduser / buildslave / monoAndRuntimeClassLibs /编译/ MCS /类/ corlib / Mono.Security.Cryptography / SymmetricTransform.cs:515)
Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock(System.Byte [] INPUTBUFFER,的Int32 inputOffset,的Int32 inputCount)(AT /用户/ builduser / buildslave / monoAndRuntimeClassLibs /编译/ MCS /班/ corlib / Mono.Security.Cryptography / SymmetricTransform.cs:554)
System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(System.Byte [] INPUTBUFFER,的Int32 inputOffset,的Int32 inputCount )(在/Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Security.Cryptography/RijndaelManagedTransform.cs:94)
APIConnector.Decrypt(System.String toDecrypt)(在资产/ APIConnector的.cs:85)

我的代码

 使用UnityEngine; 
使用系统; System.Collections中使用
;
使用System.Collections.Generic;
使用System.Security.Cryptography;
使用System.Text;
使用的System.Xml;
:使用System.IO;

公共类APIConnector:MonoBehaviour {

//使用这个初始化
无效的start(){

的debug.log(启动API接口);


}

//更新是每帧
无效更新调用一次(){



}

静态字符串数据;
字符串的firstName =;
字符串密码=;

无效OnGUI(){

的firstName = GUILayout.TextField(名字,GUILayout.Width(300));
密码= GUILayout.TextField(密码,GUILayout.Width(300));

如果(GUILayout.Button(提交))
提交();

}

//我们的函数

无效提交(){

的debug.log(名称是: + +的firstName加密是:+加密(名字));
的debug.log(名称是:+ +的firstName解密是:+解密(名字));

}


公共静态字符串加密(字符串toEncrypt)
{
字节[] = keyArray UTF8Encoding.UTF8.GetBytes( SecretPassphrase);
// 256-AES密钥
INT的numBytes = System.Text.Encoding.UTF8.GetBytes(toEncrypt)。长度;
的debug.log(字节:+的numBytes);
字节[] = toEncryptArray UTF8Encoding.UTF8.GetBytes(toEncrypt);
RijndaelManaged的RDEL =新RijndaelManaged的();
rDel.Key = keyArray;
rDel.BlockSize = 128;
rDel.Mode = CipherMode.CBC;
// http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
rDel.Padding = PaddingMode.PKCS7;
//更好的支持郎
ICryptoTransform的cTransform = rDel.CreateEncryptor();
字节[] = resultArray cTransform.TransformFinalBlock(toEncryptArray,0,toEncryptArray.Length);
返回Convert.ToBase64String(resultArray,0,resultArray.Length);
}

公共静态字符串解密(字符串toDecrypt)
{
字节[] = keyArray UTF8Encoding.UTF8.GetBytes(SecretPassphrase);
// AES-256键
字节[]的EncryptedData = System.Convert.FromBase64String(toDecrypt);
//字节[] = toEncryptArray Convert.FromBase64String(toDecrypt);
RijndaelManaged的RDEL =新RijndaelManaged的();
rDel.Key = keyArray;
rDel.BlockSize = 128;
rDel.Mode = CipherMode.CBC;
rDel.IV = rDel.Key;
// http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
rDel.Padding = PaddingMode.PKCS7;
//更好的支持郎
ICryptoTransform的cTransform = rDel.CreateDecryptor();
字节[] = resultArray cTransform.TransformFinalBlock(的EncryptedData,0,encryptedData.Length);
返回UTF8Encoding.UTF8.GetString(resultArray);
}


}


解决方案

您似乎没有加密的时候,所以随机IV将被自动用于将设置IV。既然你的的解密时设置的IV(它不会像加密过程中使用相同的),第一个输出块将会被破坏。如果消息是足够短(小于1块),则填充也将被破坏可能导致这个错误



像往常一样,我会注意到,中其他可能的问题,使用的关键是不好的做法,因为是密钥派生使用 Encoding.UTF8.GetBytes()的。同样的四


I'm trying to encrypt and decrypt with the following functions, however, it results in a bad padding error.

If I set PaddingMode to None it returns some alpha characters and random symbols in Log.

I'm possibly missing something from setting the correct structure which is as follows:

  • Cipher Rijndael (AES)
  • Block Size 128 bits (16 bytes)
  • Mode CBC (Cipher Block Chaining) Key
  • MD5 hash passphrase
  • IV Same as the key
  • Data Encoding Base64 Character
  • UTF-8 Encoding

Any help fixing this error and any assistance with ensuring the above structure is met would be greatly appreciated! Thanks

Error

CryptographicException: Bad PKCS7 padding. Invalid length 106.
Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (PaddingMode padding, Int32 length, Int32 position) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/Mono.Security.Cryptography/SymmetricTransform.cs:363)
Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/Mono.Security.Cryptography/SymmetricTransform.cs:515)
Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/Mono.Security.Cryptography/SymmetricTransform.cs:554)
System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock (System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Security.Cryptography/RijndaelManagedTransform.cs:94)
APIConnector.Decrypt (System.String toDecrypt) (at Assets/APIConnector.cs:85)

My code

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using System.IO;

public class APIConnector : MonoBehaviour {

    // Use this for initialization
    void Start () {

        Debug.Log ("Starting API connector");


    }

    // Update is called once per frame
    void Update () {



    }

    static string data;
    string firstName="";
    string password="";

    void OnGUI() {

        firstName = GUILayout.TextField (firstName, GUILayout.Width(300));
        password = GUILayout.TextField (password, GUILayout.Width(300));

        if (GUILayout.Button ("Submit"))
                        submit ();

    }

    //Our functions

    void submit(){

        Debug.Log ("Name is: " + firstName + " encrypted is: " + Encrypt(firstName));
        Debug.Log ("Name is: " + firstName + " decrypted is: " + Decrypt(firstName));

    }


            public static string Encrypt (string toEncrypt)
            {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("SecretPassphrase");
                // 256-AES key
        int numBytes = System.Text.Encoding.UTF8.GetBytes(toEncrypt).Length;
        Debug.Log ("Bytes: " + numBytes);
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (toEncrypt);
                RijndaelManaged rDel = new RijndaelManaged ();
                rDel.Key = keyArray;
                rDel.BlockSize = 128;
                rDel.Mode = CipherMode.CBC;
                // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
                rDel.Padding = PaddingMode.PKCS7;
                // better lang support
                ICryptoTransform cTransform = rDel.CreateEncryptor ();
                byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
                return Convert.ToBase64String (resultArray, 0, resultArray.Length);
            }

            public static string Decrypt (string toDecrypt)
            {
        byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("SecretPassphrase");
                // AES-256 key
        byte[] encryptedData = System.Convert.FromBase64String(toDecrypt);
                //byte[] toEncryptArray = Convert.FromBase64String (toDecrypt);
                RijndaelManaged rDel = new RijndaelManaged ();
                rDel.Key = keyArray;
                rDel.BlockSize = 128;
                rDel.Mode = CipherMode.CBC;
                rDel.IV = rDel.Key;
                // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
                rDel.Padding = PaddingMode.PKCS7;
                // better lang support
                ICryptoTransform cTransform = rDel.CreateDecryptor ();
        byte[] resultArray = cTransform.TransformFinalBlock (encryptedData, 0, encryptedData.Length);
                return UTF8Encoding.UTF8.GetString (resultArray);
            }


}

解决方案

You don't appear to be setting the IV when encrypting, and so a random IV will be used automatically. Since you are setting the IV when decrypting (and it won't be the same as was used during encryption), the first output block will be corrupt. If the message is short enough (< 1 block), then the padding will also be corrupt likely resulting in this error.

As usual, I'll note that among other possible issues, the use of the same IV as key is bad practice, as is the use of Encoding.UTF8.GetBytes() for key derivation.

这篇关于坏PKCS7填充错误:无效长度106的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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