Skip to content

Instantly share code, notes, and snippets.

@danibram
Created May 17, 2022 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danibram/4cc4e77d58d5d67c8d9c51781004f437 to your computer and use it in GitHub Desktop.
Save danibram/4cc4e77d58d5d67c8d9c51781004f437 to your computer and use it in GitHub Desktop.
XOR encode decode browser
const s1 = ``
const s2 = ``
function utf8_to_b64( str ) {
return btoa(unescape(encodeURIComponent( str )));
}
function b64_to_utf8( str ) {
return decodeURIComponent(escape(atob( str )));
}
function xor (input, key) {
let output = ''
for (let i = 0; i < input.length; i++) {
output += String.fromCharCode(input.charCodeAt(i) ^ key.charCodeAt(i % key.length))
}
return output
}
function encode(text) {
const messageDecipher = xor(xor(utf8_to_b64(text), s1), s2)
return utf8_to_b64(messageDecipher)
}
function decode(text) {
const messageDecipher = b64_to_utf8(xor(xor(b64_to_utf8(text), s2), s1))
return messageDecipher
}
let text = "wedc"
let enc = encode(text)
console.log(">>>",enc)
console.log("<<<",decode(enc))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment