Feb 17, 2025 | 1314 words | 13 min read
12.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 Beaufort.
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 |
Beaufort Cipher#
The Beaufort 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 Beaufort cipher uses a sequence of shifts based on the letters of a keyword.
How it works#
Encrypting#
Prepare the keyword.
Keyword: Convert to lowercase. 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. Use the table below to assign a numerical value to each letter and number:
Table 12.15 Numerical Values for Characters# Character
Number
a
0
b
1
c
2
d
3
e
4
f
5
g
6
h
7
i
8
j
9
k
10
l
11
m
12
n
13
o
14
p
15
q
16
r
17
s
18
t
19
u
20
v
21
w
22
x
23
y
24
z
25
0
26
1
27
2
28
3
29
4
30
5
31
6
32
7
33
8
34
9
35
Make the plaintext string lowercase before encrypting.
Calculate the ciphertext.
For each letter in the plaintext, apply the shift determined by the corresponding keyword letter:
\(\text{Ciphertext Letter Value} = (\text{Keyword Value} - \text{Plaintext Letter Value}) \mod 36\)
Note
If the subtraction of \((\text{Keyword Value} - \text{Plaintext Letter Value})\) results in a negative number, make it positive by adding 36
Also it can be noted that the mod of a negative number is equivalent to adding the divisor to the number and then applying the mod \(e.g. -4\mod7 = (-4 + 7)\mod7 = 3\mod7 = 3\)
Look up how the mod operator in Python works for in-depth details.
Leave non-alphabetic characters unchanged.
Convert ciphertext values back to letters or numbers.
Convert the \(\text{Ciphertext Values}\)s back to characters using the same table Table 12.15.
Decrypting#
Prepare the Keyword.
Ensure keyword is in lowercase.
Assign numerical values.
As in encryption, assign numerical values to the letters and numbers.
Calculate the plaintext values using the same encryption process.
\(\text{Plaintext Letter Value} = (\text{Keyword Value} - \text{Ciphertext Letter Value}) \mod 36\)
Note
If the subtraction of \((\text{Keyword Value} - \text{Ciphertext Letter Value})\) results in a negative number, make it positive by adding 36
Also it can be noted that the mod of a negative number is equivalent to adding the divisor to the number and then applying the mod \(e.g. -4\mod7 = (-4 + 7)\mod7 = 3\mod7 = 3\)
Look up how the mod operator in Python works for in-depth details.
Leave non-alphabetic characters unchanged.
Convert the numerical plaintext values back to a letter or numbers.
Convert the \(\text{Plaintext Values}\)s back to characters using the same table Table 12.15.
Example#
For demonstration purposes, below is Beaufort 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 the plaintext and key to lowercase.)
Assign Numerical Values#
Letter |
Numeric Value |
---|---|
a |
0 |
b |
1 |
… |
… |
h |
7 |
e |
4 |
l |
11 |
o |
14 |
k |
10 |
y |
24 |
1 |
27 |
2 |
28 |
Calculate the Ciphertext#
For each character, calculate:
\(\text{Ciphertext Letter Value} = (\text{Keyword Value} - \text{Plaintext Letter Value}) \mod 36\)
Position |
Plaintext Letter |
Plaintext Value |
Keyword Letter |
Keyword Value |
Cipher Value |
Cipher Character |
---|---|---|---|---|---|---|
1 |
h |
7 |
k |
10 |
(10 - 7) % 36 = 3 |
d |
2 |
e |
4 |
e |
4 |
(4 - 4) % 36 = 0 |
a |
3 |
l |
11 |
y |
24 |
(24 - 11) % 36 = 13 |
n |
4 |
l |
11 |
k |
10 |
(10 - 11) % 36 = 35 |
9 |
5 |
o |
14 |
e |
4 |
(4 - 14) % 36 = 26 |
0 |
6 |
y |
24 |
||||
7 |
1 |
27 |
k |
10 |
(10 - 27) % 36 = 19 |
t |
8 |
2 |
28 |
e |
4 |
(4 - 28) % 36 = 12 |
m |
Convert Numbers Back to Letters#
Ciphertext:
dan90 tm
Decryption#
To decrypt the ciphertext dan90 tm
using the same keyword Key
, we reverse the
encryption process.
Prepare the Ciphertext and Keyword#
Ciphertext:
dan90 tm
Keyword:
Key
Repeat the lowercase 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{Keyword Value} - \text{Ciphertext Letter Value}) \mod 36\)
Position |
Cipher Letter |
Cipher Value |
Keyword Letter |
Keyword Value |
Plaintext Value |
Plaintext Letter |
---|---|---|---|---|---|---|
1 |
d |
3 |
k |
10 |
(10 - 3) % 36 = 7 |
h |
2 |
a |
0 |
e |
4 |
(4 - 0) % 36 = 4 |
e |
3 |
n |
13 |
y |
24 |
(24 - 13) % 36 = 11 |
l |
4 |
9 |
35 |
k |
10 |
(10 - 35) % 36 = 11 |
l |
5 |
0 |
26 |
e |
4 |
(4 - 26) % 36 = 14 |
o |
6 |
y |
24 |
||||
7 |
t |
19 |
k |
10 |
(10 - 19) % 36 = 27 |
1 |
8 |
m |
12 |
e |
4 |
(4 - 12) % 36 = 28 |
2 |
In summary#
Caesar Cipher: Decryption is simply the encryption process with a negative shift.
XOR Cipher: Encryption and decryption use the same operation.
Beaufort Cipher: Encryption and decryption use the same operation.