function xorEncrypt(plaintext, key) {
let encrypted = '';
for (let i = 0; i < plaintext.length; i++) {
encrypted += String.fromCharCode(plaintext.charCodeAt(i) ^ key.charCodeAt(i % key.length));
}
return btoa(encrypted);
}
let key = 'gotojiam';
let plaintext = 'https://www.baidu.com/';
let encrypted = xorEncrypt(plaintext, key);
console.log(encodeURIComponent(encrypted));
//DxsAHxlTTkIQGANBCAgICRJBFwAHRg==
// 解密
function xorDecrypt(encrypted, key) {
let decoded = atob(encrypted);
let decrypted = '';
for (let i = 0; i < decoded.length; i++) {
decrypted += String.fromCharCode(decoded.charCodeAt(i) ^ key.charCodeAt(i % key.length));
}
return decrypted;
}
let key = 'gotojiam';
let encrypted = 'DxsAHxlTTkIQGANBCAgICRJBFwAHRg==';
let decrypted = xorDecrypt(encrypted, key);
console.log(decrypted);
评论
发表评论