You are on page 1of 8

University of Engineering &

Technology, Taxila

LAB REPORT 2
Classical Cipher I
Wireless Networks-Lab
(CP-404)

LAB INSTRUCTOR
Dr. Asif Khan

SUBMITTED BY
Hareem Khan
17-CP-29

Faculty of Telecommunication & Information Engineering


B.Sc. Computer Engineering
Classical Cipher I
OBJECTIVES
The objective of this lab session is to understand the Caesar Cipher Encryption and
Decryption Algorithms.

Question #1: Encryption and Decryption with Caesar Cipher


a) Implement encryption/decryption functions that take a key (as an integer in 0, 1,2, … ,
25), and a string. The function should only operate on the characters ‘a’, ‘b’, … ‘z’
(both upper and lower case), and it should leave any other characters, unchanged.

Caesar Cipher Encryption Caesar Cipher Decryption

CODE CODE
function Encryption = function decryption =
Task_1_1(plain_text,key) Task_1_1(Cipher_text,key)
fprintf('Plain Text : %s fprintf('Cipher Text : %s
\n',plain_text) \n',Cipher_text)
fprintf('Key : %d \n',key) fprintf('Key : %d \n',key)

D_plain_text = double(plain_text); D_cipher_text =


for i = 1:length(D_plain_text) double(Cipher_text);
if D_plain_text(i) >= 65 && for i = 1:length(D_cipher_text)
D_plain_text(i) <= 90 % character if D_cipher_text(i) >= 65 &&
is uppercase alphabet D_cipher_text(i) <= 90 % character
min = 65; is uppercase alphabet
max = 90; min = 65;
D_plain_text(i) = max = 90;
mod(((D_plain_text(i) - min) + D_cipher_text(i) =
key),((max - min) + 1)) + min; %c- mod(((D_cipher_text(i) - min) -
min makes the range of c from 65-90 key),((max - min) + 1)) + min; %c-
to 0-25, c+min makes the range of c min makes the range of c from 65-90
back to 65-90 to 0-25, c+min makes the range of c
elseif D_plain_text(i) >= 97 && back to 65-90
D_plain_text(i) <= 122 % character elseif D_cipher_text(i) >= 97 &&
is lowercase alphabet D_cipher_text(i) <= 122 % character
min = 97; is lowercase alphabet
max = 122; min = 97;
D_plain_text(i) = max = 122;
mod(((D_plain_text(i) - min) + D_cipher_text(i) =
key),((max - min) + 1)) + min; % c- mod(((D_cipher_text(i) - min) -
min makes the range of c from 97- key),((max - min) + 1)) + min; % c-
122 to 0-25, c+min makes the range min makes the range of c from 97-
of c back to 97-122 122 to 0-25, c+min makes the range
end of c back to 97-122
D_cipher_text(i)=D_plain_text(i); end
end
Encryption = char(D_plain_text); D_plain_text(i)=D_cipher_text(i);
fprintf('Cipher Text : %s end
\n',Encryption) decryption = char(D_plain_text);
end fprintf('Plain Text : %s
\n',decryption)
end
OUTPUT OUTPUT

b) Implement a function that performs a brute force attack on a ciphertext, it should


print a list of the keys and associated decryptions. It should also take an optional
parameter that takes a substring and only prints out potential plaintexts that contain
that decryption.

function BruteForce = Task_1_2(Cipher_text)


D_cipher_text = double(Cipher_text);
for key = 1:26
for i = 1:length(D_cipher_text)
if D_cipher_text(i) >= 65 && D_cipher_text(i) <= 90 % character is
uppercase alphabet
min = 65;
max = 90;
D_cipher_text(i) = mod(((D_cipher_text(i) - min) - key),((max -
min) + 1)) + min; %c-min makes the range of c from 65-90 to 0-25, c+min
makes the range of c back to 65-90
elseif D_cipher_text(i) >= 97 && D_cipher_text(i) <= 122 %
character is lowercase alphabet
min = 97;
max = 122;
D_cipher_text(i) = mod(((D_cipher_text(i) - min) - key),((max -
min) + 1)) + min; % c-min makes the range of c from 97-122 to 0-25, c+min
makes the range of c back to 97-122
end
D_plain_text(i)=char(D_cipher_text(i));

end
fprintf('plain text for key %d is %s \n',key,D_plain_text);
end
end

c) Show the output of your encrypt function (part a) on the following (key, plaintext)
pairs:
• k = 6 plaintext = "Get me a vanilla ice cream, make it a double."

• k = 15 plaintext = "I don't much care for Leonard Cohen."


• k = 16 plaintext = "I like root beer floats."

d) Show the output of your decrypt function (part a) on the following (key, ciphertext)
pairs:
• k = 12 ciphertext = 'nduzs ftq buzq oazqe.'

• k = 3 ciphertext = "fdhvdu qhhgv wr orvh zhljkw."

• k = 20 ciphertext = "ufgihxm uly numnys."

e) Show the output of your attack function (part c) on the following ciphertext, if an
optional
keyword is specified, pass that to your attack function:
• ciphertext = 'baeeq klwosjl osk s esf ozg cfwo lgg emuz.' no keyword
Question # 2: Cryptanalysis of classical cipher
The following examples show a plaintext and its corresponding ciphertext. What do you
think is the key?
1. Plaintext: hello Ciphertext: KHOOR

function key = Task_2(PT,CT)


fprintf('Plain Text : %s \n',PT);
fprintf('Cipher Text : %s \n',CT);
D_PT = double(PT);
D_CT = double(CT);
for i = 1:length(D_PT)
k(i) = mod((D_PT(i)-D_CT(i)),26);
end
key=k;
end

Here Caesar cipher algorithm is used for encryption that’s why key is same.

2. Plaintext: hello Ciphertext: ABNZF


Here Playfair cipher algorithm is used for encryption that’s why key is different for every
alphabet.

Question # 3: Letter Frequency Attack on shift cipher


This question is to implement some functions useful to performing classical cipher attacks.
a) Implement a function that performs frequency attacks on a mono-alphabetic
substitutionciphers. This function should take a ciphertext string compute a
histogram of the incidence each letter (ignoring all non alphabet characters.) And
return a list of pairs (letter, incidence percentage) sorted by incidence percentage.

function freq_analysis = Task_3(text)


% This function tabulates the amount of times each character ’a’-’z’

for k = 1:length(text)
c_arr{k} = char(text(k));
end
categ_arr = categorical(c_arr);
%G = double(text); %this is histogram of ascii values
freq_analysis = histogram(categ_arr);
xlabel('alphabets');
ylabel('frequency of alphabets');
title('frequency letter analysis');
end

b) Implement a function that takes a partial mono-alphabetic substitution and a


ciphertext and returns a potential plaintext. The partial mono-alphabetic
substitution should be specified as follows: As a 26 character string where the
character at position i is the substitution of ith character of the alphabet, OR an
underscore ‘_’ if the corresponding substitution is unknown. The potential
plaintext should be the ciphertext with values specified by the mono-alphabetic
substitution replaced by the lower-case plaintext. If the corresponding character
is unknown (i.e. ‘_’ in the monoalphabetic substitution cipher) print the cipher
text as an uppercase character.)
function freq_analysis = Task_3(Cipher_text)
% This function tabulates the amount of times each character ’a’-’z’
key=3;
for k = 1:length(Cipher_text)
c_arr1{k} = char(Cipher_text(k));
end
categ_arr1 = categorical(c_arr1);
%G = double(text); %this is histogram of ascii values
D_cipher_text = double(Cipher_text);
for i = 1:length(D_cipher_text)
if D_cipher_text(i) >= 65 && D_cipher_text(i) <= 90 % character is
uppercase alphabet
min = 65;
max = 90;
D_cipher_text(i) = mod(((D_cipher_text(i) - min) - key),((max -
min) + 1)) + min; %c-min makes the range of c from 65-90 to 0-25, c+min
makes the range of c back to 65-90
elseif D_cipher_text(i) >= 97 && D_cipher_text(i) <= 122 % character is
lowercase alphabet
min = 97;
max = 122;
D_cipher_text(i) = mod(((D_cipher_text(i) - min) - key),((max -
min) + 1)) + min; % c-min makes the range of c from 97-122 to 0-25, c+min
makes the range of c back to 97-122
end
D_plain_text(i)=D_cipher_text(i);
end
fprintf('Cipher Text : %s \n',Cipher_text)
fprintf('Key : %d \n',key)
decryption = char(D_plain_text);
fprintf('Plain Text : %s \n',D_plain_text)
for k = 1:length(decryption)
c_arr{k} = char(decryption(k));
end
categ_arr = categorical(c_arr);
%G = double(text); %this is histogram of ascii values
subplot(2,1,1)
freq_analysis = histogram(categ_arr);
xlabel('alphabets');
ylabel('frequency of alphabets');
title('frequency letter analysis of plain text');
subplot(2,1,2)
freq_analysis1 = histogram(categ_arr1);
xlabel('alphabets');
ylabel('frequency of alphabets');
title('frequency letter analysis of cipher text');

end
c) Use your functions from (a) and (b) to decrypt the following cipher text:"ztmn
pxtne cfa peqef kecnp cjt tmn zcwsenp ontmjsw ztnws tf wsvp xtfwvfefw, c feb
fcwvtf, xtfxevqea vf gvoenwk, cfa aeavxcwea wt wse rntrtpvwvtf wscw cgg lef cne
xnecwea eymcg."

You might also like