Skip to content

Instantly share code, notes, and snippets.

@atomicframeworks
Forked from 140bytes/LICENSE.txt
Last active January 1, 2016 08:19
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/8117695 to your computer and use it in GitHub Desktop.
Save atomicframeworks/8117695 to your computer and use it in GitHub Desktop.
A Caesar cipher in 133 bytes

Caesar Cipher

A Caesar cipher in 133 bytes.

"In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence."

Wikipedia

Source

var caesar = function(a,b,c){return a.replace(/[A-z]/g,function(a){c=(a=a.charCodeAt())>96?97:65;return String.fromCharCode((a+b%26+26-c)%26+c)})}

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, // Input text
b, // Number of places to shift each character
c // Placeholder for character offset
){
return a.replace(/[A-z]/g,function(a) { // Replace all A-Z and a-z characters function (r)
c = (a = a.charCodeAt()) > 96 ? 97 : 65; // Set a to the character code. Set the character offset based on it
return String.fromCharCode( // Create a string from the rotated value
(a + b % 26 + 26 - c) % 26 + c // Shift the character while making sure it is in the character set
)
})
}
function(a,b,c){return a.replace(/[A-z]/g,function(a){c=(a=a.charCodeAt())>96?97:65;return String.fromCharCode((a+b%26+26-c)%26+c)})}
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": "caesarCipher",
"description": "A Caesar cipher aka shift cipher",
"keywords": [
"caesar",
"cipher",
"string",
"encrypt",
"decrypt"
]
}
<!DOCTYPE html>
<title>Caesar Cipher</title>
<div>Expected encrypted value: <b>xyzabcdefghijklmnopqrstuvwXYZABCDEFGHIJKLMNOPQRSTUVW</b></div>
<div>Actual encrypted value: <b id="encrypted"></b></div>
<div>Expected decrypted value: <b>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ</b></div>
<div>Decrypted value: <b id="decrypted"></b></div>
<div>Expected test high and low multiple value: <b>true</b></div>
<div>Test high and low multiple value: <b id="multiple"></b></div>
<script>
var caesar = function(a,b){return a.replace(/./g,function(a){return String.fromCharCode(a.charCodeAt()^b)})}
document.getElementById( "encrypted" ).innerHTML = caesar('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 23);
document.getElementById( "decrypted" ).innerHTML = caesar('xyzabcdefghijklmnopqrstuvwXYZABCDEFGHIJKLMNOPQRSTUVW', -23);
// Rotating by any positive or negative multiple of 26 should equal the original text.
document.getElementById( "multiple" ).innerHTML = (
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' === caesar('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', -26 * 12345) &&
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' === caesar('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 26 * 54321)
);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment