vigenere_cipher
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_cipher(in,key,mode)
**
** Arguments:
** in input, string
** key enciphering key, string
** mode 0 = decipher, 1 = encipher
**
** Returns:
** a string, deciphered or enciphered
** using a Vigenere style cipher
**
** 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;
inLen = string_length(in);
keyLen = string_length(key);
for (pos=0;pos<inLen;pos+=1) {
inChar = string_char_at(in,pos+1);
keyChar = string_char_at(key,(pos mod keyLen)+1);
inVal = ord(inChar);
keyVal = ord(keyChar);
if (mode) {
outVal = (inVal + keyVal) mod 256;
}else{
outVal = (256 + inVal - keyVal) mod 256;
}
outChar = chr(outVal);
out = out + outChar;
}
return out;
}
** Usage:
** vigenere_cipher(in,key,mode)
**
** Arguments:
** in input, string
** key enciphering key, string
** mode 0 = decipher, 1 = encipher
**
** Returns:
** a string, deciphered or enciphered
** using a Vigenere style cipher
**
** 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;
inLen = string_length(in);
keyLen = string_length(key);
for (pos=0;pos<inLen;pos+=1) {
inChar = string_char_at(in,pos+1);
keyChar = string_char_at(key,(pos mod keyLen)+1);
inVal = ord(inChar);
keyVal = ord(keyChar);
if (mode) {
outVal = (inVal + keyVal) mod 256;
}else{
outVal = (256 + inVal - keyVal) mod 256;
}
outChar = chr(outVal);
out = out + outChar;
}
return out;
}
[Please Login]
Projects: 3
Contributor: xot
comments powered by Disqus

Related: