Anda di halaman 1dari 7

Name: Osama Bin Tariq

ENROLLMENT ID: 01-235151-017
TEACHER NAME: Maam AaliyaAamir
Date of Submission: May 16, 2016

(Project Report)

Department Of Computer Science

Islamabad Campus

The affine cipher is a type of monoalphabetic substitution cipher, whereeach letter in an alphabet
is mapped to its numeric equivalent, encrypted using a simple mathematical function, and
converted back to a letter. The formula used means that each letter encrypts to one other letter,
and back again, meaning the cipher is essentially a standard substitution cipher with a rule
governing which letter goes to which.


In the affine cipher the letters of an alphabet of size m are first mapped to the integers in the
range 0 m 1. It then uses modular arithmetic to transform the integer that each plaintext
letter corresponds to into another integer that corresponds to a ciphertext letter. The encryption
function for a single letter is

Encryption:-( a x + b) \ mod {m}

Where modulus m is the size of the alphabet and a and b are the key of the cipher. The value a
must be chosen such that a and m are coprime.

The decryption function is


where a^-1 is the modular multiplicative inverse of a modulo m. I.e., it satisfies the equation

1 = a a^{-1}\mod{m}.

The multiplicative inverse of a only exists if a and m are coprime. Hence without the restriction
on a decryption might not be possible. It can be shown as follows that decryption function is the
inverse of the encryption function,

{D}({E}(x)) &= a^{-1}( {E}(x)-b)\mod{m}

= a^{-1}(((ax+b)\mod{m})-b)\mod{m}

= a^{-1}(a x + b - b)\mod{m} \\
= a^{-1}ax \mod{m}\\

= x\mod{m}.

Program code:

172-line code of affine cipher was created. Following are the code fragments to understand he
working of code.

Following below are the library functions used along with their purpose for usageand were
mandatory for the creation of code.

In the following fragment, string s1 will be used to type the message or text to encrypt or
decrypt. An integer array of s2 is declared so that text written for encryption can be converted
into numeric.

Both c of data type integer and f of data type character were used to control the loop for
multiple inputs and the function getline (cin,s1) is used to enter text.

The following code fragment is used to convert the text into numeric values.
The following code fragment is used to apply the operation 5X+8 where x contains the
numeric values obtained by converting the real text for encryption.

After that, modulus 26 is applied on these values.

Hence, encrypted text is obtained by the code fragment shown below.

After encryption, the encrypted text will be decrypted simply by again converting that text
into corresponding numeric values.

After numeric values are obtained, following operation 21(y-8) where 21 is the inverse of
5, is applied on these numeric values.

Mod 26 will be applied on the values obtained later and if negative values are obtained then
they will be converted to positive values by adding 26 to them as shown in following code
After that, decrypted text is displayed right on the screen.

Problems & Solution

One of the problem is the usage of capital and small letters. As ASCII codes assigned to
small alphabets are different than capital alphabets, so in order to remove this problem, a
string function toupper was used to convert the whole text into capital letters as shown in
following code fragment.

Another problem was that whenever we use space while writing text, the whole program
crashes. In order to avoid it, we use the library function #include<algorithm> and STL
algorithm as shown in code fragment give below. It reduces the spaces in the text from
beginning to end and hence no matter how much spaces are provided to the text, program
will not crash anymore.
STL Algorithm:

To use program multiple times, a loop is inserted so that at the end of program whenever you
press R or r key, you can use it multiple times and if you want to quit, just press any
other key and the program will end.


This program is created on the basis of learning process so every calculation is displayed on
purpose for proper understanding. Following figure will show the working of affine cipher on C+