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.
/*
** 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;
}
** 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;
}
[Please Login]
Projects: 0
Contributor: xot
comments powered by Disqus

Related: