Dec 03, 2024 | 1203 words | 12 min read
10.1.1. Materials#
Encrypting Text with Ciphers#
Encryption is the process of converting plaintext into ciphertext to secure the information. Ciphers are algorithms used to perform encryption and decryption. In this project, we’ll explore three types of ciphers: Caesar, XOR, and Vigenère.
Caesar Cipher#
The “Caesar Cipher” is one of the simplest and oldest encryption techniques. It is a
substitution cipher, where each character in the plaintext is shifted by a fixed
number of positions down or up the alphabet. For example, with a shift of \(3\),
A
becomes D
, B
becomes E
, and so on. The alphabet wraps around, so Z
would
become C
. Encrypting the plaintext message HELLO
with a shift of \(3\) would
give KHOOR
as the ciphertext. If a message was encrypted with a shift of \(3\),
we can decrypt it by applying the same process with a shift of \(-3\). Applying a
shift of \(-3\) to the ciphertext KHOOR
would reveal the plaintext HELLO
.
Numbers are shifted in the same way as letters, but they wrap around from 9 to 0.
Example#
For demonstration purposes, below is Caesar cipher example using the plaintext Hello 5!
and shift of \(3\).
Shifted Character Tables (Shift of 3)#
Original Letter |
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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Shifted Letter |
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 |
Original Letter |
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 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Shifted Letter |
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 |
Original Digits |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
0 |
---|---|---|---|---|---|---|---|---|---|---|
Shifted Digits |
4 |
5 |
6 |
7 |
8 |
9 |
0 |
1 |
2 |
3 |
Encryption Process#
Original Character |
H |
E |
L |
L |
O |
5 |
! |
|
---|---|---|---|---|---|---|---|---|
Character Position |
7 |
4 |
11 |
11 |
14 |
- |
5 |
- |
Shifted Position |
10 |
7 |
14 |
14 |
17 |
- |
8 |
- |
Shifted Character |
K |
H |
O |
O |
R |
8 |
! |
Note
Character Position: Assign numbers to letters where A=0, B=1, …, Z=25.
Shifted Position: For letters, calculated as (Original Position + Shift) mod 26. For numbers, calculated as (Original Position + Shift) mod 10.
Shifted Character: The character corresponding to the shifted position.
Special characters, like spaces are not shifted.
Decryption Process#
Cipher Character |
K |
H |
O |
O |
R |
8 |
! |
|
---|---|---|---|---|---|---|---|---|
Character Position |
10 |
7 |
14 |
14 |
17 |
- |
8 |
- |
Shifted Position |
7 |
4 |
11 |
11 |
14 |
- |
5 |
- |
Original Character |
H |
E |
L |
L |
O |
5 |
! |
XOR Cipher#
The XOR Cipher
is a type of bitwise operation
where each bit of the plaintext is
combined with a bit from the key using the XOR (exclusive OR) operation.
How it works:
Binary Representation: Convert both the plaintext and key into binary.
XOR Operation: XOR each bit of the plaintext with the corresponding bit of the key. The XOR operation returns 1 when the bits are different and 0 when they are the same.
Encrypting:
For each bit of the plaintext, apply the XOR operation with the corresponding bit of the key.
If the key is shorter than the plaintext, it can be repeated (looped over).
Formula: \(\text{Ciphertext} = \text{Plaintext Value} \oplus \text{key}\)
Decrypting:
The XOR operation is its own inverse, meaning applying XOR with the same key again will give back the original plaintext.
Formula: \(\text{Plaintext Value} = \text{Ciphertext} \oplus \text{key}\)
Example#
For demonstration purposes, below is XOR cipher example using the Plaintext Value “Hello” and the key “ENGR”.
1. Convert “Hello” and “ENGRE” to 8-bit binary#
Since “Hello” has 5 characters and “ENGR” has 4, we’ll repeat the key to match the length, resulting in “ENGRE”.
Character |
ASCII Code |
Binary |
---|---|---|
H |
72 |
01001000 |
e |
101 |
01100101 |
l |
108 |
01101100 |
l |
108 |
01101100 |
o |
111 |
01101111 |
Character |
ASCII Code |
Binary |
---|---|---|
E |
69 |
01000101 |
N |
78 |
01001110 |
G |
71 |
01000111 |
R |
82 |
01010010 |
E |
69 |
01000101 |
2. Perform the XOR operation#
We XOR each bit of the plaintext with the corresponding bit of the key.
Bit Position |
‘H’ Bit |
‘E’ Bit |
XOR Result |
---|---|---|---|
8 |
0 |
0 |
0 |
7 |
1 |
1 |
0 |
6 |
0 |
0 |
0 |
5 |
0 |
0 |
0 |
4 |
1 |
0 |
1 |
3 |
0 |
1 |
1 |
2 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
Bit Position |
‘e’ Bit |
‘N’ Bit |
XOR Result |
---|---|---|---|
8 |
0 |
0 |
0 |
7 |
1 |
1 |
0 |
6 |
1 |
0 |
1 |
5 |
0 |
0 |
0 |
4 |
0 |
1 |
1 |
3 |
1 |
1 |
0 |
2 |
0 |
1 |
1 |
1 |
1 |
0 |
1 |
Bit Position |
‘l’ Bit |
‘G’ Bit |
XOR Result |
---|---|---|---|
8 |
0 |
0 |
0 |
7 |
1 |
1 |
0 |
6 |
1 |
0 |
1 |
5 |
0 |
0 |
0 |
4 |
1 |
0 |
1 |
3 |
1 |
1 |
0 |
2 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
Bit Position |
‘l’ Bit |
‘R’ Bit |
XOR Result |
---|---|---|---|
8 |
0 |
0 |
0 |
7 |
1 |
1 |
0 |
6 |
1 |
0 |
1 |
5 |
0 |
1 |
1 |
4 |
1 |
0 |
1 |
3 |
1 |
0 |
1 |
2 |
0 |
1 |
1 |
1 |
0 |
0 |
0 |
Bit Position |
‘o’ Bit |
‘E’ Bit |
XOR Result |
---|---|---|---|
8 |
0 |
0 |
0 |
7 |
1 |
1 |
0 |
6 |
1 |
0 |
1 |
5 |
0 |
0 |
0 |
4 |
1 |
0 |
1 |
3 |
1 |
1 |
0 |
2 |
1 |
0 |
1 |
1 |
1 |
1 |
0 |
3. Show the output in 8-bit binary#
Note
The bytes resulting from the XOR operation might not be printable characters.
XOR Binary |
ASCII Code |
Character |
---|---|---|
00001101 |
13 |
‘\r’ (Carriage Return) |
00101011 |
43 |
‘+’ |
00101011 |
43 |
‘+’ |
00111110 |
62 |
‘>’ |
00101010 |
42 |
‘*’ |
4. Decryption Process#
To retrieve the original plaintext, XOR the Ciphertext with the same key “ENGRE”.
Ciphertext |
Cipher Binary |
Key |
Key Binary |
XOR Result |
Plaintext |
---|---|---|---|---|---|
CR (13) |
00001101 |
E |
01000101 |
01001000 |
H |
‘+’ (43) |
00101011 |
N |
01001110 |
01100101 |
e |
‘+’ (43) |
00101011 |
G |
01000111 |
01101100 |
l |
‘>’ (62) |
00111110 |
R |
01010010 |
01101100 |
l |
‘*’ (42) |
00101010 |
E |
01000101 |
01101111 |
o |
Vigenère Cipher#
The Vigenère Cipher encrypts messages by shifting each letter of the plaintext by a number of positions determined by a keyword. Unlike the Caesar cipher, which uses a single shift value, the Vigenère cipher uses a sequence of shifts based on the letters of a keyword.
How it works#
Encrypting#
Prepare the keyword.
Keyword: Convert to uppercase. If the keyword is shorter than the plaintext, repeat it to match the length of the plaintext.
Assign numerical values.
Convert each letter in the plaintext and keyword to its corresponding numerical value: A = 0, B = 1, …, Z = 25.
Use the same numerical values for both uppercase and lowercase letters.
For numbers, use the number itself (e.g., 0 for 0, 1 for 1, etc.).
Calculate the ciphertext.
For each letter in the plaintext, apply the shift determined by the corresponding keyword letter:
\(\text{Ciphertext Letter Value} = (\text{Plaintext Letter Value} + \text{Keyword Value}) \mod 26\)
\(\text{Ciphertext Number Value} = (\text{Plaintext Number Value} + \text{Keyword Value}) \mod 10\)
Leave non-alphabetic characters unchanged.
Convert ciphertext values back to letters or numbers.
Convert the \(\text{Ciphertext Letter Value}\)s back to letters.
Convert the \(\text{Ciphertext Number Value}\)s back to numbers.
Decrypting#
Prepare the Keyword.
Ensure keyword is in uppercase.
Assign numerical values.
As in encryption, assign numerical values to the letters and numbers.
Calculate the plaintext values by reversing the encryption process.
\(\text{Plaintext Letter Value} = (\text{Ciphertext Letter Value} - \text{Keyword Value}) \mod 26\)
\(\text{Plaintext Number Value} = (\text{Ciphertext Number Value} - \text{Keyword Value}) \mod 10\)
Convert the numerical plaintext values back to a letter or numbers.
Convert the \(\text{Plaintext Letter Value}\)s back to letters.
Convert the \(\text{Plaintext Number Value}\)s back to numbers.
Example#
For demonstration purposes, below is Vigenère cipher example using the plaintext
Hello 123
and keyword of Key
.
Encryption#
Prepare the Plaintext and Keyword#
Plaintext:
Hello 12
Keyword:
Key
Repeat Key to match length of Plaintext, as shown below
Plaintext Letter |
H |
e |
l |
l |
o |
1 |
2 |
|
---|---|---|---|---|---|---|---|---|
Keyword Letter |
K |
E |
Y |
K |
E |
Y |
K |
E |
Extended Keyword:
KEYKEYKE
(Note that we’ve converted it to uppercase.)
Assign Numerical Values#
Letter |
Numeric Value |
---|---|
A |
0 |
B |
1 |
… |
… |
H |
7 |
E |
4 |
L |
11 |
O |
14 |
K |
10 |
Y |
24 |
1 |
1 |
2 |
2 |
Calculate the Ciphertext#
For each letter, calculate:
\(\text{Ciphertext Letter Value} = (\text{Plaintext Letter Value} + \text{Keyword Value}) \mod 26\)
For each number, calculate:
\(\text{Ciphertext Number Value} = (\text{Plaintext Number Value} + \text{Keyword Value}) \mod 10\)
Position |
Plaintext Letter |
Plaintext Value |
Keyword Letter |
Keyword Value |
Cipher Value |
Cipher Character |
---|---|---|---|---|---|---|
1 |
H |
7 |
K |
10 |
( 7 + 10) % 26 = 17 |
R |
2 |
e |
4 |
E |
4 |
( 4 + 4) % 26 = 8 |
i |
3 |
l |
11 |
Y |
24 |
(11 + 24) % 26 = 9 |
j |
4 |
l |
11 |
K |
10 |
(11 + 10) % 26 = 21 |
v |
5 |
o |
14 |
E |
4 |
(14 + 4) % 26 = 18 |
s |
6 |
Y |
24 |
||||
7 |
1 |
1 |
K |
10 |
( 1 + 10) % 10 = 1 |
1 |
8 |
2 |
2 |
E |
4 |
( 2 + 4) % 10 = 6 |
6 |
Convert Numbers Back to Letters#
Ciphertext:
Rijvs 16
Decryption#
To decrypt the ciphertext Rijvs 16
using the same keyword Key
, we reverse the
encryption process.
Prepare the Ciphertext and Keyword#
Ciphertext:
Rijvs 16
Keyword:
Key
Repeat the uppercase key to match length of Ciphertext:
KEYKEYKE
Assign Numerical Values#
Using the same numerical values as before.
Calculate the Plaintext#
\(\text{Plaintext Letter Value} = (\text{Ciphertext Letter Value} - \text{Keyword Value}) \mod 26\)
\(\text{Plaintext Number Value} = (\text{Ciphertext Number Value} - \text{Keyword Value}) \mod 10\)
Position |
Cipher Letter |
Cipher Value |
Keyword Letter |
Keyword Value |
Plaintext Value |
Plaintext Letter |
---|---|---|---|---|---|---|
1 |
R |
17 |
K |
10 |
(17 - 10) % 26 = 7 |
H |
2 |
i |
8 |
E |
4 |
( 8 - 4) % 26 = 4 |
e |
3 |
j |
9 |
Y |
24 |
( 9 - 24) % 26 = 11 |
l |
4 |
v |
21 |
K |
10 |
(21 - 10) % 26 = 11 |
l |
5 |
s |
18 |
E |
4 |
(18 - 4) % 26 = 14 |
o |
6 |
Y |
24 |
||||
7 |
1 |
4 |
K |
10 |
( 1 - 10) % 10 = 1 |
1 |
8 |
6 |
1 |
E |
4 |
( 6 - 4) % 10 = 2 |
2 |
In summary#
Caesar Cipher: Decryption is simply the encryption process with a negative shift.
XOR Cipher: Encryption and decryption use the same operation.
Vigenère Cipher: Has distinct processes for encryption and decryption, although both involve shifting letters based on a keyword. The difference lies in the direction of the shift for each operation.