在javascript以上的二进制字符串charcode 128

时间:2015-09-22 13:13:33

标签: javascript string binary cryptojs

我试图解密OpenSSL AES加密文件,为此我需要从文件的开头读取salt,并将其密码提供给CryptoJS openssl密钥派生函数以获取派生密钥和IV。

可悲的是,CryptoJS希望salt作为String,而JS中的String处理二进制数据不佳。 在128中,一个超过128的字符被解释为2位(参见:Trouble with binary string in javascript above character code 128):

// if you wanna test it, openssl command to generate a file from any file :
// openssl enc -aes-256-cbc -in file.txt -out file.enc -k password
fs.readFile('file.enc', function(err, data) {
    var Salted__ = data.toString("utf-8", 0, 8); // 'Salted__' prefix
    var salt = data.toString("hex", 8, 16); // the actual salt I want to give to CryptoJS
    // output in hex: 46d69efb7f57380b

    var buf = new Buffer(salt, "hex");
    console.log(buf);
    // output <Buffer 46 d6 9e fb 7f 57 38 0b>, exactly what I want, but it is a buffer not a String object.

    // the problem can be seen here:
    var buf2 = new Buffer(buf.toString());
    console.log(buf2);
    // output <Buffer 46 d6 9e ef bf bd 7f 57 38 0b>
    // as you can see, fb => ef bf bd

    // fun fact, when I try to do it manualy, I get another result (that seem more logical to me):
    var str = '';
    for (var i = 0; i < hex.length; i += 2)
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    console.log(new Buffer(str));
    // output <Buffer 46 c3 96 c2 9e c3 bb 7f 57 38 0b>
    // in this case (I've tryed it char by char to be sure):
    // d6 => c3 96
    // 9e => c2 9e
    // fb => c3 bb

    var derivedParams = CryptoJS.kdf.OpenSSL.execute(password, 256/32, 128/32, buf.toString());
    console.log(derivedParams.key.toString());
    console.log(derivedParams.iv.toString());
    // output a wrong key and a wrong iv (I know what I should get using openssl enc -P option)

});

如何理解JS如何使用二进制字符串的任何帮助或解释:)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案!

library(RcppRoll)
df <- as.data.frame(ret)
system.time(result<- sapply(df, function(x) roll_sd(x,n=251)))
#    user  system elapsed 
#    0.94    0.00    0.95