在解密我正在一個錯誤和加密的字符串:得到錯誤而解密串
5duOH + Tlg5deIrWZiHoNaQ == wVxXSl9pFu6A8h14/nLdyBkDzO4xmec7PQ0cB7MHjCDqhSRum3C7I1OfqL1rEWbNonU/ayCaJS18zV7ivQQU7A == MBJzKMrrrbmc2/vBZSPDkQ == I09Kj25UO + LcmRzgoqTT2g == + Fkm9VCGplEK6eEyHyEtuEodKSbckC07M2FShu2ukCg =
錯誤如下:
輸入不是有效的Base-64的字符串作爲它ç包含非基本64字符,多於兩個填充字符或填充字符中的非法字符。
我的加密的代碼是: 公共字符串EncryptQueryString(串的inputText,串鍵,串鹽) { 字節[]明文= Encoding.UTF8.GetBytes(inputText的);
using (RijndaelManaged rijndaelCipher = new RijndaelManaged())
{
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(Encoding.ASCII.GetBytes(key), Encoding.ASCII.GetBytes(salt));
using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
using (MemoryStream memoryStream = new MemoryStream())
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainText, 0, plainText.Length);
cryptoStream.FlushFinalBlock();
string base64 = Convert.ToBase64String(memoryStream.ToArray());
// Generate a string that won't get screwed up when passed as a query string.
string urlEncoded = HttpUtility.UrlEncode(base64);
return urlEncoded;
}
}
}
解密:
public string DecryptQueryString(string inputText, string key, string salt)
{
byte[] encryptedData = Convert.FromBase64String(inputText);
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(Encoding.ASCII.GetBytes(key), Encoding.ASCII.GetBytes(salt));
using (RijndaelManaged rijndaelCipher = new RijndaelManaged())
using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainText = new byte[encryptedData.Length];
cryptoStream.Read(plainText, 0, plainText.Length);
string utf8 = Encoding.UTF8.GetString(plainText);
return utf8;
}
}
錯誤似乎是你雖然這是一個代碼編寫服務 –
什麼是錯誤?你是如何加密字符串的?你怎麼試圖解密字符串?認真 - 我們需要更多的信息! –
@AlfieGoodacre一個心靈的代碼寫作服務! –