0
我有以下解密方法,似乎工作正常,除了它只返回16個字節,而不是32它應該我盯着它太長,無法弄清楚我做錯了什麼。我希望不同的眼睛會有所幫助。謝謝。解密返回16個字節而不是32個,爲什麼?
private string Decrypt(string dataToDecrypt, string password, string salt)
{
//Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator
Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt));
//Create AES algorithm with 256 bit key and 128-bit block size
using (var aes = new AesManaged())
{
// These two values are hard coded in Silverlight
//aes.Mode = CipherMode.CBC;
//aes.Padding = PaddingMode.PKCS7;
aes.Key = rfc2898.GetBytes(aes.KeySize/8);
aes.IV = rfc2898.GetBytes(aes.BlockSize/8);
//Create Memory and Crypto Streams
using (var memoryStream = new MemoryStream())
{
using (var 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();
string decrypted = Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
return decrypted;
} // using (cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
} // using (memoryStream = new MemoryStream())
} // using (aes = new AesManaged())
} // string Decrypt(string dataToDecrypt, string password, string salt)
嗨延恩,感謝您的意見。我不能將BlockSize設置得更大,它不允許它,所以它應該如何解碼超過一個塊? – 2010-12-15 21:23:07
@WillTartak:完全取決於正在使用的密碼模式。歐洲央行? CBC? CTR? XTR? CFB?你想要完成什麼,一個更高級別(和精心設計)的加密API不提供? – 2010-12-15 22:13:06