[英]XOR cipher decryption in Javascript
I have a base64 string that I have to decode, which is AwELBwc=
. 我有一个必须解码的base64字符串,即
AwELBwc=
。 Using the XOR cipher key given to me, which is 26364
, I have to decode the string to get a number, which I already know ( 7813
). 使用给我的XOR密码密钥
26364
,我必须对字符串进行解码以获取一个我已经知道的数字( 7813
)。
How would this be done in Javascript, where you take a base64-encoded string, run it through a XOR cipher with a known key, then output the result? 如何在Javascript中完成此操作,在Javascript中,您使用base64编码的字符串,通过具有已知密钥的XOR密码运行它,然后输出结果?
This code should do what you want: 此代码应执行您想要的操作:
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>
The function below is inspired by the xor character by character method that Terry Lennox used in his answer. 下面的功能是由Terry Lennox在回答中使用的异或字符方法启发的。
function xorDecryptBase64(base64string, cipher) { let keys = cipher.toString().split(''); let charCodes = atob(base64string).split('') .map(function(c){return c.charCodeAt(0)}); return Number(charCodes .map(function(v,i){return v ^ keys[i%keys.length]}) .slice(1).join('') ); } console.log(xorDecryptBase64('AwELBwc=', 26364)); console.log(xorDecryptBase64('AwAFAAIEAA==', 26364));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.