我在c#
中有以下AES加密功能public class AES
{
private AesManaged AESCipher;
private String Password;
private int Iterations;
private byte[] Salt;
public AES(String Password)
{
this.AESCipher = new AesManaged();
this.AESCipher.Mode = CipherMode.CBC;
this.AESCipher.Padding = PaddingMode.PKCS7;
this.AESCipher.IV = HexStringToByteArray("000102030405060708090A0B0C0D0E0F");
this.AESCipher.KeySize = 256;
this.AESCipher.BlockSize = 128;
this.Iterations = 1000;
this.Salt = System.Text.Encoding.ASCII.GetBytes("saltsalt");
this.Password = Password;
}
public String Encrypt(String PlainText)
{
this.AESCipher.Key = GenerateKey();
byte[] plainTextBytes = System.Text.Encoding.ASCII.GetBytes(PlainText);
ICryptoTransform transform = this.AESCipher.CreateEncryptor();
return Convert.ToBase64String(transform.TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length));
}
public String Decrypt(String CipherText)
{
this.AESCipher.Key = GenerateKey();
byte[] cipherTextBytes = Convert.FromBase64String(CipherText);
ICryptoTransform transform = this.AESCipher.CreateDecryptor();
return System.Text.Encoding.ASCII.GetString(transform.TransformFinalBlock(cipherTextBytes, 0, cipherTextBytes.Length));
}
private static string ByteArrayToHexString(byte[] b)
{
System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
int i = 0;
for (i = 0; i < b.Length; i++)
{
sb1.Append(System.String.Format("{0:X2}", b[i]));
}
return sb1.ToString().ToUpper();
}
private static byte[] HexStringToByteArray(string s)
{
var r = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
{
r[i / 2] = (byte)Convert.ToInt32(s.Substring(i, 2), 16);
}
return r;
}
private byte[] GenerateKey()
{
var rfc2898 = new System.Security.Cryptography.Rfc2898DeriveBytes(this.Password, this.Salt, this.Iterations);
return rfc2898.GetBytes(this.AESCipher.KeySize / 8);
}
}
通过上述功能,我从单词“hello”获得base64编码的字符串“HlHY8Y2v6idG2DoDMgvR / Q ==”。
然后我在Javascript中使用以下解密函数,使用slowAES库和其他东西,如cryptohelpers.js和pbkdf2.js:
function decrypt3() {
var mypbkdf2 = new PBKDF2("password", "saltsalt", 1000, 32);
var newKey;
var result_callback = function (key) {
newKey = key;
var bytes = cryptoHelpers.base64.decode('HlHY8Y2v6idG2DoDMgvR/Q==');
var result = slowAES.decrypt(bytes, 6, slowAES.modeOfOperation.CBC, hexStringToByteArray(key), 32, hexStringToByteArray("000102030405060708090A0B0C0D0E0F"));
alert(byteArrayToAscii(result));
};
mypbkdf2.deriveKey(function () {}, result_callback);
}
function asciiToByteArray(s)
{
var r= Array(s.length);
for (var i = 0; i < s.length; i++)
{
r[i]= s.charCodeAt(i);
}
return r;
}
function byteArrayToAscii(a) {
var r = "";
for (var i = 0; i < a.length; i++) {
r += String.fromCharCode(a[i]);
}
return r;
}
function hexStringToByteArray(s) {
try { hexcase } catch (e) { hexcase = 0; }
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var r = Array(s.length / 2);
for (var i = 0; i < s.length; i += 2) {
r[i / 2] = parseInt(s.substr(i, 2), 16);
}
return r;
}
function byteArrayToHexString(a) {
try { hexcase } catch (e) { hexcase = 0; }
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var r = "";
for (var i = 0; i < a.length; i++) {
var b = hex_tab.charAt((a[i] >> 4) & 0x0F) +
hex_tab.charAt(a[i] & 0x0F);
r += b;
}
return r;
}
我无法解密字符串,'结果'总是一个空数组,我无法弄明白。谢谢你的帮助!