解密一个c# AES加密
本文关键字:AES 加密 一个 解密 | 更新日期: 2023-09-27 18:17:26
我想解密JavaScript中的文本。
我加密c#中的文本。这是我的c#加密代码:
readonly byte[] _salt = new byte[] { 48,94,2,4,9,4,52,222,12,65 };
private string getEncryptedString(string text, string password)
{
string retCryptedString = null;
byte[] retCryptedByteArr = null;
retCryptedByteArr = encryptStringToBytes(text, createHash(password));
retCryptedString = Convert.ToBase64String(retCryptedByteArr);
return retCryptedString;
}
private Rfc2898DeriveBytes createHash(string password)
{
Rfc2898DeriveBytes hash = default(Rfc2898DeriveBytes);
hash = new Rfc2898DeriveBytes(password, _salt);
return hash;
}
private byte[] encryptStringToBytes(string Text, Rfc2898DeriveBytes hash)
{
RijndaelManaged encryption = new RijndaelManaged();
RijndaelManaged rijAlg = new RijndaelManaged();
byte[] encrypted = null;
ICryptoTransform encryptor;
if (Text == null || Text.Length <= 0)
{
throw new ArgumentNullException("No text");
}
rijAlg = (RijndaelManaged)Rijndael.Create();
rijAlg.Key = hash.GetBytes(encryption.KeySize / 8);
rijAlg.IV = hash.GetBytes(encryption.BlockSize / 8);
encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
using (MemoryStream ms_Encrypt = new MemoryStream())
{
using (CryptoStream cs_Encrypt = new CryptoStream(ms_Encrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter sw_Encrypt = new StreamWriter(cs_Encrypt))
{
sw_Encrypt.Write(Text);
}
encrypted = ms_Encrypt.ToArray();
}
}
return encrypted;
}
我尝试了很多库(例如crypto-js)。但是我无法解密它,因为我不知道如何生成密钥和iv。
当我生成PBKDF2密钥时,我不能将其添加到解密中。
//Generic Handler
public void ProcessRequest(HttpContext context)
{
string EncryptedString = context.Request.QueryString["encrypted"];
response.write(decryptedString(EncryptedString);
}
//javascript
$.get("MyHandler.ashx?encrypted=YourEncryptedString").done(function(response){
var DecryptedString = response
});