\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% 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} \]

Oct 24, 2024 | 1163 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 letter 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.

Example#

For demonstration purposes, below is Caesar cipher example using the plaintext Hello and shift of \(3\).

Shifted Alphabet Table (Shift of 3)#
Table 10.2 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 10.3 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

Encryption Process#
Table 10.4 Encryption#

Original Letter

H

E

L

L

O

Alphabet Position

7

4

11

11

14

Shifted Position

10

7

14

14

17

Shifted Letter

K

H

O

O

R

Note

  • Alphabet Position: Assign numbers to letters where A=0, B=1, …, Z=25.

  • Shifted Position: Calculated as (Original Position + Shift) mod 26.

  • Shifted Letter: The letter corresponding to the shifted position.

Decryption Process#
Table 10.5 Decryption#

Cipher Letter

K

H

O

O

R

Alphabet Position

10

7

14

14

17

Shifted Position

7

4

11

11

14

Original Letter

H

E

L

L

O

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 10.6 Plaintext: “Hello”#

Character

ASCII Code

Binary

H

72

01001000

e

101

01100101

l

108

01101100

l

108

01101100

o

111

01101111

Table 10.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 10.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 10.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 10.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 10.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 10.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 10.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 10.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

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

    • Keyword: Convert to uppercase. 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: 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.).

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

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

    • Ensure keyword is in uppercase.

  2. Assign numerical values.

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

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

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

Table 10.15 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 it to uppercase.)

Assign Numerical Values#
Table 10.16 Convert Letters to Numbers#

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

Table 10.17 Calculate Shifted Positions#

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

Table 10.18 Calculate Shifted Positions#

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.