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

New: Try it yourself!

Cryptanalysis of the Caesar Cipher

If you need a reminder on how the Caesar Cipher works click here.

The Caesar Cipher is a very easy to crack as there are only 25 unique keys so we can test all of them and score how English they are using either Chi-Squared Statistic or N-Gram Probability.

Example

Ciphertext of “RCZIOCZXGJXFNOMDFZNORZGQZVOOVXF”

Shift | Decrypted Text                 | Chi-Sq Score
1       QBYHNBYWFIWEMNLCEYMNQYFPYUNNUWE  201.327499
2       PAXGMAXVEHVDLMKBDXLMPXEOXTMMTVD  599.489345
3       OZWFLZWUDGUCKLJACWKLOWDNWSLLSUC  267.058510
4       NYVEKYVTCFTBJKIZBVJKNVCMVRKKRTB  325.267580
5       MXUDJXUSBESAIJHYAUIJMUBLUQJJQSA  775.163340
6       LWTCIWTRADRZHIGXZTHILTAKTPIIPRZ  434.880892
7       KVSBHVSQZCQYGHFWYSGHKSZJSOHHOQY  554.916606
8       JURAGURPYBPXFGEVXRFGJRYIRNGGNPX  340.923863
9       ITQZFTQOXAOWEFDUWQEFIQXHQMFFMOW  1012.384679
10      HSPYESPNWZNVDECTVPDEHPWGPLEELNV  115.358434
11      GROXDROMVYMUCDBSUOCDGOVFOKDDKMU  91.670467
12      FQNWCQNLUXLTBCARTNBCFNUENJCCJLT  283.701596
13      EPMVBPMKTWKSABZQSMABEMTDMIBBIKS  194.299832
14      DOLUAOLJSVJRZAYPRLZADLSCLHAAHJR  385.733449
15      CNKTZNKIRUIQYZXOQKYZCKRBKGZZGIQ  1520.292006
16      BMJSYMJHQTHPXYWNPJXYBJQAJFYYFHP  801.523128
17      ALIRXLIGPSGOWXVMOIWXAIPZIEXXEGO  603.683962
18      ZKHQWKHFORFNVWULNHVWZHOYHDWWDFN  280.874579
19      YJGPVJGENQEMUVTKMGUVYGNXGCVVCEM  269.610988
20      XIFOUIFDMPDLTUSJLFTUXFMWFBUUBDL  176.849244
21      WHENTHECLOCKSTRIKESTWELVEATTACK  51.921327
22      VGDMSGDBKNBJRSQHJDRSVDKUDZSSZBJ  460.236803
23      UFCLRFCAJMAIQRPGICQRUCJTCYRRYAI  262.108135
24      TEBKQEBZILZHPQOFHBPQTBISBXQQXZH  1373.411997
25      SDAJPDAYHKYGOPNEGAOPSAHRAWPPWYG  90.715517

As you can see the lowest Chi-Squared value is 51.921327, which was using a shift of 21. If you read the decrypted text for a shift of 21 you can indeed see that it is English. Hence cipher has been broken!

WIP

 

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.

New: Try it yourself!