Skip to content

Instantly share code, notes, and snippets.

@atomicframeworks
Forked from 140bytes/LICENSE.txt
Last active January 1, 2016 06:59
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 atomicframeworks/8108345 to your computer and use it in GitHub Desktop.
Save atomicframeworks/8108345 to your computer and use it in GitHub Desktop.
A simple XOR cipher in 95 bytes

XOR Cipher

A simple XOR cipher in 95 bytes.

"In cryptography, the simple XOR cipher is a type of additive cipher... a string of text can be encrypted by applying the bitwise XOR operator to every character using a given key. To decrypt the output, merely reapplying the XOR function with the key will remove the cipher."

Wikipedia

Source

var xor = function(a,b){return a.replace(/./g,function(a){return String.fromCharCode(a.charCodeAt()^b)})}

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function(
a, // The input text
b // The encryption key
){
return a.replace(/./g, function(a){ // Replace every character
return String.fromCharCode(a.charCodeAt() ^ b) // Shift the character code with the key using XOR and create new character from the result
})
}
function(a,b){return a.replace(/./g,function(a){return String.fromCharCode(a.charCodeAt()^b)})}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 Kevin M. McGinty www.atomicframeworks.com
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "xorCipher",
"description": "A simple XOR cipher",
"keywords": [
"xor",
"string",
"cipher",
"encrypt",
"decrypt",
]
}
<!DOCTYPE html>
<title>XOR Cipher</title>
<div>Expected encrypted value: <b>Wkf#Rvj`h#Aqltm#El{#Ivnsp#Lufq#Wkf#Obyz#Gld-</b></div>
<div>Actual encrypted value: <b id="encrypted"></b></div>
<div>Expected decrypted value: <b>The Quick Brown Fox Jumps Over The Lazy Dog.</b></div>
<div>Decrypted value: <b id="decrypted"></b></div>
<script>
var xor = function(a,b){return a.replace(/./g,function(a){return String.fromCharCode(a.charCodeAt()^b)})}
document.getElementById( "encrypted" ).innerHTML = xor('The Quick Brown Fox Jumps Over The Lazy Dog.',3);
document.getElementById( "decrypted" ).innerHTML = xor('Wkf#Rvj`h#Aqltm#El{#Ivnsp#Lufq#Wkf#Obyz#Gld-',3);
</script>
@atk
Copy link

atk commented Dec 30, 2013

Recursive-replace approach in 83bytes:

function x(a,b,c){return c?String.fromCharCode(a.charCodeAt()^b):a.replace(/./g,x)}

@atomicframeworks
Copy link
Author

@atk
Doesn't the recursive function set b as the index of the replace each time? Meaning that each letter would be XOR'd by the index instead of the XOR key. So running x('hhhhhhhh',5) produces "hijklmno" because b is incremented each time. Instead the result should be "mmmmmmmm".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment