Skip to content

Instantly share code, notes, and snippets.

@neizod
Forked from 140bytes/LICENSE.txt
Created October 4, 2011 16:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neizod/1262123 to your computer and use it in GitHub Desktop.
Save neizod/1262123 to your computer and use it in GitHub Desktop.
Affine Cipher

The Affine Cipher

139b. Encrypt and decrypt text using affine cipher method.

How to Use

Invoke function using 4 arguments.

1st arg (string) ... Input pain/cipher text. Its can contain only English alphabet [a..z], no punctuation.
2nd arg (number) ... The multiplication number. Only [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25] are valid.
3rd arg (number) ... The key number. Range [0..25].
4th arg (number) ... Direction of encrypt/decrypt. 1 for encryption and 0 for decryption.

Let's Talk

By studying @wrayal's Vigenère Cipher. I decide to implement the popular well-known Caesar cipher again. But then I realized that Caesar cipher is just one special case from Vigenère cipher. That's mean I done nothing new! So I rewrite this function in Affine cipher. Which still can be use as the Caesar cipher by setting the 2nd arg = 1, e.g. function("texthere", 1, 4, 1).

function(
t, // input text string.
m, // input multiplication number.
k, // input key number.
d, // direction.
// placeholder.
a // current alphabet
){
for(var x=i=o=""; // init counter -> i, outtext -> o, and inverse multiplier -> x.
a=t.charCodeAt(i++)+7; // grap char at i, parse it to number. and loop through text.
o+=String.fromCharCode(( // parse char from number after finished this below steps.
(d?m:x)*a+ // multiple a with the right multiplier.
(d?1:-x)*k) // multiple k with the right multiplier. then add to a.
%26+97)) // parse base-26 number back to ascii code. (finished)
while(m*++x%26-1); // find inverse multiplier and store them as x.
return o
}
function(t,m,k,d,a){for(var x=i=o="";a=t.charCodeAt(i++)+7;o+=String.fromCharCode(((d?m:x)*a+(d?1:-x)*k)%26+97))while(m*++x%26-1);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": "affineCipher",
"description": "this will encrypt/decrypt your text using the affine method.",
"keywords": [
"affine",
"cipher",
"encryption",
"decryption",
"string"
]
}
<!DOCTYPE html>
<title>The Affine Cipher</title>
<div>Expected value: <b>insxchmrwbglqvafkpuzejotyd, exxegoexsrgi, iminlovewithmybestfriend</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 affine = function(t,m,k,d,a){for(var x=i=o="";a=t.charCodeAt(i++)+7;o+=String.fromCharCode(((d?m:x)*a+(d?1:-x)*k)%26+97))while(m*++x%26-1);return o}
document.getElementById( "ret" ).innerHTML = [affine("abcdefghijklmnopqrstuvwxyz", 5, 8, 1),
affine("attackatonce", 1, 4, 1),
affine("lnlugbyjflkentojdkqwljuc", 7, 7, 0)];
</script>
@Prinzhorn
Copy link

i and o are leaking the global scope.

@neizod
Copy link
Author

neizod commented Oct 4, 2011

@Prinzhorn need var for them? (i don't quite understand the concept of global scope very much).

@Prinzhorn
Copy link

If you just "use" a variable (i.e. assign a value to it) without declaring it first, it implicitly is global (i.e. it belongs to the outer most scope, which is in this case the window scope).
That means, after calling your function, we can get the value of "i" in the outer scope. Or even worse, if we would have a variable named "i" in outer scope, you would overwrite it.

Declaring them with the var keyword (inside you function) would a valid method. But you could save yourself two bytes, just add them as parameters to your function head. The caller doesn't need to supply them, but you can then use them inside your function just as any other variable.

function(t,k,d,i,o)

@neizod
Copy link
Author

neizod commented Oct 4, 2011

wow! thanks very much for the explanation. i just know that javascript can declare parameter at function head.

@tsaniel
Copy link

tsaniel commented Oct 5, 2011

Save 13 bytes.
function(t,k,d,i,o,a){for(i=o="";a=t.charCodeAt(i++);o+=String.fromCharCode((a-71+d*(k.charCodeAt()-96))%26+97));return o}

@neizod
Copy link
Author

neizod commented Oct 5, 2011

@tsaniel very thanks. ^^

@nikola
Copy link

nikola commented Oct 5, 2011

Save another 2 bytes with something like:

for(var i=o="",c="charCodeAt";i<t.length;o+=String.fromCharCode((((t[c](i++)-97)+26+d*(k[c]()-96))%26)+97));

@neizod
Copy link
Author

neizod commented Oct 5, 2011

@nikola that's a brilliance technique! now i can handle a.longObjectAttribute much easier!

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