加密填充无效,无法删除
本文关键字:删除 填充 无效 加密 | 更新日期: 2023-09-27 17:52:12
我正在尝试解密windows phone 8中的字符串。但不幸的是,它给了我以下错误
这是我的代码。 txtText。文本是256位加密的。System.Security.Cryptography。CryptographicException:填充无效,不能删除。在System.Security.Cryptography.RijndaelManagedTransform。DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]&outputBuffer, Int32 outputOffset, PaddingMode, PaddingMode, Boolean fLast)在System.Security.Cryptography.RijndaelManagedTransform。TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)在System.Security.Cryptography.CryptoStream.FlushFinalBlock ()在SampleAESEncryption.AES256。解密(String dataToDecrypt, String password, String salt)在SampleAESEncryption.MainPage。btnDecrypt_Click(对象发送方,RoutedEventArgs)在System.Windows.Controls.Primitives.ButtonBase.OnClick ()在System.Windows.Controls.Button.OnClick ()在System.Windows.Controls.Primitives.ButtonBase。OnMouseLeftButtonUp (MouseButtonEventArgs e)在System.Windows.Controls.Control。OnMouseLeftButtonUp(Control ctrl, EventArgs)在MS.Internal.JoltHelper。FireEvent(IntPtr unmanagedObj, IntPtr unmanagedobjgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
txtText.Text = "Y5tq+5Smr13ChO2KYTOxvbCBlRTIDFXf+Ott2Euq+HiXTHDtUXn2+E46CYCGSC7P";
private void btnDecrypt_Click(object sender, RoutedEventArgs e)
{
AES256 encryptor = new AES256();
string strBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(txtText.Text.Trim()));
string decryptedString = encryptor.Decrypt(strBase64, "12345678", "12345678");
txtText.Text = decryptedString;
}
<<p> 解密方法/strong> public string Decrypt(string dataToDecrypt, string password, string salt)
{
AesManaged aes = null;
MemoryStream memoryStream = null;
try
{
//Generate a Key based on a Password and HMACSHA1 pseudo-random number generator
//Salt must be at least 8 bytes long
//Use an iteration count of at least 1000
Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);
//Create AES algorithm
aes = new AesManaged();
//Key derived from byte array with 32 pseudo-random key bytes
aes.Key = rfc2898.GetBytes(32);
//IV derived from byte array with 16 pseudo-random key bytes
aes.IV = rfc2898.GetBytes(16);
//Create Memory and Crypto Streams
memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);
//Decrypt Data
byte[] data = Convert.FromBase64String(dataToDecrypt);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
//Return Decrypted String
byte[] decryptBytes = memoryStream.ToArray();
//Dispose
if (cryptoStream != null)
cryptoStream.Dispose();
//Retval
return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
}
finally
{
if (memoryStream != null)
memoryStream.Dispose();
if (aes != null)
aes.Clear();
}
}
我试了很多,但不能解决这个问题。我怎么解决这个问题?我的解密方法有什么问题吗?
Convert.ToBase64String(Encoding.UTF8.GetBytes(txtText.Text.Trim()));
这几乎肯定是一个错误。您应该使用Convert.FromBase64
从数据中获取字节数组,对其进行解密,然后使用Encoding.UTF8.GetString
将结果转换为字符串。当加密时,用另一种方式:用Encoding.UTF8.GetBytes
获得字节,加密它们,然后用Convert.ToBase64String
将结果转换为字符串。