## Affine Cipher

The Affine cipher (gets it name from the definition of an affine function which is a combination of a translation and scaling) is another example of a substitution cipher where each letter is replaced by another based on some rule.

## Encryption

The affine cipher key consists of a pair of integers (a, b) which is used to are used to form the equation ax+b and subsequently used to generate the mapping of each plaintext character to ciphertext character.

Take (3, 5) as an example. Note a must be a co-prime (have no common factors other than 1) to 26 otherwise different characters may map to the same letter and thus wouldn’t be reversible. In group theory this is because a needs to be cyclic generator of the group Z/26Z

(a) 3*0+5=5 (F)
(d) 3*3+5=5 (O)
(x) 3*23+5=74=5 (mod 22) (W)

abcdefghijklmnopqrstuvwxyz
FILORUXADGJMPSVYBEHKNQTWZC

For each letter in your plaintext you replace it it with its corresponding ciphertext letter.

### Example

when the clock strikes twelve attack
TARS KAR LMVLJ HKEDJRH KTRMQR FKKFLJ

## Decryption

For each letter in your ciphertext you replace it it with its corresponding plaintext letter (the one above it in the mapping).

abcdefghijklmnopqrstuvwxyz - Key (9, 15)
PYHQZIRAJSBKTCLUDMVENWFOXG

FAZC EAZ HKLHB VEMJBZV EFZKWZ PEEPHB
when the clock strikes twelve attack

## Caesar Cipher

TheÂ Caesar Cipher is one of the most commonly used and simplest ciphers, named afterÂ Julius Caesar, it is a great place to start learning about ciphers.

## Encryption

The key is an integer normally known as the ‘shift’, it can be a number from 0-25 (0 being the identity). First you create your alphabet mapping of plaintext letters (lowercase) to ciphertext letters (uppercase) using the shift.

Some examples a shifts would look like this

Shift of 1:                Shift of 21:
abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz
BCDEFGHIJKLMNOPQRSTUVWXYZA VWXYZABCDEFGHIJKLMNOPQRSTU

For each letter in your plaintext you replace it it with its corresponding ciphertext letter (the one below it in the mapping).

It can also be thought of like converting each letter to is equivalent value (A=0, B=1…. Z=25 etc) and adding the key shift, and subtracting 26 if the value is 26 or greater,Â then convert back to letters.

### Example:

Encrypting “when the clock strikes twelve attack” using the shift of 21

when the clock strikes twelve attack
RCZI OCZ XGJXF NOMDFZN ORZGQZ VOOVXF

## Decryption

For each letter in your ciphertext you replace it it with its corresponding plaintext letter (the one above it in the mapping).

You can also convert each letter to is equivalent value (A=0, B=1…. Z=25 etc) and subtract the key shift this time, and add 26 if the value smaller than 0,Â then convert back to letters.

VWXYZABCDEFGHIJKLMNOPQRSTU - Shift of 21:
abcdefghijklmnopqrstuvwxyz

RCZI OCZ XGJXF NOMDFZN ORZGQZ VOOVXF
when the clock strikes twelve attack

As we are working in modular 26 a shift of -5 is the same as a shift of 21. The inverse key of 21 would be 5.

## Nihilist Substitution Cipher

The Nihilist Substitution is a poly-alphabetic cipher which means it uses multiple substitution alphabets and similar to the VigenĂšre Cipher.

The key consists of a 5×5 polybius square which has all the letters in the alphabet however I/J are treatedÂ the same and a second key.

 Â 1 2 3 4 5 1 A B C D E 2 F G H I/J K 3 L M N O P 4 Q R S T U 5 V W X Y Z

The second key can be of any length, keep in mind that the longer the key the more secure it theoretically is, however the key should be memorable so a person could remember and use it. Examples:

MAGIC, KEY, DEFEND, POLYALPHABETIC etc.

# Encryption

Consider the polybius square created using the keyword CIPHER

 Â 1 2 3 4 5 1 C I/J P H E 2 R A B D F 3 G K L M N 4 O Q S T U 5 V W X Y Z

and a second key of PAGE which defines the period as 4.

The second key is replaced with its position the polybius square (row then column), the numbers effectively become the key. Then each plaintext letter is Â written in rows of the period length and it too also replaced with its position the polybius square (row then column).Â The cipher text is then the sum of the key and the cipher text numbers.Â If the number is greater than 99 (3 digit number) subtract 100. 105 becomes 05, 100 becomes 00.

P  A  G  E   P  A  G  E   P  A  G  E
13 22 31 15  13 22 31 15  13 22 31 15
-----------  -----------  -----------
W  H  E  N   S  T  R  I   E  A  T  T
52 14 15 35  43 44 21 12  15 22 44 44
65 36 46 50  56 66 52 27  28 44 75 59
-----------  -----------  -----------
T  H  E  C   K  E  S  T   A  C  K
44 14 15 11  32 15 43 44  22 11 32
57 36 46 26  45 37 74 59  35 33 63
-----------  -----------
L  O  C  K   W  E  L  V
33 41 11 32  52 15 33 51
46 63 42 47  65 37 64 66

WHENTHECLOCKSTRIKESTWELVEATTACK using these keys encrypts to 65364650573646264663424756665227453774596537646628447559353363.

# Decryption

To decrypt simply split the number text into blocks of 2 andÂ write each block in rows of the period length then subtract the key numbers. If the result is less than 0 add 100.

There are however some serious flaws that significantly decrease the security of this cipher, these flaws can be used to break the Nihilist Substitution Cipher.

# Encryption

To encrypt in Hill a key first needs to be chosen, this will be a square matrix which has an inverse in modular 26. For the matrix to have an inverse the determinant must be co-prime to 26.

Here are two example matrices…
$M_{2}=\begin{bmatrix}2 & 3 \\5 & 3 \end{bmatrix}, M_{3}=\begin{bmatrix}25 & 2 & 11 \\19 & 5 & 12 \\21 & 22 & 6 \end{bmatrix}$
$det(M_{2})=-9$ (-9 mod 26) is 17 (-9+26Ă1), 17 is co-prime to 26
$det(M_{3})=-2131$ (-2131 mod 26) is 1 (-2131+26Ă82), 1 is co-prime to 26
Hence both these matrices are valid keys for the Hill cipher
*Co-prime means that the greatest common factor between the two numbers is 1.

If a 2 by 2 matrix is chosen for the key, the plaintext must be padded usually with an âXâ so that it is a multiple of 2. So for an N by N matrix the plaintext must be padded so that it that it is a multiple of N. E.G
For N=2, âCAREFULâ would become âCAREFULXâ
For N=2, âSPORTSâ would stay as âSPORTSâ
For N=3, âCAREFULâ would become âCAREFULXXâ
Once the text is a valid length, you take letters in blocks of N and convert them to a column vector. Letter âAâ has the value 0, âBâ is 1, âCâ is 2 âŠ âZâ is 25 etc.

Example:
$CA=\begin{bmatrix}2 \\0 \end{bmatrix}$, $RE=\begin{bmatrix}17 \\4 \end{bmatrix}$, $FU=\begin{bmatrix}5 \\20 \end{bmatrix}$, $LX=\begin{bmatrix}11 \\23 \end{bmatrix}$
$CAR=\begin{bmatrix}2 \\0 \\17 \end{bmatrix}$, $EFU=\begin{bmatrix}4 \\5 \\20 \end{bmatrix}$, $LXX=\begin{bmatrix}11 \\23 \\23 \end{bmatrix}$

You do this for each block of N characters. Then multiply the key matrix by each column vector. You will get a new column vector which can be converted back to letters.

$\begin{bmatrix}2&3 \\5&3 \end{bmatrix}\begin{bmatrix}2 \\0 \end{bmatrix}=\begin{bmatrix}2\times2+3\times0 \\5\times2+3\times0 \end{bmatrix}=\begin{bmatrix}4 \\10 \end{bmatrix}\bmod26$
âCAREFULâ encodes to âEKUTSHNUâ using the key matrix M2

$\begin{bmatrix}25 & 2 & 11 \\19 & 5 & 12 \\21 & 22 & 6 \end{bmatrix}\begin{bmatrix}2 \\0 \\17 \end{bmatrix}=\begin{bmatrix}25\times2+2\times0+11\times17 \\19\times2+5\times0+12\times17 \\21\times2+22\times0+6\times17 \end{bmatrix}=\begin{bmatrix}3 \\8 \\14 \end{bmatrix}\bmod26$
âCAREFULXâ encodes to âDIOSDCCCRâ using the key matrix M3