Skip to content

Instantly share code, notes, and snippets.

@Wostafa
Forked from donma/javascript.xor.encryption.js
Last active April 30, 2023 18:00
Show Gist options
  • Select an option

  • Save Wostafa/c3732ea41ce5f5184fdff5a01d66e205 to your computer and use it in GitHub Desktop.

Select an option

Save Wostafa/c3732ea41ce5f5184fdff5a01d66e205 to your computer and use it in GitHub Desktop.
javascript.xor.encryption.js
function EncodeB64(str) {
return window.btoa(unescape(encodeURIComponent(str)));
}
function DecodeB64(str) {
return decodeURIComponent(escape(window.atob(str)));
}
function XorEncrypt(data, salt) {
data = EncodeB64(data);
let salts = Array.from(salt);
let output = [];
for (let i = 0; i < data.length; i++) {
let charCode = data.charCodeAt(i) ^ salts[i % salts.length].charCodeAt(0);
output.push(String.fromCharCode(charCode));
}
let result = output.join("");
return EncodeB64(result);
}
function XorDecrypt(data, salt) {
data = DecodeB64(data);
let salts = Array.from(salt);
let output = [];
for (let i = 0; i < data.length; i++) {
let charCode = data.charCodeAt(i) ^ salts[i % salts.length].charCodeAt(0);
output.push(String.fromCharCode(charCode));
}
let res = output.join("");
return DecodeB64(res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment