JavaScript: 已在C#中加密的描述字符串
JavaScript: Decript string that has been encrypted in C#
我创建了这个方法来使用密钥加密字符串:
public static string EncryptString(string key, string plainText)
{
byte[] iv = new byte[16];
byte[] array;
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(key);
aes.IV = iv;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
{
streamWriter.Write(plainText);
}
array = memoryStream.ToArray();
}
}
}
return Convert.ToBase64String(array);
}
但是我在JS中也需要这个方法,来解密同样的字符串。
我想知道
AES、编码。UTF8.GetBytes、ICryptoTransform、Cryptostreammode 和 MemoryStream。
JS中存在这些吗?
这个方法在 JS 中还能用吗?
使用您的 C# 代码通过 public static string EncryptString(string key, string plainText)
加密字符串,我得到以下结果:
EncryptString("xxxxxxxxxxxxxxxx", "Hello World") -> "yM377gXxX5Du71hgkPH+Fg=="
要扭转这一局面,您可以查看此 Javascript 代码:
const key = "xxxxxxxxxxxxxxxx";
const msg = "yM377gXxX5Du71hgkPH+Fg==";
const decrypted = CryptoJS.AES.decrypt(
msg,
CryptoJS.enc.Utf8.parse(key),
{ mode: CryptoJS.mode.ECB }
);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
我创建了这个方法来使用密钥加密字符串:
public static string EncryptString(string key, string plainText)
{
byte[] iv = new byte[16];
byte[] array;
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(key);
aes.IV = iv;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
{
streamWriter.Write(plainText);
}
array = memoryStream.ToArray();
}
}
}
return Convert.ToBase64String(array);
}
但是我在JS中也需要这个方法,来解密同样的字符串。
我想知道
AES、编码。UTF8.GetBytes、ICryptoTransform、Cryptostreammode 和 MemoryStream。
JS中存在这些吗?
这个方法在 JS 中还能用吗?
使用您的 C# 代码通过 public static string EncryptString(string key, string plainText)
加密字符串,我得到以下结果:
EncryptString("xxxxxxxxxxxxxxxx", "Hello World") -> "yM377gXxX5Du71hgkPH+Fg=="
要扭转这一局面,您可以查看此 Javascript 代码:
const key = "xxxxxxxxxxxxxxxx";
const msg = "yM377gXxX5Du71hgkPH+Fg==";
const decrypted = CryptoJS.AES.decrypt(
msg,
CryptoJS.enc.Utf8.parse(key),
{ mode: CryptoJS.mode.ECB }
);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>