AES加密填充的問題,我開始了使用歐洲央行,因爲我聽說,這是開始,所以我創建了一個接受輸入加密一個控制檯應用程序最簡單的,然後將其解密,並同時輸出加密文本和解密的文本。這一切都很完美。我去嘗試使用CBC從頭創建一個新程序,我收到一個錯誤:「填充無效,無法刪除。」從我所看到的我所做的一切都是正確的,任何人都可以在正確的方向上指出我的問題可能是什麼?我曾試過在博客和本網站上找到的一切無濟於事。 DecryptData方法中的行DecryptedText = srDecrypt.ReadToEnd()
發生異常。在.NET
RijndaelManaged rijm = new RijndaelManaged();
//more variables here
protected void Page_Load(object sender, EventsArgs e)
{
//Convert Key from string to Byte Array Here
rijm.BlockSize = 128;
rijm.KeySize = 256;
rijm.Key = keyByteArray;
rijm.Mode = CipherMode.CBC;
rijm.Padding = PaddingMode.PKCS7;
ICryptoTransform crypt = rijm.CreateEncryptor(keyByteArray, null);
using(crypt)
{
byte[] value1Cipher = EncryptData(crypt, value1);
byte[] value2Cihper = EncryptData(crypt, value2);
encryptedValue1 = Convert.ToBase64String(vaue1Cipher);
encrpytedValue2 = Convert.ToBase64String(value1Cipher);
}
ICryptoTransform decrypt = rijm.CreateDecryptor(keyByteArray, null);
using (decrypt)
{
decryptedValue1 = DecryptData(decrypt, encryptedValue1); //string
decryptedValue2 = DecryptData(decrypt, encrpytedValue2); //string
}
//... Do something with the values here.
}
private byte[] EncryptData(ICryptoTransform encrypt, string text)
{
var stream = new MemoryStream();
using (var cryptoStream = new CryptoStream(stream, encrypt, CryptoStreamMode.Write))
{
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(text);
}
}
return stream.ToArray();
}
private string DecryptData(ICryptoTransform decrypt, string cipherText)
{
string decryptedText = null;
byte[] text = Convert.FromBase64String(cipherText);
using (MemoryStream msDecrypt = new MemoryStream(text))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decrypt, CryptoStreamMode.Read))
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
decryptedText = srDecrypt.ReadToEnd();
return decryptedText;
}
@JonWillemse我已經嘗試過,而且它沒有影響。 – ios85
你會在哪一行發生異常? –
@JohnWillemse它發生在DecryptData方法中的decryptedText = srDecrypt.ReadToEnd()行。 – ios85