0
我與AES在C#這樣加密的字符串:解密AES加密在C#中使用加密的js
public static byte[] KeyFromString(string key, int keyBits)// keyBits is 128, 192, or 256.
{
byte[] keyBinary = Encoding.UTF8.GetBytes(key);
byte[] b = new byte[keyBits/8];
for (int i = 0, j = 0; i < b.Length && j < keyBinary.Length; i++, j++)
{
b[i] = keyBinary[j];
}
return b;
}
public static string encrypt(string key, string input)
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] keyBytes = KeyFromString(key, 256);
byte[] encrypted = null;
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Mode = CipherMode.CBC;
rijAlg.Padding = PaddingMode.PKCS7;
rijAlg.KeySize = 256;
rijAlg.Key = keyBytes;
rijAlg.IV = new byte[rijAlg.IV.Length]; //use empty IV
using(var encryptor = rijAlg.CreateEncryptor())
{
encrypted = encryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
}
}
var res = Convert.ToBase64String(encrypted);
return res;
}
我想解密的javascript加密的文本,我tryed與加密JS:
var iv = [];
for (var i = 0 ; i < 16 ; i++) iv.push(0); //empty IV
var options = { keySize: 256/8, mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7 };
var decrypted = CryptoJS.AES.decrypt(cipheredtext, key, options);
var decryptedText = decrypted.toString(CryptoJS.enc.Utf8);
我得到的解密文本是空的。我試着在編碼多變化,按鍵大小等
我可以用其他JavaScript如果需要