Skip to content

Instantly share code, notes, and snippets.

@atomicframeworks
Forked from 140bytes/LICENSE.txt
Last active February 4, 2023 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atomicframeworks/7981834 to your computer and use it in GitHub Desktop.
Save atomicframeworks/7981834 to your computer and use it in GitHub Desktop.
ROT47 in 103 bytes

ROT47

A ROT47 encrypter/decrypter implementation in 103 bytes.

"ROT47 is a derivative of ROT13 which, in addition to scrambling the basic letters, also treats numbers and common symbols. Instead of using the sequence A–Z as the alphabet, ROT47 uses a larger set of characters from the common character encoding known as ASCII. Specifically, the 7-bit printable characters, excluding space, from decimal 33 '!' through 126 '~', 94 in total, taken in the order of the numerical values of their ASCII codes, are rotated by 47 positions, without special consideration of case."

Wikipedia

Source

function r(a,b){return++b?String.fromCharCode((a=a.charCodeAt()+47,a>126?a-94:a)):a.replace(/[^ ]/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.

Thanks to

atk: 115b ~> 103b

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 ? // Will return a.replace when used by itself. When used as a replacement function it will return the new charater due to the fact that b is being set
String.fromCharCode((a=a.charCodeAt()+47, a > 126 ? a - 94 : a)) // Rotate character index by adding 47. If the result is > 126 (the max) loop to the beginning by subtracting 94
: a.replace(/[^ ]/g, r) // Replace everything but whitespace using this function (r)
}
function r(a,b){return++b?String.fromCharCode((a=a.charCodeAt()+47,a>126?a-94:a)):a.replace(/[^ ]/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": "rot47",
"description": "This will encrypt or decrypt your text using the ROT47 cipher",
"keywords": [
"rot47",
"encryption",
"decryption",
"cipher",
"string"
]
}
<!DOCTYPE html>
<title>ROT47</title>
<div>Expected encrypted value: <b>%96 "F:4< qC@H? u@I yF>AD ~G6C %96 {2KJ s@8]</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>
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('The Quick Brown Fox Jumps Over The Lazy Dog.');
document.getElementById( "decrypted" ).innerHTML = r('%96 "F:4< qC@H? u@I yF>AD ~G6C %96 {2KJ s@8]');
</script>
@atk
Copy link

atk commented Dec 16, 2013

Why not use a recursive replace approach? It's quite popular for string-related entries.

function r(a,b){return++b?String.fromCharCode((a=a.charCodeAt()+47,a>126?a-94:a)):a.replace(/[^ ]/g,r)}

@atomicframeworks
Copy link
Author

@atk I hadn't thought of it, but it makes perfect sense. Thanks!

@tillkruss
Copy link

@atomicframeworks: This is amazing, but doesn't work with non ascii characters such as: ö

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