Skip to main content

Section 6.2 Affine Ciphers

In this section we use the mathematical ideas to create a new type of cipher. The cipher its self is a not particularly secure monoalphabetic substitution cipher. Therefore, as you work through the material, try to practice getting comfortable with the math and how we can use it.

Subsection 6.2.1 Affine Cipher: Encryption

Definition 6.2.1. Affine Cipher.

An affine cipher is a cipher with a two part key, a multiplier \(m\) and a shift \(s\) and calculations are carried out using modular arithmetic; typically the modulus is \(n=26\text{.}\) Characters of the plain text are enciphered with the formula

\begin{equation*} CIPHER\,\equiv\, m(plain)+s\pmod{26}, \end{equation*}

and characters of the cipher text are deciphered with the formula

\begin{equation*} plain\,\equiv\, m^{-1}(CIPHER-s)\pmod{26}, \end{equation*}

or

\begin{equation*} plain\,\equiv\, m^{-1}CIPHER-(m^{-1}s)\pmod{26}. \end{equation*}

Note that the multiplier \(m\) must be relatively prime to the modulus so that it has a multiplicative inverse. (See definitions Definition 6.1.8 and Definition 6.1.13)

Sample Enciphering.

Let's encipher the message “hello world” with an affine cipher. Let the multiplicative key be \(m=5\) and the shift \(s=16\text{;}\) assume that we match up the alphabet with the integers from 0 to 25 in the usual way so that ‘a’ is 0, ‘b’ is 1, ‘c’ is 2, etc.. In this way the letter ‘h’ is replaced by the number 7 and when we encipher it we get

\begin{equation*} 5\cdot 7+16\equiv 25\pmod{26} \end{equation*}

and 25 is Z, so plain ‘h’ becomes cipher Z. Next ‘e’ is replaced by 4 and we get

\begin{equation*} 5\cdot 4+16\equiv 10\pmod{26} \end{equation*}

and 10 is K, so plain ‘e’ becomes cipher K. The plain ‘l’ corresponds to 11 and

\begin{equation*} 5\cdot 11+16\equiv 19\pmod{26}\text{,} \end{equation*}

which is ‘T’, that is plain ‘l’ is replaced by cipher T. Try to encipher the rest of the message on your own, you will want to use Figure C.0.14 to help you with the multiplication modulo 26.

Comprehension Check:

  • What is the completed message?
  • Looking at the enciphered message how can you tell this is a monoalphabetic substitution cipher?
Sample Deciphering.

Now let's decipher the message AJINF CVCSI JCAKU which was enciphered using an affine cipher and a key of \(m=11\) and \(s=4\text{.}\) Note that \(m^{-1}\equiv 19\pmod{26}\) and \(-s\equiv 22\pmod{26}\text{.}\) Take the A and replace it by 0 and then using the formula above we get

\begin{equation*} 19(0+22)\equiv 2\pmod{26} \end{equation*}

so we replace cipher A with plain text ‘c’. The J is replaced by 9 and

\begin{equation*} 19(9+22)\equiv 17\pmod{26} \end{equation*}

therefore cipher J becomes plain ‘r’. To use the other formula for deciphering (see definition Definition 6.2.1) we need \(m^{-1}s\equiv 2\pmod{26}\text{.}\) Then converting the cipher I to 8 we get

\begin{equation*} 19(8)+2\equiv 24\pmod{26} \end{equation*}

which is plain ‘y’ or with the next letter N we get

\begin{equation*} 19(13)+2\equiv 15\pmod{26} \end{equation*}

which is ‘p’. Try to decipher the remaining characters in the message on your own.

Comprehension Check:

  • What is the completed message?
  • At the start how did we get \(m^{-1}\equiv 19\pmod{26}\) when \(m=11\text{?}\)
  • At the start how did we get \(-s\equiv 22\pmod{26}\) when \(s=4\text{?}\)
  • How do we arrive at \(m^{-1}s\equiv 2\pmod{26}\text{?}\)

Encipher the message “a fine affine cipher” using the key \(m=17\) and \(s=12\text{.}\)

Hint
  • Convert letters to numbers using ‘a’=0 through ‘z’=25.
  • Take advantage of Figure C.0.14.
  • The cipher is monoalphabetic so once you know how to encipher a letter, you don't have to do the calculation a second time.
Answer

MTSZC MTTSZ CUSHB CP

Decipher the message RXGTM CHUHJ CFWM which was enciphered using the key \(m=3\) and \(s=7\text{.}\)

Hint
  • Convert letters to numbers using ‘a’=0 through ‘z’=25.
  • Take advantage of Figure C.0.14.
  • The cipher is monoalphabetic so once you know how to decipher a letter, you don't have to do the calculation a second time.
  • Use either of the deciphering equations in definition Definition 6.2.1, but you should pick one and stick with it.
Answer

more than a shift

Subsection 6.2.2 Affine Cipher: Decryption

To decrypt, as opposed to just decipher, an affine cipher you can use the techniques we learned in Chapter 2 since they are a type of monoalphabetic substitution cipher. However, we can also take advantage of the fact that it is an affine cipher and use the formulas in definition Definition 6.2.1.

DZIUI UDZYH ILUDO HHIBY GITZY LSYUU OQYDZ ODEYE IJJPY GLWTD 
IDEOU YBGIT ZYLYP EIDZO AYWKH BIBYH KLDZY SMJDI TJIYL OBPHK 
MLDYY BHKLD ZYUZI HDUIB GYIDI UOSKB KOJTZ OXYDI GEYGO BODDO 
GAIDE IDZXO UIGHL YCMYB GWOBO JWUIU XMDEY GOBOJ UKMUY DZODO 
BOJWU IUDKL YGKVY LDZYK LIQIB OJAYW UKDZO DEYGO BPYGI TZYLD 
ZYSYU UOQY

Analyzing this we get that the most common characters are Y, D, I, O and U; the most common bigrams are DZ, ZY, YG, and OB; the most common trigrams are DZY, OBO, LDZ, and DZO. Therefore it is reasonable to assume that DZY is ‘the’, Y is ‘e’, and D is ‘t’. So when this was enciphered we have to of had

\begin{gather*} m\cdot 4+s\equiv 24 \pmod{26}\\ m\cdot 19+s\equiv 3 \pmod{26} \end{gather*}

Subtracting the second expression from the first we get

\begin{gather*} m\cdot -15\equiv 21 \pmod{26} \end{gather*}

or

\begin{gather*} m\cdot 11\equiv 24 \pmod{26}. \end{gather*}

Looking at the multiplication table modulo 26 (figure Figure C.0.14 we can see that \(m=9\) since \(9\cdot 11\equiv 21\pmod{26}\text{.}\) Substituting \(m=9\) into the first equation above we get

\begin{gather*} 9\cdot 4+s\equiv 24 \pmod{26} \end{gather*}

which simplifies to

\begin{gather*} s\equiv 24-10 \pmod{26} \end{gather*}

so that \(s=14\text{.}\) We can then get the inverse keys

\begin{equation*} m^{-1}\equiv 3\pmod{26}\ \text{and}\ -(m^{-1}s)\equiv 10\pmod{26}. \end{equation*}

Using these with the affine cipher cell we get the deciphered message:

thisi sthef irsta ffine ciphe rmess ageth atwew illde crypt 
itwas encip hered witha keyof ninef orthe multi plier andfo 
urtee nfort heshi ftsin ceiti samon oalph abeti cweca natta 
ckitw ithba sicfr equen cyana lysis butwe canal souse thata 
nalys istor ecove rtheo rigin alkey sotha tweca ndeci phert 
hemes sage

Or, in a more readable form

“this is the first affine cipher message that we will decrypt ...”

Comprehension Check:

  • Above we assumsed ciphertext Y is plaintext ‘e’, how did this give us \(m\cdot 4+s\equiv 24 \pmod{26}\text{?}\)
  • Above we assumsed ciphertext D is plaintext ‘t’, how did this give us \(m\cdot 19+s\equiv 3 \pmod{26}\text{?}\)
  • How did we then get the -15 and 21 in \(m\cdot -15\equiv 21 \pmod{26}\text{?}\)
  • In the calculations above we simplified \(9\cdot 4+s\equiv 24 \pmod{26}\) to \(s\equiv 24-10 \pmod{26}\text{,}\) where did the -10 come from?
  • How and why did we then calculate
    \begin{equation*} m^{-1}\equiv 3\pmod{26}\ \text{and}\ -(m^{-1}s)\equiv 10\pmod{26}? \end{equation*}

Try to decrypt this message which was enciphered using an affine cipher.

IPAGS WWANP YMFZC TAEWI PZFGC TZANW QWCNN YCMAY ECGGA CZAHV 
QWQIE PFACN ZFGWI KYPMC DCGZB YBACP HPIHH YPMIL ANCPI LADTI 
NWQHC QGEIN KFCHV AAPCP AXFCS GZYPM IPAWQ EYTAF CHCDN ACHQM 
IPASB GZCYN GCPHZ FAGIS PHITZ FADIO KYPMI TZFAF CDDHI INGIW 
AZYWA VATIN AZIDH WAZFC ZZFAG ANLCP ZGFCH CDGIN AZYNA HYFCH 
NYGAP TNIWW QGACZ CPHEC GKPIO KYPMI SZZFA CGFAG ITWQB YBAEF 
APYGS HHAPD QFACN HZFAO DCPMI TZFAV ADDYD IIKAH CZZFA ODIOK 
YZECG CUSCN ZANZI ZEADL AZFYG OISDH PIZVA CLYGY ZINCZ GIDCZ 
ACPFI SNCBC ZYAPZ ALYHA PZDQC PHBIG GYVDQ CPCDD PYMFZ GYZZY 
PMEYZ FCENQ TCOAY EAPZI SZYPZ IZFAF CDDCP HIBAP AHZFA HIINZ 
IWQCG ZIPYG FWAPZ YZECG GFAND IOKFI DWAGE FIGZI IHSBI PWQGZ 
AB
Hint
  • First use frequency analysis to identify at least two of the letters in the message.
  • With your two letters set up two equations like this:
    \begin{equation*} CIPHER\equiv m(plain)+s\pmod{26}. \end{equation*}
  • Subtract the second equation from the first and try to find \(m\text{.}\)
  • Substitute your value for \(m\) into the first equation and use it to find \(s\text{.}\)
  • Now that you have the key you should be able to decipher the message as you had previously.
Answer

The message begins with “One summer night, a few months after my ...”

Try to decrypt this message which was enciphered using an affine cipher.

IGZMZ KZMZU DZPHX DYZHM YLIFH UFUGT JHUXQ ZDRZX 
FKRUD MZORS YZQMF QZMIR ZXUFI RUORV ZUFTX IFZHM 
IGHXQ ZDRZX RGHXI ZUIFQ FRUIF TIDTX IFJHM RYLJH 
XBTZM HORUV HXFMO RUHML GTJHU SZRUV XIGZR MORXV 
TRXZG FPZEZ MSZDH JZIMH UXQHM ZUIRU IGZKH DZFKI 
GZKFY YFPRU VFSXZ MEHIR FUXSL IGZHT IGFMR IPHXH 
IFUDZ FSERF TXIGZ HTIGF MNUZP ZEZML IGRUV NUZPZ 
EZMLI GRUVH UOPHX IHNRU VRIRU GRXXI MROZI GZYRU 
ZHUOR IMZJS YZMZJ ZJSZM RUVRI ZEZUU FPMZH OGRXZ 
LZXXY FPYLM FEZOH SFTII GZMFF JEHVT ZDGRY YXHXX 
HRYZO JZRIM RZOIF QRDIT MZIGZ ZLZXO ROIGZ LMFYY 
YRNZO RJZXI GZQHX XHVZR UORDH IZOUF IIGZL XZZJZ 
OIFJF EZIGM FTVGI GZHRM UFIFE ZMIGZ XTMKH DZMHI 
GZMMH QROYL HQQHM ZUIYL UFFUZ RUIGZ XIFML PHXXT 
MQMRX ZOIGH IXPGH IIRQQ ZOJZF KKUFX RVUFK HJHWZ 
JZUIH IXTDG HUFTI MHVZF TXIGR UVIGZ ZLZXG HEZRI 
SLQGR YRQNO RDN 
Hint
  • First use frequency analysis to identify at least two of the letters in the message.
  • With your two letters set up two equations like this:
    \begin{equation*} CIPHER\equiv m(plain)+s\pmod{26}. \end{equation*}
  • Subtract the second equation from the first and try to find \(m\text{.}\)
  • Substitute your value for \(m\) into the first equation and use it to find \(s\text{.}\)
  • Now that you have the key you should be able to decipher the message as you had previously.
Answer

The message begins with “The reference was clearly to a nonhuman species of incredible properties, ...”

Try to decrypt this message which was enciphered using an affine cipher.

FXJHS DBFSP VNFSZ TVQNU ULZAJ IQIFL QFQFE FZULB 
FDLSV QJXJH IZVKM FEJSR HLGJQ VJMVL DUQVV SFEJS 
RHLGJ QVPLQ UJHQU LZNJI ZVKVQ PLQUJ HQULZ NJFQJ 
MBFLK PLQUJ HQULZ NHLZZ VZFEJ SRHLG JQVNK JQUVE 
LSFPJ JKVSE JHWKV QQUVW KHVNJ KJIJM PULNU UFEMF 
EVELS QJFSF BVKVZ ZZUFE VWVQP VVSKV VZJMP LSVFS 
EFUVF YVSKX FOHIV MFNVK JSDFS EWIJP SULDU NUVVT 
WJSVZ FZLDS JMZFD FNLQX QUVBF GLKKF IXBHZ NKVZV 
SJIBJ HZKXE VYVKJ AVEFS LSMFK KLWKV ZLDSW XPULN 
UFDFZ NJSBF XFKPF XZWVE VQVNQ VEQUI VVBHZ TVQVV 
IZWXF KVGFS EIVEH BFZ 
Hint
  • First use frequency analysis to identify at least two of the letters in the message.
  • With your two letters set up two equations like this:
    \begin{equation*} CIPHER\equiv m(plain)+s\pmod{26}. \end{equation*}
  • Subtract the second equation from the first and try to find \(m\text{.}\)
  • Substitute your value for \(m\) into the first equation and use it to find \(s\text{.}\)
  • Now that you have the key you should be able to decipher the message as you had previously.
Answer

The message begins with “A young man--we can sketch his portrait at a dash. Imagine to yourself a Don Quixote of eighteen; ...”

Sage N-Gram Cell:

Figure 6.2.7. Sage N-Gram Cell

Affine Cipher Cell:

You can use this Sage Cell to encipher and decipher messages that use an affine cipher. To decipher you will need to use the second formula listed in Definition 6.2.1. Also, be sure you understand how to encipher and decipher by hand.

Figure 6.2.8. Affine Cipher Cell