This doesn't really explain Vigenere well, since what's left out is where the key comes from:<p>* If key is the same length as plaintext, and truly random, that's a One-Time Pad.<p>* If the message is appended to the seed key, that's an Autokey Cipher.<p>* If the key is repeated (what the PHP does but not made clear), that's classical Vigenere.<p>It also doesn't describe breaking them:<p>* OTPs are unbreakable.<p>* You can break Autokey by using crib words (e.g. "the") at different locations of the ciphertext, selecting the most phonetically plausible and working backwards to recover the earlier plaintext.<p>* Vigenere can be attacked by trying the same shift for every Nth letter, or using cribs.
Quote: "Our decryption function is basically the same, but in reverse" and follows with a full decrypt function.<p>A very simple optimization is to use encrypt function to decrypt the encrypted message by providing the "inverse key" as parameter, which is a 2 line function altogether. Implementation of said implementation is left as an exercise for the reader.
For a course several years ago, I wrote a compact/efficient implementation of a Vigenere cipher:<p><pre><code> private static char encipherCharacter(char message_char, char key_char){
return (char)(((int)(message_char) + (int)(key_char) - 194)%26 + 97);
}
public static String encipher(String plaintext, String key){
String ciphertext = "";
for(int i = 0; i < plaintext.length(); i++)
ciphertext += encipherCharacter(plaintext.charAt(i), key.charAt((i % key.length())));
return ciphertext;
}
</code></pre>
I'd love to see other implementations, if anyone has some to share.<p><a href="https://gist.github.com/ctrezevant/8ca9e163d702af245ded35d97f522ec9" rel="nofollow">https://gist.github.com/ctrezevant/8ca9e163d702af245ded35d97...</a>
This reminds of the times when I was at school and was solving the weekly online challenges for National Cipher Challenge (UK). My program which cracked Vigenere cipher was one of my first projects when I was learning programming. My solution depended on the fact that I knew a few words which will occur in the plaintext, and I would just try these words in all possible positions in the text and get the possible keys.
It's not a very nice implementation. It creates a long key as long as the message by appending the key to itself as many times as necessary.<p>Could have just used a mod operator to cycle around the key.