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
  • Save atomicframeworks/8108836 to your computer and use it in GitHub Desktop.
Save atomicframeworks/8108836 to your computer and use it in GitHub Desktop.
ROT13 in 113 bytes

ROT13

A ROT13 encrypter/decrypter implementation in 113 bytes.

"ROT13... is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher, developed in ancient Rome. ... ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption."

Wikipedia

Source

function r(a,b){return++b?String.fromCharCode((a<"["?91:123)>(a=a.charCodeAt()+13)?a:a-26):a.replace(/[A-z]/g,r)}

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 r(
a, // Input text
b // Will be undefined at first ++undefined results in NaN which is falsy. Subsequently b will be the index of match which will test true so the character will be rotated
){
return ++b ? // When used as a string replacement function it will return the new charater due to the fact that b is being set
String.fromCharCode( // Create a string from the rotated value
(a < "[" ? 91 : 123 ) // If the character is uppercase (character code value < ]) set the character limit to 91 otherwise set the limit to 123
> ( a=a.charCodeAt() + 13 ) ? a : a - 26) // If rotated character value is greater than the limit (91 or 123) loop to the beginning by subtracting 26
: a.replace(/[A-z]/g, r) // Replace all A-Z and a-z characters using this function (r)
}
function r(a,b){return++b?String.fromCharCode((a<"["?91:123)>(a=a.charCodeAt()+13)?a:a-26):a.replace(/[A-z]/g,r)}
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": "rot13",
"description": "This will encrypt or decrypt your text using the ROT13 cipher",
"keywords": [
"rot13",
"encryption",
"decryption",
"cipher",
"string"
]
}
<!DOCTYPE html>
<title>ROT13</title>
<div>Expected encrypted value: <b>nopqrstuvxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM</b></div>
<div>Actual encrypted value: <b id="encrypted"></b></div>
<div>Expected decrypted value: <b>abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ</b></div>
<div>Decrypted value: <b id="decrypted"></b></div>
<script>
function r(a,b){return++b?String.fromCharCode((a=a.charCodeAt()+47,a>126?a-94:a)):a.replace(/[^ ]/g,r)};
document.getElementById( "encrypted" ).innerHTML = r('abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
document.getElementById( "decrypted" ).innerHTML = r('nopqrstuvxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment