I have the following decryption method that seems to work fine except it is only returning 16 bytes instead of 32 it should I've stared at it too long and can't figure out what I've done wrong. I'm hoping a different set of eyes will help. Thanks.
private string Decrypt开发者_StackOverflow(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)
Your block size is 128 bits. If you are only decoding one block, you will get 16 bytes.
精彩评论