Padding is invalid and cannot be removed
尝试解密某些数据时出现运行时错误。我已经阅读了我在 stackoverflow 上找到的所有类似线程,并考虑了以下问题:
给定一个与用于加密的密钥不同的解密密钥可能会带来上述问题。但是,正如您在用于测试目的的源代码中看到的那样,我对密钥进行了硬编码,但仍然出现运行时错误。
应用程序可能会尝试使用与用于加密的填充模式不同的填充模式进行解密。在加密或解密过程中明确设置模式并不能解决问题。
有人说您需要使用该
FlushFinalBlock
方法,以便告诉应用程序将最后的位放入blocksize
. 试着把它放在 Write 方法下面,也没有用。
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
csEncrypt.FlushFinalBlock();
}
以下是我的代码片段:
// Encrypt
private byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
byte[] encrypted;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
rijAlg.Padding = PaddingMode.PKCS7;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
// Write all data to the stream.
swEncrypt.Write(plainText);
csEncrypt.FlushFinalBlock();
}
encrypted = msEncrypt.ToArray();
MessageBox.Show("Encrypted str: " + Encoding.UTF8.GetString(encrypted).ToString());
}
}
}
// Update buffer length
this.Buflen = encrypted.Length;
MessageBox.Show("Byte count: " + (Encoding.UTF8.GetByteCount(encrypted.ToString())).ToString());
MessageBox.Show("Decrypted: " + DecryptStringFromBytes(encrypted, this.Encryption_key, this.Encryption_iv));
// Return the encrypted bytes from the memory stream.
return encrypted;
}
// Decrypt
private string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
{
Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
UTF8Encoding e = new UTF8Encoding();
MessageBox.Show("ciperText " + cipherText.Length);
//cipherText = e.GetBytes(cipherText);
//MessageBox.Show("ciperText2 Length " + cipherText.Length);
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
rijAlg.Padding = PaddingMode.PKCS7;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
if (cipherText.Length != rijAlg.BlockSize)
{
throw new InvalidOperationException("Invalid cipherText length");
}
plaintext = srDecrypt.ReadToEnd();
csDecrypt.FlushFinalBlock();
}
}
}
}
return plaintext;
}
byte[] encoded_message = new byte[16];
encoded_message = EncryptStringToBytes("test", this.Encryption_key,this.Encryption_iv);
从昨天早上开始,我一直在努力让它工作。到目前为止,我发现的解决方案都没有在我的应用程序中起作用。我知道代码可能看起来有点乱,但请不要介意没有使用的部分——一旦我得到这个工作,它肯定需要一些重构。
如您所见,在使用硬编码密钥和 IV 进行测试时,仍然会发生运行时错误,因此传递与用于加密的凭据不同的凭据进行解密不是原因。
任何人都可以提出如何解决这个问题的想法吗?谢谢。