无法使用slowaes(javascript)解密aes密文

时间:2010-01-23 15:29:23

标签: c# .net javascript encryption aes

我有一个用AesManaged .Net类编码的密文。

明文:“string”

密码:“密码”

这是我用来加密的c#代码:

private AesManaged AESCipher;
    private String Password;

    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.KeySizeInBits/8);
    }

当我用slowAES和pbkdf2解密时,解密后的字符串不正确。

密文(base64)为:“MJ8gxKkUKU / S + CgLPf8Sjg ==”

返回的解密文本是:“supkj`”

我的JavaScript是:

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;
    }



    var mypbkdf2 = new PBKDF2("password", "saltsalt", 1000, 32);
    var newKey;
    var status_callback = function(percent_done) {
        document.getElementById("status").innerHTML = "Computed " + percent_done + "%"
    };
    var result_callback = function(key) {
        document.getElementById("status").innerHTML = "The derived key is: " + key
        newKey = key;

        var bytes = cryptoHelpers.base64.decode(document.getElementById("string").innerHTML);

        var result = slowAES.decrypt(bytes, 6, slowAES.modeOfOperation.CBC, hexStringToByteArray(key), 32, "000102030405060708090A0B0C0D0E0F")
        alert(byteArrayToAscii(result));


    };
    mypbkdf2.deriveKey(status_callback, result_callback);

谁能看到我做错了什么?

非常感谢

1 个答案:

答案 0 :(得分:1)

好的,我能回答自己的问题吗?

我发现在JavaScript中我将IV作为HEX字符串而不是char数组传递。

var result = slowAES.decrypt(bytes, 6, slowAES.modeOfOperation.CBC, hexStringToByteArray(key), 32, "000102030405060708090A0B0C0D0E0F")

应该是

var result = slowAES.decrypt(bytes, 6, slowAES.modeOfOperation.CBC, hexStringToByteArray(key), 32, hexStringToByteArray("000102030405060708090A0B0C0D0E0F"))