vigenere_ascii

Vigenère Cipher

 G  A  M  E  M  A  K  E  R
K E Y K E Y K E Y
- - - - - - - - -
R F L P R Z V J Q

7 1 13 5 13 1 11 5 18
11 5 25 11 5 25 11 5 25
-- -- -- -- -- -- -- -- --
18 6 12 16 18 26 22 10 17

This is based on the classic Vigenère cipher. Using a repeating key, the letters in the target text are shifted in the alphabet by varying amounts. While effective, this is not a strong encryption. A determined person would likely be able to crack it. The cipher becomes more effective as the length of the key increases. The biggest weakness with Vigenère ciphers is in cases where many spaces or zero-value characters exist in the source data. When that happens the enciphering key can be exposed. Using long keys which appear random can help hide the exposure.

Downloadvigenere_ascii(in,key,mode)   Returns a copy of string in deciphered or enciphered with using string key. Set mode to 0 to decipher, or 1 to encipher. This cipher is meant to work with printable ASCII characters only. For a more versatile version check vigenere_cipher().
/*
**  Usage:
**      vigenere_ascii(in,key,mode)
**
**  Arguments:
**      in      input, string
**      key     enciphering key, string
**      mode    0 = decipher, 1 = encipher
**
**  Returns:
**      a string, deciphered or enciphered using
**      a simple Vigenere style cipher
**
**  Notes:
**      filters out non-printable characters
**
**  GMLscripts.com
*/

{
    var in,key,mode,out;
    in = argument0;
    key = argument1;
    mode = argument2;
    out = "";
    var inLen,keyLen,pos,inChar,keyChar,outChar;
    var inVal,keyVal,outVal,loVal,hiVal,span;
    inLen = string_length(in);
    keyLen = string_length(key);
    loVal = 32;
    hiVal = 126;
    span = (hiVal - loVal) + 1;
    for (pos=0;pos<inLen;pos+=1) {
        inChar = string_char_at(in,pos+1);
        keyChar = string_char_at(key,(pos mod keyLen)+1);
        inVal = min(max(loVal,ord(inChar)),hiVal)-loVal;
        keyVal = min(max(loVal,ord(keyChar)),hiVal)-loVal;
        if (mode) {
            outVal = ((inVal + keyVal) mod span) + loVal;
        }else{
            outVal = ((span + inVal - keyVal) mod span) + loVal;
        }
        outChar = chr(outVal);
        out = out + outChar;
    }
    return out;
}

Click if you've used this script[Please Login]
Projects: 0

 Contributor: xot


comments powered by Disqus