尝试使用使用SJCL的RNCryptor-js解密AES。在记录完每一端的所有步骤后,(另一端是RNCryptor-python)键、盐类、HMAC散列,所有的内容都匹配起来。但当我走到最后一步:
var aes = new sjcl.cipher.aes(encryption_key);
sjcl.beware["CBC mode is dangerous because it doesn't protect message integrity."]()
var decrypted = aes.decrypt(ciphertext, iv);我知道错误:
sjcl.exception.invalid {toString: function, message: "invalid aes block size"}以下是完整的代码:
PBKDF2:
this.KeyForPassword = function(password, salt) {
var hmacSHA256 = function (password) {
var hasher = new sjcl.misc.hmac(password, sjcl.hash.sha256);
this.encrypt = function () {
return hasher.encrypt.apply(hasher, arguments);
};
};
return sjcl.misc.pbkdf2(password, salt, 10000, 32 * 8, hmacSHA256);
};解密(接受十六进制输入):
this.decrypt = function(password, message, options) {
message = sjcl.codec.hex.toBits(message);
options = options || {};
var version = sjcl.bitArray.extract(message, 0 * 8, 8);
var options = sjcl.bitArray.extract(message, 1 * 8, 8);
var encryption_salt = sjcl.bitArray.bitSlice(message, 2 * 8, 10 * 8);
var encryption_key = _this.KeyForPassword(password, encryption_salt, "decryption");
var hmac_salt = sjcl.bitArray.bitSlice(message, 10 * 8, 18 * 8);
var hmac_key = _this.KeyForPassword(password, hmac_salt, "decryption");
var iv = sjcl.bitArray.bitSlice(message, 18 * 8, 34 * 8);
var ciphertext_end = sjcl.bitArray.bitLength(message) - (32 * 8);
var ciphertext = sjcl.bitArray.bitSlice(message, 34 * 8, ciphertext_end);
var hmac = sjcl.bitArray.bitSlice(message, ciphertext_end);
var expected_hmac = new sjcl.misc.hmac(hmac_key).encrypt(sjcl.bitArray.bitSlice(message, 0, ciphertext_end));
if (! sjcl.bitArray.equal(hmac, expected_hmac)) {
throw new sjcl.exception.corrupt("HMAC mismatch or bad password.");
}
var aes = new sjcl.cipher.aes(encryption_key);
sjcl.beware["CBC mode is dangerous because it doesn't protect message integrity."]()
var decrypted = aes.decrypt(ciphertext, iv);
return decrypted;
}在定义decrypted的第二条到最后一条语句上抛出错误。
我查看了sjcl异常,它看起来像是在寻找长度为4的输入,我猜它是一个WordArray。我只是不知道如何获得有效的输入。就像我说的,密文,iv,hmac标签,盐类都在javascript的末端被正确的切片。可能只是编码问题。
这个错误似乎也只发生在json (格式:{“key”:“value”})上,当我尝试"Hello,world“时,我得到了一个没有错误的4字数组。
有什么建议吗?
发布于 2015-10-02 00:19:50
https://stackoverflow.com/questions/32662918
复制相似问题