我试图让这段代码加密和解密,但在.FlushFinalBlock()
.
我不确定我在这里做错了什么,因为我在这个问题上没有我的元素。
谁能告诉我我在这里做错了什么?加密是可以的,但它不能解密它的加密。
static byte[] u8_Salt = new byte[] { 0x26, 0x19, 0x81, 0x4E, 0xA0, 0x6D, 0x95, 0x34, 0x26, 0x75, 0x64, 0x05, 0xF6 };
public static string Encrypt(string data, string password)
{
//
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, u8_Salt);
//
Rijndael i_Alg = Rijndael.Create();
i_Alg.Padding = PaddingMode.None;
i_Alg.Key = pdb.GetBytes(32);
i_Alg.IV = pdb.GetBytes(16);
//
using (var cryptoProvider = new DESCryptoServiceProvider())
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(), CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(data);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
}
public static string Decrypt(string data, string password)
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, u8_Salt);
//
Rijndael i_Alg = Rijndael.Create();
i_Alg.Padding = PaddingMode.None;
i_Alg.Key = pdb.GetBytes(32);
i_Alg.IV = pdb.GetBytes(16);
//
using (var cryptoProvider = new DESCryptoServiceProvider())
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(), CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(data);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
}