Lab 7 (1) 094
Lab 7 (1) 094
Lab 7 (1) 094
LAB#7
OPEN ENDED LAB-I
Objective
Design and implement a cipher for secure communication that should have the mixture
of atleast two substitution and transposition ciphers for two rounds (mono, affine,rail
and row) in Feistel structure.
It is to be noted that the encrypted text should be converted back into the plaintext.
Hardware/Software required
*Matlab 2013a
Diagram
Methodology
Encryption is the process of translating plain text data (plaintext) into something that
appears to be random and meaningless (ciphertext). Decryption is the process of
converting ciphertext back to plaintext.
1
SSUET/QR/114
Ayesha Nasim CE-094-2020
To encrypt more than a small amount of data, symmetric encryption is used. A symmetric
key is used during both the encryption and decryption processes. To decrypt a particular
piece of ciphertext, the key that was used to encrypt the data must be used.
The goal of every encryption algorithm is to make it as difficult as possible to decrypt
the generated ciphertext without using the key. If a really good encryption algorithm is
used, then there's no technique significantly better than methodically trying every
possible key. For such an algorithm, the longer the key, the more difficult it is to decrypt
a piece of ciphertext without possessing the key.
It's difficult to determine the quality of an encryption algorithm. Algorithms that look
promising sometimes turn out to be very easy to break, given the proper attack. When
selecting an encryption algorithm, it's a good idea to choose one that's been in use for
several years, and has successfully resisted all attacks.
Observation /Code
clc
clear all
display('OPEN ENDED LAB')
PLAIN_TEXT = 'ayesha'
PTright=PLAIN_TEXT(4:end)
PTleft=PLAIN_TEXT(1:3)
alphabet = ['a':'z'];
letter=['a':'z'];
key = 'kvezoyfghijlmnpqbswturxdac';
key4 = 'abcdefghijklmnopqkrstuvdd';
K = 'd';
key1 = find(letter==K)-1;
K2 = 'g';
key2 = find(letter==K2)-1;
key3 = [2, 5, 4, 3, 1, 6];
display('Encryption ')
display('Monoalphabetic Cipher Algorithm')
% Encryption
ciphertext = '';
for i = 1:length(PTright)
if isletter(PTright(i))
Index = find(alphabet == PTright(i));
if ~isempty(Index)
ciphertext = [ciphertext, key(Index)];
else
ciphertext = [ciphertext, PText(i)];
end
else
ciphertext = [ciphertext, PText(i)];
end
2
SSUET/QR/114
Ayesha Nasim CE-094-2020
end
display(['Encrypted Text:', ciphertext]);
PT1 = ciphertext;
display('Affine Cipher Algorithm')
%Encryption
for i = 1:length(PT1)
PT1_no(i) = find(letter==PT1(i))-1;
end
CT_no = mod(PT1_no*key1+key2,26);
CT_affine = letter(CT_no+1);
display(['Encrypted Text:', CT_affine]);
PT2 = CT_affine;
depth = 5;
% Encryption
display('Rail Fence Cipher');
PT2 = regexprep(PT2, '\s+', '');
len = length(PT2);
demo = depth - mod(len, depth);
PT2 = [PT2, repmat('x', 1, demo)];
numRows = depth;
numColumn = ceil(len / depth);
matrix = repmat(' ', numRows, numColumn);
index = 1;
for col = 1:numColumn
for row = 1:numRows
matrix(row, col) = PT2(index);
index = index + 1;
end
end
encrypt_text = '';
for row = 1:numRows
for col = 1:numColumn
if matrix(row, col) ~= ' '
encrypt_text = [encrypt_text, matrix(row, col)];
end
end
end
display(['Encrypted Text: ', encrypt_text]);
display('Row Transposition');
plaintext = encrypt_text;
key_length = length(key3);
3
SSUET/QR/114
Ayesha Nasim CE-094-2020
% Encryption
ciphertext = '';
for k = 1:key_length
column_index = find(key3 == k);
ciphertext = strcat(ciphertext, plaintext_mat(:, column_index).');
end
display(['Encrypted Text: ', ciphertext]);
ciphertext_len = length(ciphertext);
FinalEncryption=[ciphertext1 ciphertext]
display('Decryption ')
% % Decryption
decryptedText = '';
for i = 1:length(ciphertext1)
if isletter(ciphertext1(i))
Index = find(key4 == ciphertext1(i));
if ~isempty(Index)
decryptedText = [decryptedText, alphabet(Index)];
else
decryptedText = [decryptedText, ciphertext1(i)];
end
else
decryptedText = [decryptedText, ciphertext1(i)];
4
SSUET/QR/114
Ayesha Nasim CE-094-2020
end
end
display(['Decrypted Text:', decryptedText]);
5
SSUET/QR/114
Ayesha Nasim CE-094-2020
% Decryption
decryptedText = '';
for i = 1:length(PT5)
if isletter(PT5(i))
Index = find(key == PT5(i));
if ~isempty(Index)
decryptedText = [decryptedText, alphabet(Index)];
else
decryptedText = [decryptedText, PT5(i)];
end
else
decryptedText = [decryptedText, PT5(i)];
end
end
display(['Decrypted Text:', decryptedText]);
FinalDecryption=[PT4 decryptedText]
PLAIN_TEXT =
6
SSUET/QR/114
Ayesha Nasim CE-094-2020
'ayesha'
PTright =
'sha'
PTleft =
'aye'
Encryption
Monoalphabetic Cipher Algorithm
Encrypted Text:wgk
Affine Cipher Algorithm
Encrypted Text:uyk
Rail Fence Cipher
Encrypted Text: uykxx
Row Transposition
Encrypted Text: xuxkyx
Affine Cipher Algorithm ROUND 2
Encrypted Text:gas
Monoalphabetic Cipher Algorithm ROUND 2
Encrypted Text:gar
FinalEncryption =
'garxuxkyx'
Decryption
Decrypted Text:gas
2nd Decryption round 2
Decrypted Text:aye
1st Decryption round 1
Decrypted Text: uykxx
2nd Decryption round 1
Decrypted Text: uyk
3rd Decryption round 1
Decrypted Text:wgk
Decrypted Text:sha
FinalDecryption =
'ayesha'
7
SSUET/QR/114
Ayesha Nasim CE-094-2020
Conclusion
A group of related and adjacent fields of data about a subject or transaction in a database. A
collection of rows makes up a database file. . The key is how many rows is implemented. It can
be guessed by making a brute-force attack. A horizontal set of data or components. In a graph,
it is called the "x-axis." Contrast with column .The Rail Fence algorithm is a simple
cryptography algorithm. However, it is not secure. Affine is a simple polyalphabetic
substitution method that goes from a simple to advanced method. mono algorithm can be
implemented in many encryption projects to make data secure and better.
8
SSUET/QR/114