Cesar Cipher
One of the earliest cryptographic systems is the Cesar Cipher. In
this encoding scheme, each letter is shifted a set number of elements
forward in the alphabet, wrapping around past z. For instance, if
we use a shift of 3, then A would be relpaced with D, B would be
replaced with E, and so on. So a Cesar Cipher with a shift of 5
would be encoded using the table:
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
|
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
|
And the message:
ATTACK AT DAWN
would be encoded as:
FYYFHP FY IFBS
The problem with a Cesar Cipher is that the plaintext letter "e" will
always be represented by the same encrypted letter. Thus cesar
ciphers are very vunerable to letter frequency attacks -- we merely
look for the most frequent letter in the encrypted text, and try the
most frequent letters in the English (or, in Cesar's case, Latin)
language -- greatly cutting down the search space. 2000 years
ago, however, it seemed to work fine!
You can find an HTML cesar encoder/decoder online
here.
Autokey Ciphers
We can avoid frequency attacks by using a variant of the Cesar cipher
where the shift changes with each letter of the plaintext. In
Autokey ciphers, the message itself is used as a key to encrypt the
message. We will be using the Autokey variant of the Vigenere
cipher.
This is easiest to see with an example.
Let's say that we wanted to encrypt the message ATTACK AT
DAWN. First, we pick a word to use as our initial encryption key
-- let's use QUEEN. We line up QUEEN under ATTACK AT DAWN:
A
|
T
|
T
|
A
|
C
|
K
|
|
A
|
T
|
|
D
|
A
|
W
|
N
|
Q
|
U
|
E
|
E
|
N
|
|
|
|
|
|
|
|
|
|
Next, we fill up the rest of the letters under our message with the
message shifted over:
A
|
T
|
T
|
A
|
C
|
K
|
|
A
|
T
|
|
D
|
A
|
W
|
N
|
Q
|
U
|
E
|
E
|
N
|
A
|
|
T
|
T
|
|
A
|
C
|
K
|
A
|
Now we can use the Key to tell us which Cesar cypher to use for each
letter using the table below. For instance, to encode the first
"A" from ATTACK AT DAWN, we look at row "A" of the table below,
and column "Q", (or row "Q", column "A", the matrix is symmetric) to
get "Q". For the first "T", we use row "T",
column "U", to get N. The encrypted message is thus:
QNXEPK TM DCGN
|
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
|
A
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
H
|
I
|
J
|
K
|
L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
P
|
Q
|
R
|
S
|
T
|
U
|
V
|
W
|
X
|
Y
|
You can find a web based Vigenere Autokey encrypter/decrypter
here
Assignment
For Project 4, you will create a program that can encrypt using both a
simple Cesar cypher, and the Vigenere Autokey cipher described
above. Your
main program should take 5 inputs from the command line (in this order):
- Type of encryption (C for cesar, A for AutoKey)
- Key to use (number of the shift for Cesar, keyword for AutoKey)
- Either a E for encrypt, or a D for decrypt
- The filename to read from
- The filename to write to
So, to encrypt the file "foo.txt", writing to "foo.encrypt.txt",
using a Cesar cypher with an offset of 5, you would call your program
with the command line parameters:
C 5 E foo.txt
foo.encrypt.txt
To decrypt this file, saving the result to bar.txt, you would use the
parameters:
C 5 D foo.encrypt.txt
bar.txt
Capitalization, Non-letter characters
For both the Cesar Cipher and the Autokey cipher, your project should
preserve capitalization, and leave non-letter characters
unchanged. For example, if you use a Cesar Cipher with an offset
of 1, the message
One if by LAND,
Two if by SEA!
Should be encrypted as:
Pof jg cz MBOE,
Uxp jg cz TFB!
(Of course, if you wanted stronger encryption, you might remove spaces
and punctuation, or encrypt them as well. For this project,
however, we will retain punctuation, so that if we encrypt then decrypt
a file, we will be left with a file identical to the one we started
with. Even leaving in punctuation, the Vigenere Autokey cipher is
remarkably strong)
Program Decomposition
Your program should be built to reuse code. For example, you
should have an abstract superclass Encrypter, that has a sublcass for
encrypting/decrypting using the Cesar Cipher, and another subclass for
encrypting/decrypting using the AutoKey ciper. Your main loop
should use a variable of type Encrypter to do the encription and
decryption -- instantiate a Cesar Ciper or AutoKey depending upon input
parameters.
Implementation Details
Converting between Characters and Integers
If you cast a character as an integer, you can get its ASCII
value. The ASCII values for letters are all contiguous (that is,
the ASCII for 'b' is one larger than the ASCII for 'a', and so
on). So, we could convert from a character in the range of 'A' -
'Z' to an integer in the range 0-25 as follows:
char c;
// Assign c a character in the range 'A' - 'Z'
int i = (int) c - ((int) 'A');
Likewise, we can convert an integer in the range 0..25 to a character
in the range 'A' .. 'Z':
int i;
// Asign i an integer value in the range 0..25
char c = (char) (i + (int) 'A');
Building Tables
While you are wecome to build a 2D table for for calculating the next
encrypted letter, you don't need to. Instead, you can just take
the plaintext letter, convert it to a value between 0 and 25, convert
the key letter to a value between 0 and 25, add then together (mod (%)
26), and then convert the resulting integer to a character.
Encrypting using Autokey
If you have a keyword of length 5 (like QUEEN), you only need to store
5 characters of the key, no matter how long the plaintext message
is. How? Let's say you are encrypting "ATTACK AT
DAWN" using QUEEN:
Message: ATTACK AT DAWN
Key: QUEEN
Output: A + Q mod 26 = Q (after doing appropriate letter /
number conversions described above)
After using the first letter of the key to encrypt the plaintext,
remove the first letter of the key, and append the plaintext letter you
just encrypted to the end of the key:
Message: TTACK AT DAWN
Key: UEENA
Output: T + U mod 26 = N (after doing appropriate letter /
number conversions described above)
Again, after using the first letter of the key to encrypt the
plaintext,
remove the first letter of the key, and append the plaintext letter you
just encrypted to the end of the key:
Message: TACK AT DAWN
Key: EENAT
Output = T + E mod 26 = X
Continue until the entire message has been encrypted
Decrypting using Autokey
Decruption using Vigenere's Autokey is very similar to encryption -- to
encrypt you were adding the key value to the plaintext to get the
encrypted text -- how could you reverse that operation?
Grading
- Following coding standards: Code is appropriately
commented, variables are properly named, code is formatted correctly: 5
points.
- Program Decomposition: Using an abstract superclass and
subclasses for Cesar and Autokey correctly: 15 points
- Parsing command line parameters, reading file correctly 15 points
- Encrypting with Cesar cipher: 10 points
- Decrypting with Cesar cipher: 10 points
- Encrypting with AutoKey cipher 15 points
- Decrypting with AutoKey cipher 15 points
- Printing out correctly. 15 points
Extra Credit
You can earn up to 20 points of extra credit by adding a further
encryption algorithm, such as the random encryption algorithm discussed
in class on Friday. If you do use the random encryption
algorithm, be sure to write your own version of Random -- so that a
change in the Java Random class doesn't render all of your encrypted
files unreadable! If you want to do a different addtional
encryption scheme, let me know -- I'll likely approve it (assuming it
is non-trivial).
Submission
All file(s) required for your project should be in the folder
https://www.cs.usfca.edu/svn/<username>/cs112/Project4/