A Java programme that uses the Vigenere (Vigenère) Cypher (or Cipher) to encrypt or decrypt a message.
The main elements are:
key: a key chosen by the user
message: the message to be encrypted or decrypted
cypher: a string of letters which have replaced the original letters of the message
The encryption/decryption happens with the use of a matrix referred to as the Tabula Recta. This matrix contains 26 rows of letters. Each row contains all letters of the alphabet with the first row containing the alphabet starting with the letter A and each subsequent row shifted cyclically to the left thus containing all possible 26 Caesar ciphers.
| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
| B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A |
| C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B |
| D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C |
| E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D |
| F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E |
| G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F |
| H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G |
| I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H |
| J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I |
| K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J |
| L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K |
| M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L |
| N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M |
| O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N |
| P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O |
| Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P |
| R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q |
| S | T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R |
| T | U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S |
| U | V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T |
| V | W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U |
| W | X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V |
| X | Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W |
| Y | Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X |
| Z | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y |
For example if the user inputs the following:
key: ORAK
message: AVONISEVIL (spaces are not used in the message)
to encode the message the key must first be repeated to the length of the message. The message (AVONISEVIL) is 10 characters long and the key (ORAK) is 4 characters long so it will become ORAKORAKOR. In the event that the message is shorter than the key then only the number of letters required will be used to encrypt the message.
Next using the key, message and the tabula recta the letter at the intersection of the each key and message letter is identified to create the cypher. So the intersection of O (ORAKORAKOR) and A (AVONISEVIL) points to the letter O and so this is the first letter of the cypher. The whole itteration goes as follows:
| KEY | MESSAGE | INTERSECTION LETTER |
| ORAKORAKOR | AVONISEVIL | O |
| ORAKORAKOR | AVONISEVIL | M |
| ORAKORAKOR | AVONISEVIL | O |
| ORAKORAKOR | AVONISEVIL | X |
| ORAKORAKOR | AVONISEVIL | W |
| ORAKORAKOR | AVONISEVIL | J |
| ORAKORAKOR | AVONISEVIL | E |
| ORAKORAKOR | AVONISEVIL | F |
| ORAKORAKOR | AVONISEVIL | W |
| ORAKORAKOR | AVONISEVIL | C |
Therefore the cypher is: OMOXWJEFWC
If instead we have the key and the cypher and wish to decode the cypher to get the message then we use the Tabula Recta again starting with the row where the letter of the key is located first and then we move across the row to find the relevant letter of the cypher, this is the intersection with the y axis pointing to the relevant letter of the decyphered message.
The programme could be modified to read in the message from a file and output the message to a file. Error checks on user input with relevant messages could also be implemented.- Isn't there a simpler way to code this?. In short, yes. There's probably many different/easier/harder ways to code this, this is my take on it.
- Isn't it cipher and not cypher? The answer, as far as I can tell, is that both spellings are correct with cypher being British English.
- Who (or what) are Orak and Avon? If you like vintage sci-fi TV shows you're in for a treat. Search for Blake's 7.