填充无效,无法删除 [英] Padding is invalid and cannot be removed
问题描述
首先,我知道还有很多其他线程,我已经研究了它们并尝试了他们的建议,但到目前为止没有一个起作用,所以我开始了我自己的线程. /p>
我的程序需要从文本框中获取一个字符串并对其进行加密.稍后,它将被另一个Rijndael对象解密.
我确保2个rijndael对象使用相同的密钥和IV,设置了Padding属性,并且两者都相同,甚至设置了密码模式.尽管有所有这些,我仍然在"cs.FlushFinalBlock();"中收到填充无效且无法删除"错误 解密方法.
这是代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Security.Cryptography; using System.IO; namespace Encrypt { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //ENCRYPT BUTTON IS CLICKED private void btnEncrypt_Click(object sender, EventArgs e) { string input = tbxInput.Text.Trim(); string pwd = "password"; string salt = "234LKJawenHK45ddI91L"; pwd += salt; byte[] pwdArr = Encoding.Unicode.GetBytes(pwd.ToCharArray()); byte[] saltArr = Encoding.Unicode.GetBytes(salt.ToCharArray()); byte[] inputArr = Encoding.Unicode.GetBytes(input.ToCharArray()); //byte[] inputArr = Convert.FromBase64String(input); PasswordDeriveBytes pdb = new PasswordDeriveBytes(pwdArr, saltArr); RijndaelManaged eRijndael = new RijndaelManaged(); eRijndael.Mode = CipherMode.CBC; eRijndael.Padding = PaddingMode.PKCS7; eRijndael.Key = pdb.GetBytes(32); eRijndael.IV = pdb.GetBytes(16); eRijndael.BlockSize = 128; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, eRijndael.CreateEncryptor(eRijndael.Key, eRijndael.IV), CryptoStreamMode.Write); cs.Write(inputArr, 0, inputArr.Length); cs.FlushFinalBlock(); cs.Close(); tbxEncrypted.Text = Convert.ToBase64String(ms.ToArray()); ms.Close(); eRijndael.Clear(); } //DECRYPT BUTTON CLICKED private void btnDecrypt_Click(object sender, EventArgs e) { string input = tbxEncrypted.Text.Trim(); string pwd = "password"; string salt = "234LKJawenHK45ddI91L"; //byte[] inputArr = Convert.FromBase64String(input); byte[] inputArr = Encoding.Unicode.GetBytes(input); byte[] pwdArr = Encoding.Unicode.GetBytes(pwd); byte[] saltArr = Encoding.Unicode.GetBytes(salt); PasswordDeriveBytes pdb = new PasswordDeriveBytes(pwdArr, saltArr); RijndaelManaged dRijndael = new RijndaelManaged(); dRijndael.Mode = CipherMode.CBC; dRijndael.Padding = PaddingMode.PKCS7; dRijndael.Key = pdb.GetBytes(32); dRijndael.IV = pdb.GetBytes(16); dRijndael.BlockSize = 128; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, dRijndael.CreateDecryptor(dRijndael.Key, dRijndael.IV), CryptoStreamMode.Write); cs.Write(inputArr, 0, inputArr.Length); cs.FlushFinalBlock(); cs.Close(); tbxDecrypted.Text = Convert.ToBase64String(ms.ToArray()); ms.Close(); dRijndael.Clear(); } } }
是否已在调试模式下运行此程序... ... ???
只要看看错误出现在哪里并尝试找出原因〜
First of all, I'm aware there are many other threads out there and i have looked at them and have tried their recommendations and none have worked so far so i'm starting my own thread.
My program needs to take a string from a textbox and encrypt it. Later, it will be decrypted by a different Rijndael object.
I've made sure the 2 rijndael objects use the same key and IV, the Padding property is set and identical for both, and even the cipher mode is set. Despite all of that I still get a "Padding is invalid and cannot be removed" error on 'cs.FlushFinalBlock();' in the decryption method.
Here's the code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Security.Cryptography; using System.IO; namespace Encrypt { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //ENCRYPT BUTTON IS CLICKED private void btnEncrypt_Click(object sender, EventArgs e) { string input = tbxInput.Text.Trim(); string pwd = "password"; string salt = "234LKJawenHK45ddI91L"; pwd += salt; byte[] pwdArr = Encoding.Unicode.GetBytes(pwd.ToCharArray()); byte[] saltArr = Encoding.Unicode.GetBytes(salt.ToCharArray()); byte[] inputArr = Encoding.Unicode.GetBytes(input.ToCharArray()); //byte[] inputArr = Convert.FromBase64String(input); PasswordDeriveBytes pdb = new PasswordDeriveBytes(pwdArr, saltArr); RijndaelManaged eRijndael = new RijndaelManaged(); eRijndael.Mode = CipherMode.CBC; eRijndael.Padding = PaddingMode.PKCS7; eRijndael.Key = pdb.GetBytes(32); eRijndael.IV = pdb.GetBytes(16); eRijndael.BlockSize = 128; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, eRijndael.CreateEncryptor(eRijndael.Key, eRijndael.IV), CryptoStreamMode.Write); cs.Write(inputArr, 0, inputArr.Length); cs.FlushFinalBlock(); cs.Close(); tbxEncrypted.Text = Convert.ToBase64String(ms.ToArray()); ms.Close(); eRijndael.Clear(); } //DECRYPT BUTTON CLICKED private void btnDecrypt_Click(object sender, EventArgs e) { string input = tbxEncrypted.Text.Trim(); string pwd = "password"; string salt = "234LKJawenHK45ddI91L"; //byte[] inputArr = Convert.FromBase64String(input); byte[] inputArr = Encoding.Unicode.GetBytes(input); byte[] pwdArr = Encoding.Unicode.GetBytes(pwd); byte[] saltArr = Encoding.Unicode.GetBytes(salt); PasswordDeriveBytes pdb = new PasswordDeriveBytes(pwdArr, saltArr); RijndaelManaged dRijndael = new RijndaelManaged(); dRijndael.Mode = CipherMode.CBC; dRijndael.Padding = PaddingMode.PKCS7; dRijndael.Key = pdb.GetBytes(32); dRijndael.IV = pdb.GetBytes(16); dRijndael.BlockSize = 128; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, dRijndael.CreateDecryptor(dRijndael.Key, dRijndael.IV), CryptoStreamMode.Write); cs.Write(inputArr, 0, inputArr.Length); cs.FlushFinalBlock(); cs.Close(); tbxDecrypted.Text = Convert.ToBase64String(ms.ToArray()); ms.Close(); dRijndael.Clear(); } } }
Have you run this in Debug Mode......???
just see where the error show up and try to find the cause~
这篇关于填充无效,无法删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!