0% found this document useful (1 vote)
868 views

Encryption and Decryption in Matlab

The document describes implementations of Caesar cipher, Playfair cipher and Hill cipher encryption and decryption in MATLAB. It includes code snippets and sample outputs for encrypting and decrypting text using each cipher with different keys and plaintexts.

Uploaded by

SHREE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
868 views

Encryption and Decryption in Matlab

The document describes implementations of Caesar cipher, Playfair cipher and Hill cipher encryption and decryption in MATLAB. It includes code snippets and sample outputs for encrypting and decrypting text using each cipher with different keys and plaintexts.

Uploaded by

SHREE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

BAPUJI EDUCATIONAL ASSOCIATION ®

BAPUJI INSTITUTE OF ENGINEERING AND TECHNOLOGY DAVANAGERE – 577004

DEPARTMENT OF ELECTRONICS & COMMUNICATION


ENGINEERING 2020 – 21

ASSIGNMENT ON MODULE 2

IMPLEMENTATION OF DIFFERENT
DIFFERENT CIPHERS IN MA
MATLAB

Course: : CRYPTOGRAPHY
Subject Code : 17EC744
Semester/Section : VII / ‘A’
Submission Date : 12 – 01 – 2021
Implementation of different ciphers in matlab

CEASER CIPHER ENCRYPTION


clc;
clear all;
close all;
p=input('Enter the plain text:\n');
p=double(lower(p))-double('a');
k=input('Enter the key:\n');
C=mod(p+k,26);
disp('Cipher is:');
disp(char(C+double('A')));

OUTPUTS

Example 1 Example 2 Example 3


Enter the plain text: Enter the plain text: Enter the plain text:
'meetmeafterthetogaparty' 'security' 'hello'
Enter the key: Enter the key: Enter the key:
3 10 12
Cipher is: Cipher is: Cipher is:
PHHWPHDIWHUWKHWRJDSDUWB COMEBSDI TQXXA

CEASER CIPHER DECRYPTION


clc;
clear all;
close all;
C=input('Enter the cipher text:\n');
C=double(upper(C))-double('A');
k=input('Enter the key:\n');
p=mod(C-k,26);
disp('Cipher is:');
disp(char(p+double('a')));

OUTPUTS

Example 1 Example 2 Example 3


Enter the cipher text: Enter the cipher text: Enter the cipher text:
'PHHWPHDIWHUWKHWRJDSDUWB' 'COMEBSDI' 'WTAAD'
Enter the key: Enter the key: Enter the key:
3 10 15
Cipher is: Cipher is: Cipher is:
meetmeafterthetogaparty security hello

~1~
Assignment on module 2 Cryptography[17EC744]
Implementation of different ciphers in matlab

PLAYFAIR CIPHER ENCRYPTION


clc;
clear all;
close all;
k = input('Enter the key within single quotes:\n');
k = double(upper(k))-double('A');
k = remove_repeated(k);
a = append_alphabets(k);
%Considering only first letter among I and J
for i=1:26
if(a(i)==8|a(i)==9)
break;
end
end
for (j=i+1:26)
if(a(j)==8|a(j)==9)
break;
end
end
a(j:25)=a(j+1:26);
a=a(1:25);
%Converting row playfair matrix into 5X5 matrix
for N=1:5
b=((N-1)*5)+1;
M(N,1:5)=a(b:b+5-1);
end
disp('Playfair matrix is:-');
disp(char(M+double('A')));
p=input('Enter the plain text in single quotes:\n');
filler=input('Enter the filler in single quotes:\n');
p=double(p)-double('a');
p=[p,27];
%To add filler letters in repeated letters of pair of plain text
i=1;
while p(i)~=27
if((p(i)==p(i+1))&&(mod(i,2)==1))
p((i+2):(length(p)+1)) = p((i+1):length(p));
p(i+1) = double(lower(filler)-double('a'));
end
i = i+1;
end
%Adding filler to last left out letter
p = p(1:length(p)-1);
if(mod(length(p),2)==1)
p=[p,double(lower(filler)-double('a'))];
end
disp('Augmented plain text is;-');disp(char(p+double('a')));

C=27;%Initializing the cipher with a non-alphabet


%Assigning Cipher text values
for i=1:2:(length(p)-1)
[r1 r2 c1 c2] = rcsearch(p(i),p(i+1),M);

~2~
Assignment on module 2 Cryptography[17EC744]
Implementation of different ciphers in matlab

if(r1==r2)
if c1 == 5;
c1=0;
end
if c2 == 5;
c2=0;
end
C = [C,M(r1,(c1+1)),M(r1,(c2+1))];
elseif (c1==c2)
if r1 == 5;
r1=0;
end
if r2 == 5;
r2=0;
end
C = [C,M((r1+1),c1),M((r2+1),c1)];
elseif ((r1~=r2)&&(c1~=c2))
C = [C,M(r1,c2),M(r2,c1)];
end
end
C=C(2:length(C));
disp('The Cipher text is:-');disp(char(C+double('A')));

FUNCTIONS USED IN CODE

remove_repeated.m rcsearch.m
function [k1] = function[r1 r2 c1 c2] = rcsearch(a,b,M)
remove_repeated(k) for i=1:5
ptr = 1; for j=1:5
for i=1:length(k) if M(i,j)==a
for j=i:-1:1 r1=i;
if (i~=j)&&(k(j)==k(i)) c1=j;
found = 1; end
break; end
else for i=1:5
found = 0; for j=1:5
end if M(i,j)==b
end r2=i;
if found == 0 c2=j;
k1(ptr) = k(i); end
ptr = ptr+1; end
end end
end end
end

PTO

~3~
Assignment on module 2 Cryptography[17EC744]
Implementation of different ciphers in matlab

append_alphabets.m
function [a] = append_alphabets(k)
there_is_match = 1;
ptr = 1;
for i=0:25
for j = 1:length(k)
if k(j)==i
there_is_match = 1;
break;
else
there_is_match = 0;
end
end
if there_is_match == 0
a(ptr) = i;
ptr = ptr+1;
end
end
a = [k,a];
end

OUTPUTS

Example 1 Example 2
Enter the key within single quotes: Enter the key within single quotes:
'VISVESVARAYA' 'GUIDANCE'
Playfair matrix is:- Playfair matrix is:-
VISEA GUIDA
RYBCD NCEBF
FGHKL HKLMO
MNOPQ PQRST
TUWXZ VWXYZ
Enter the plain text in single quotes: Enter the plain text in single quotes:
'happyengineersday' 'thekeyishidden'
Enter the filler in single quotes: Enter the filler in single quotes:
'x' 'x'
Augmented plain text is;- Augmented plain text is;-
hapxpyengineersday thekeyishidxdenx
The Cipher text is:- The Cipher text is:-
LSXENCIPNYPIVCABID POCLBXDRLGIYIBEV

~4~
Assignment on module 2 Cryptography[17EC744]
Implementation of different ciphers in matlab

HILL CIPHER ENCRYPTION


clc;
clear all;
close all;
hill_ord=input('Enter the order of hill cipher like (for 3x3-3; for 2x2-2
and so on...):\n');
choice=input('How do you enter key matrix?: 1.Alphabet 2.Matrix form:\n');
if choice==1
k=input('Enter the key within single quotes:\n');
k=double(lower(k))-double('a');
append_value=((ceil(length(k)/hill_ord))*(hill_ord))-length(k);
k=[k,k(1:append_value)];
for N=1:(length(k)/hill_ord)
a=((N-1)*hill_ord)+1;
k1(N,1:hill_ord)=k(a:a+hill_ord-1);
end
k=k1
else
k=input('Enter the key in matrix format:\n')
end
p=input('Enter the plain text:\n');
p=double(lower(p))-double('a');
p_append_value=(ceil(length(p)/hill_ord)*hill_ord)-length(p);
filler=double(input('Enter the filler letter in single quotes:\n')-
double('a'));
p=[p,ones(1:p_append_value)*filler];
for(N=1:length(p)/hill_ord)
a=((N-1)*hill_ord)+1;
C(a:a+hill_ord-1)=mod((p(a:a+hill_ord-1)*k),26);
end
disp("The Cipher text is:")
disp(char(C+double('A')))

OUTPUTS

Example 1
Enter the order of hill cipher like (for 3x3-3; for 2x2-2 and so on...):
2
How do you enter key matrix?: 1.Alphabet 2.Matrix form:
2
Enter the key in matrix format:
[9 4;5 7]

k =

9 4
5 7

Enter the plain text:


'honesteffort'
Enter the filler letter in single quotes:
'x'
The Cipher text is:
DWHCXXJZLOOT

~5~
Assignment on module 2 Cryptography[17EC744]
Implementation of different ciphers in matlab

Example 2
Enter the order of hill cipher like (for 3x3-3; for 2x2-2 and so on...):
3
How do you enter key matrix?: 1.Alphabet 2.Matrix form:
2
Enter the key in matrix format:
[17 17 5;21 18 21;2 2 19]

k =

17 17 5
21 18 21
2 2 19

Enter the plain text:


'paymoremoney'
Enter the filler letter in single quotes:
'x'
The Cipher text is:
RRLMWBKASPDH

Example 3
Enter the order of hill cipher like (for 3x3-3; for 2x2-2 and so on...):
4
How do you enter key matrix?: 1.Alphabet 2.Matrix form:
2
Enter the key in matrix format:
[9 7 11 13;4 7 5 6;2 21 14 9;3 23 21 06]

k =

9 7 11 13
4 7 5 6
2 21 14 9
3 23 21 6

Enter the plain text:


'codeisready'
Enter the filler letter in single quotes:
'x'
The Cipher text is:
OHKFIHGZZOCI

~6~
Assignment on module 2 Cryptography[17EC744]
Implementation of different ciphers in matlab

Example 4
Enter the order of hill cipher like (for 3x3-3; for 2x2-2 and so on...):
3
How do you enter key matrix?: 1.Alphabet 2.Matrix form:
1
Enter the key within single quotes:
'tutorials'

k =

19 20 19
14 17 8
0 11 18

Enter the plain text:


'hidemoney'
Enter the filler letter in single quotes:
'x'
The Cipher text is:
LXRKWIRUJ

HILL CIPHER DECRYPTION


clc;
clear all;
close all;
hill_ord=input('Enter the order of hill cipher like "for 3x3-3; for 2x2-
2":\n');
choice=input('How do you enter key matrix?: 1.Alphabet 2.Matrix form:\n');
if choice==1
k=input('Enter the key within single quotes:\n');
k=double(lower(k))-double('a');
append_value=((ceil(length(k)/hill_ord))*(hill_ord))-length(k);
k=[k,k(1:append_value)];
for N=1:(length(k)/hill_ord)
a=((N-1)*hill_ord)+1;
k1(N,1:hill_ord)=k(a:a+hill_ord-1);
end
k=k1
else
k=input('Enter the key in matrix format:\n')
end
r = [26,mod(det(k),26)];q = [0 0];x = [1 0];y = [0 1];
i = 3;
while r(i-1)~=0
q(i) = floor((r(i-2)/r(i-1)));
r(i) = mod(r(i-2),r(i-1));
x(i) = x(i-2)-(q(i)*x(i-1));
y(i) = y(i-2)-(q(i)*y(i-1));
i = i+1;
end
if(r(i-2)~=1)
disp('Please give a key matrix whose determinant is relatively prime to
26');

~7~
Assignment on module 2 Cryptography[17EC744]
Implementation of different ciphers in matlab

return;
end
detinv = mod(y(i-2),26);
kinv = mod((detinv*round(adjoint(k))),26);
C=input('Enter the cipher text:\n');
C=double(upper(C))-double('A');
p_append_value=(ceil(length(C)/hill_ord)*hill_ord)-length(C);
for(N=1:length(C)/hill_ord)
a=((N-1)*hill_ord)+1;
p(a:a+hill_ord-1)=mod((C(a:a+hill_ord-1)*kinv),26);
end
disp("The plain text is:")
disp(char(p+double('a')))

OUTPUTS

Example 1
Enter the order of hill cipher like (for 3x3-3; for 2x2-2 and so on...):
2
How do you enter key matrix?: 1.Alphabet 2.Matrix form:
2
Enter the key in matrix format:
[9 4;5 7]

k =

9 4
5 7

Enter the cipher text:


'DWHCXXJZLOOT'
The plain text is:
honesteffort

Example 2
Enter the order of hill cipher like (for 3x3-3; for 2x2-2 and so on...):
3
How do you enter key matrix?: 1.Alphabet 2.Matrix form:
2
Enter the key in matrix format:
[17 17 5;21 18 21;2 2 19]

k =

17 17 5
21 18 21
2 2 19

Enter the cipher text:


'RRLMWBKASPDH'
The plain text is:
paymoremoney

~8~
Assignment on module 2 Cryptography[17EC744]
Implementation of different ciphers in matlab

Example 3
Enter the order of hill cipher like (for 3x3-3; for 2x2-2 and so on...):
4
How do you enter key matrix?: 1.Alphabet 2.Matrix form:
2
Enter the key in matrix format:
[9 7 11 13;4 7 5 6;2 21 14 9;3 23 21 06]

k =

9 7 11 13
4 7 5 6
2 21 14 9
3 23 21 6

Please give a key matrix whose determinant is relatively prime to 26

~9~
Assignment on module 2 Cryptography[17EC744]

You might also like