OBJECTIVE: make a
tabula recta. Optionally use it to cipher or decipher using it as a
Vigenere Table
function vigenere(alphabet, keyword, textToCipher, decrypt){
if(typeof alphabet!="string" || !alphabet){alphabet='abcdefghijklmnopqrstuvwxyz';};
var output=[];
for(var i=0; i<alphabet.length; i++){ output[++output.length-1]=alphabet.substring(i)+alphabet.substring(0, i); }
if(typeof keyword=="string" && typeof textToCipher=="string" &&
keyword && textToCipher){
var outputText=''; keyword=keyword.split(''); textToCipher=textToCipher.split('');
for(var t=0, k=0; t<textToCipher.length; t++, k++){
if(k>=keyword.length){k=0;};
var row=alphabet.indexOf( keyword[k].toLowerCase() );
var col=textToCipher[t].toLowerCase();
if(alphabet.indexOf(col)<0 || row<0){return false;};
if(!decrypt){ outputText+=output[row].charAt( alphabet.indexOf(col) ); }
else{ outputText+=alphabet.charAt( output[row].indexOf(col) ); };
}
return outputText;
}
return output;
/*keep this comment to reuse freely
http://www.fullposter.com/?1 */}Remove colors
INPUTS: first input a String. If passed as an
empty string, defaults to a standard alphabet
exclusive of white space char.
If you want to pass an alphabet yourself you have to follow these rules: only
lowercase chars,
no duplicates present. The alphabet may include also numbers and punctuations if you prefer so.
If you want the function not to be case insensitive, just remove from the codes all the strings
.toLowerCase() and include in the alphabet both the uppercase and lowercase versions of any char.
Second input a String. If you pass it, the function instead of returning a tabula recta will return a string. If passed, this string must be the
keyword you want to use to produce the cipher. To know what it means please be sure you know how a
Vigenere Table works.
If passed, any char present in this string
must be present in the alphabet too.
Third input a String. If you pass it, the function instead of returning a tabula recta will return a string. If passed, this string must be the
plain text you want to use to cipher. To know what it means please be sure you know how a
Vigenere Table works.
If passed, any char present in this string
must be present in the alphabet too.
Last input if zero, the function encrypts. If number 1, it decrypts.
In
both cases the second argument is the keyword. The third argument instead, if you want to crypt, must be the plain text, or must be the cipher if you want to decrypt.
Encryption and decryption work correctly only if you are using the
same tabula recta.
RETURNS: array of strings if no argument or only one argument is present, otherwise a string.
If the inputs are wrong or the chars present in either of the second and third argument (if present) cannot be found in the alphabet, it returns boolean
false.
CAVEATS: please refer to the previous inputs description.
Vigenere tables are a cipher that
can be broken by cryptoanalysis (Kasiski examination
* and Babbage
* ).
Shuffling the rows may increase cipher safety.
Using a
one time pad may make the cipher truly safe.
Never use as keyword a one string long keyword whose unique char is the first one present in the alphabet: that ciphers the plain text as plain text.
By default the alphabet does not include an
empty space as a char. If your input text to cipher/decipher is inclusive of white spaces, you must include one white space also in the input alphabet (either directly in the code, or passing your alphabet of choice as the function first argument).
Example:
alert( vigenere().join("\n") );
alert( vigenere('', 'lemonlemonle', 'attackatdawn', 0) );
alert( vigenere('', 'lemonlemonle', 'lxfopvefrnhr', 1) );
Remove colors