\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

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)#
Table 12.1 Plaintext Shift for Upper-case#

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

Table 12.2 Plaintext Shift for Lower-case#

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

Table 12.3 Plaintext Shift for digits#

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#
Table 12.4 Encryption#

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#
Table 12.5 Decryption#

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:

  1. Binary Representation: Convert both the plaintext and key into binary.

  2. 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.

  3. 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}\)

  4. 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”.

Table 12.6 Plaintext: “Hello”#

Character

ASCII Code

Binary

H

72

01001000

e

101

01100101

l

108

01101100

l

108

01101100

o

111

01101111

Table 12.7 Key: “ENGRE”#

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.

Table 12.8 First Pair: ‘H’ (01001000) and ‘E’ (01000101) produces 00001101#

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

Table 12.9 Second Pair: ‘e’ (01100101) and ‘N’ (01001110) produces 00101011#

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

Table 12.10 Third Pair: ‘l’ (01101100) and ‘G’ (01000111) produces 00101011#

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

Table 12.11 Fourth Pair: ‘l’ (01101100) and ‘R’ (01010010) produces 00111110#

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

Table 12.12 Fifth Pair: ‘o’ (01101111) and ‘E’ (01000101) produces 00101010#

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.

Table 12.13 Output in 8-bit binary#

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”.

Table 12.14 Decryption Process#

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#
  1. Prepare the keyword.

    • Keyword: Convert to lowercase. If the keyword is shorter than the plaintext, repeat it to match the length of the plaintext.

  2. 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.

  3. 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\)

    • Leave non-alphabetic characters unchanged.

  4. 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#
  1. Prepare the Keyword.

    • Ensure keyword is in lowercase.

  2. Assign numerical values.

    • As in encryption, assign numerical values to the letters and numbers.

  3. Calculate the plaintext values using the same encryption process.

    • \(\text{Plaintext Letter Value} = (\text{Keyword Value} - \text{Ciphertext Letter Value}) \mod 36\)

    • Leave non-alphabetic characters unchanged.

  4. 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

Table 12.16 Extend the Keyword#

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#
Table 12.17 Convert Letters to Numbers#

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\)

Table 12.18 Calculate Shifted Positions#

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\)

Table 12.19 Calculate Shifted Positions#

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.