Skip to content

Instantly share code, notes, and snippets.

@anbinh
Forked from neizod/LICENSE.txt
Last active December 15, 2015 14:29
Show Gist options
  • Save anbinh/5275185 to your computer and use it in GitHub Desktop.
Save anbinh/5275185 to your computer and use it in GitHub Desktop.
The Substitution Cipher
function(
t, // input text string
k, // input key string
d, // direction
// placeholder
i, // iterator
o, // output
){
o=""; // set output to empty string.
for(i in t) // loop through all input string.
o+=d? // check if encrypt or decrypt? and map string <-> key.
k[t.charCodeAt(i)-97]:
String.fromCharCode(k.indexOf(t[i])+97);
return o
}
function(t,k,d,i,o){o="";for(i in t)o+=d?k[t.charCodeAt(i)-97]:String.fromCharCode(k.indexOf(t[i])+97);return o}
{
"name": "substitutionCipher",
"description": "this will encrypt/decrypt your text using the substitution method.",
"keywords": [
"substitution ",
"cipher",
"encryption",
"decryption",
"string"
]
}
<!DOCTYPE html>
<title>The Substitution Cipher</title>
<div>Expected value: <b>zebrascdfghijklmnopqtuvwxy, siaazqlkbavazoarfpbluaoar, ulhglwlfdcrrmpelbcdliuci</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var subst = function(t,k,d,i,o){o="";for(i in t)o+=d?k[t.charCodeAt(i)-97]:String.fromCharCode(k.indexOf(t[i])+97);return o}
var k1="zebrascdfghijklmnopqtuvwxy";
var k2="iveflntwrdgombxypsujhackzq";
document.getElementById( "ret" ).innerHTML = [subst("abcdefghijklmnopqrstuvwxyz", k1, 1),
subst("fleeatoncewearediscovered", k1, 1),
subst("howtoconfessmyloveforher", k2, 0)];
</script>
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Nattawut Phetmak <http://about.me/neizod>
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment