Skip to content

Instantly share code, notes, and snippets.

@neizod
Forked from 140bytes/LICENSE.txt
Created October 6, 2011 15:25
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 neizod/1267678 to your computer and use it in GitHub Desktop.
Save neizod/1267678 to your computer and use it in GitHub Desktop.
Substitution Cipher

The Substitution Cipher

112b. Encrypt and decrypt text using substitution cipher method.

How to Use

Invoke function using 3 arguments.

1st arg (string) ... Input pain/cipher text. Its can contain only English alphabet [a..z], no punctuation.
2nd arg (number) ... The key text. Contain each of a lower case English alphabet [a..z]{26}.
3rd arg (number) ... Direction of encrypt/decrypt. 1 for encryption and 0 for decryption.

Let's Talk

While Affine Cipher is a very-hard-to-understand, nearly-mind-explosive-on-implement; Substitution Cipher provide a much-easier-to-learn, no-queer-coding-require. Above of all, substitution cipher had fully cover on what affine cipher had already done!

Nevertheless, I love affine cipher the most! Because its remind me how wonderful mathematics is.

Credit

124b -> 112b  williammalo
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}
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.
{
"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>
@williammalo
Copy link

You can make it smaller.
original:
function(t,k,d){for(var i=0,o="";i<t.length;i++)o+=d?k[t.charCodeAt(i)-97]:String.fromCharCode(k.indexOf(t[i])+97);return o}
smaller version:
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}

@neizod
Copy link
Author

neizod commented Mar 26, 2012

@williammalo don't even think of for-each, thx you.

@williammalo
Copy link

@neizod yw :)

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