Vignere Cipher

Background

The Vignere cipher is "related" to the Caesar cipher, but because it uses a number of substitutions (2), it is technically a "POLYALPHABETIC Cipher.

The "key" is a phrase that both the sender and receiver knows.

The "Tableau" (2 character lookup table) that a Vignere cipher uses can be envisaged as follows:
Vignere Tableau
As keylength goes up, the possible number of possible substitutions needed to be brute-force tried skyrockets:
Vignere Tableau
stolen from Tutorials Point

Code

Let's try it out:

Key:
Message:

Original: "'.$clearText. '"
Key: "'.$key.'"
Ciphertext: "' . $cryptText.'"

'; } ?>


Algorithm

#Thanks Greg Egan for the Pseud starter
BEGIN
	SET cryptText=''
	SET keyIndex = 0
	SET alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	INPUT mode
	INPUT upcase(message)
	INPUT upcase(key)
	keyLength = length of key

	FOR each char in message:
		IF char not in alphabet THEN
			cryptText += char
		ELSE
			letterPos = position of char in alphabet
 
			keyChar = key[keyIndex]
			keyPos = position of keyChar in alphabet

			IF mode = 'encrypt' THEN
				newIndex = (letterPos + keyPos) mod 26
			ELSE
				newIndex = (letterPos - keyPos) mod 26
			ENDIF

			keyIndex = (keyIndex + 1) % keyLength		
			cryptText += alphabet[newIndex]
		ENDIF
	ENDFOR

	PRINT cryptText
END