在c#中实现AES 256解密的方法如下:
using System.Security.Cryptography; using System.IO; using System.Text;
public static string AESDecrypt(string encryptedText, string password, string salt) { byte[] encryptedBytes = Convert.FromBase64String(encryptedText); byte[] saltBytes = Encoding.Unicode.GetBytes(salt);
Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, saltBytes, 1000);
byte[] keyBytes = keyGenerator.GetBytes(32);
byte[] ivBytes = keyGenerator.GetBytes(16);
using(Aes aes = Aes.Create())
{
aes.Key = keyBytes;
aes.IV = ivBytes;
using(MemoryStream ms = new MemoryStream())
{
using(CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(encryptedBytes, 0, encryptedBytes.Length);
}
return Encoding.Unicode.GetString(ms.ToArray());
}
}
}
string encryptedText = "加密后的文本"; string password = "密码"; string salt = "盐值";
string decryptedText = AESDecrypt(encryptedText, password, salt);
解密后的文本即为 decryptedText。