我正在尝试使用以下函数进行加密和解密,但是,它会导致填充错误。
如果我将PaddingMode
设置为None
,则会在日志中返回一些字母字符和随机符号。
我可能会错过设置正确结构的内容,如下所示:
任何帮助修复此错误以及确保满足上述结构的任何帮助将不胜感激!感谢
错误
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)
我的代码
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);
}
}
答案 0 :(得分:5)
加密时,您似乎没有设置IV,因此将自动使用随机IV。由于 在解密时设置IV(并且它不会与加密期间使用的相同),因此第一个输出块将损坏。如果消息足够短(<1块),则填充也将损坏,可能导致此错误。
与往常一样,我注意到在其他可能的问题中,使用相同的IV作为密钥是不好的做法,使用Encoding.UTF8.GetBytes()
进行密钥推导也是如此。