使用使用 SJCL 的 RNCryptor。我正在尝试解密十六进制消息,但是在使用 CBC 模式时,事情变得很奇怪。显然,在使用 CBC 时必须声明一个当心声明,我得到一个错误。
function KeyForPassword(password, salt) {
console.log("Creating key...");
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);
};
function decrypt(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);
var hmac_salt = sjcl.bitArray.bitSlice(message, 10 * 8, 18 * 8);
var hmac_key = _this.KeyForPassword(password, hmac_salt);
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));
// .equal is of consistent time
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 = sjcl.mode.cbc.decrypt(aes, ciphertext, iv);
return decrypted.toString(CryptoJS.enc.Utf8);
};
在盐、密钥和哈希方面,一切都与 Python 端的加密相匹配。但我得到这个错误:
TypeError: Cannot read property 'CBC mode is dangerous because it doesn't protect message integrity.' of undefined
我认为该方法已被弃用,因此我尝试使用此 CryptoJS 方法:
var decrypted = CryptoJS.AES.decrypt(ciphertext, encryption_key, {iv:iv});
这只是返回一个空白字符串。
我觉得我真的很接近,只是在最后一部分需要一些帮助,谢谢。