开发者

Unable to decrypt aes ciphertext with slowaes (javascript)

I have a cipher text I encoded with the AesManaged .Net classes.

plaintext: "string"

password: "password"

this is the c# code I use to encrypt:

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 = ne开发者_如何转开发w 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);
    }

When I decrypt with slowAES and pbkdf2 the decrypted string is not correct.

The cipher text (base64) is : "MJ8gxKkUKU/S+CgLPf8Sjg=="

the decrypted text returned is: "supkj`"

My JavaScript is:

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

Can anyone see what I'm doing wrong?

Many Thanks


Ok do I get points for answering my own question?

I spotted that in the JavaScript I was passing the IV as a HEX string instead of a char array.

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

should be

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜