首页 > 解决方案 > Javascript中的XOR密码解密

问题描述

我有一个必须解码的 base64 字符串,即AwELBwc=. 使用给我的 XOR 密码密钥,即26364,我必须对字符串进行解码以获得一个我已经知道的数字 ( 7813)。

这将如何在 Javascript 中完成,您在其中获取一个 base64 编码的字符串,通过具有已知密钥的 XOR 密码运行它,然后输出结果?

标签: javascriptxor

解决方案


这段代码应该做你想做的事:

function base64ToArray(base64String) {
    var bstr = atob(base64String);
    var bytes = [];
    for (var i = 0; i < bstr.length; i++) {
        bytes.push(bstr.charCodeAt(i));
    }
    return bytes;
}

let key = [2,6,3,6,4];
let cipherTextBase64 = 'AwELBwc=';
let cipherTextBytes = base64ToArray(cipherTextBase64);

let result = key.map((value,index) => {
    return value ^ cipherTextBytes[index];
});

document.getElementById('output').innerHTML = 'Result: ' + result.join();


console.log('Result: ', result);
<div id="output">
</div>


推荐阅读