其他鏈接對我沒有用處。 我在保存在mysql數據庫之前加密字符串。這是正常工作。但是當我想要檢索時,它給我加密數據的長度是無效的錯誤。 在我的加密要求之前,我正在使用varchar大小的數據類型500.now即時通訊使用大小爲800的varbinary。任何1都可以告訴我這個大小呢?解密數據的長度與rijndael加密無效
加密方法:
public static byte[] encryptStringToBytes(string plainText)
{
byte[] encrypted;
//create an Rijndael object
using (Rijndael rijAlg = Rijndael.Create())
{
//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);
}
encrypted = msEncrypt.ToArray();
}
}
}
return encrypted;
}
解密方法:
public static string decryptStringFromBytes(byte[] cipherText)
{
string plaintext = null;
//create an Rijndael object
using (Rijndael rijAlg = Rijndael.Create())
{
//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.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
從結果集
using (Rijndael myRijndael = Rijndael.Create())
{
temp5 = EncryptDecrypt.decryptStringFromBytes((byte[])reader.GetValue(reader.GetOrdinal("Body")));
}
當加密或填充不匹配時,您可能沒有調用FlushFinalBlock。 – leppie 2013-02-24 15:31:29
向我們展示加密和存儲。你不能確定存儲是否工作(爲什麼你這麼說,順便說一句?)。 – usr 2013-02-24 15:33:06
如果我測試用串加密「我正在測試」。這值存儲在我的表221305b3a40332b912c1d356cdab4217 – user2092583 2013-02-24 15:39:24