[go: up one dir, main page]

0% found this document useful (0 votes)
25 views8 pages

Lab 7 (1) 094

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Ayesha Nasim CE-094-2020

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);

demo = mod(length(plaintext), key_length);


if demo > 0
plaintext = [plaintext, repmat('x', 1, key_length - demo)];
end
% display(['New Plain text: ', plaintext]);
plaintext_mat = reshape(plaintext, key_length, []).';

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);

display('Affine Cipher Algorithm ROUND 2')


PT3 = PTleft;
%Encryption
for i = 1:length(PT3)
PT3_no(i) = find(letter==PT3(i))-1;
end
CT_no = mod(PT3_no*key1+key2,26);
CT_affine1 = letter(CT_no+1);
display(['Encrypted Text:', CT_affine1]);

display('Monoalphabetic Cipher Algorithm ROUND 2')


% Encryption
ciphertext1 = '';
for i = 1:length(CT_affine1)
if isletter(CT_affine1(i))
Index = find(alphabet == CT_affine1(i));
if ~isempty(Index)
ciphertext1 = [ciphertext1, key4(Index)];
else
ciphertext1 = [ciphertext1, PText(i)];
end
else
ciphertext1 = [ciphertext1, PText(i)];
end
end
display(['Encrypted Text:', ciphertext1]);

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]);

display('2nd Decryption round 2')


%Decryption
[a,b] = gcd(key1,26);
if (a==1)
k_inverse = mod(b,26);
for i = 1:length(decryptedText)
CT_no(i) = find(letter==decryptedText(i))-1;
end
PT_no = mod((CT_no-key2)*k_inverse,26);
PT4 = letter(PT_no+1);
display(['Decrypted Text:', PT4]);
end

display('1st Decryption round 1')


% Decryption
block_size = ciphertext_len / key_length;
plaintext_dec = '';
ciphertext_mat = (reshape(ciphertext, key_length, []).');
ciphertext_mat = transpose(ciphertext_mat);
x = 1;
% ciphertext_matrix(35)
[m, n] = size(ciphertext_mat);
for i = 1:m
for k = 1:n
if (x<=numel(ciphertext_mat))
x;
new_ciphertext_mat(k,i)= ciphertext_mat(x);
else
end
x = x + 1;
end
end
new_ciphertext_mat = new_ciphertext_mat(:, key3);
new_ciphertext_mat = transpose(new_ciphertext_mat);
for i = 1:numel(new_ciphertext_mat)
plaintext_dec = strcat(plaintext_dec, new_ciphertext_mat(i));
end
plaintext_dec=plaintext_dec(1:5);
display(['Decrypted Text: ', plaintext_dec])

display('2nd Decryption round 1')


% Rearranging the encrypted text in a transposed format as per the Rail Fence pattern
rows = ceil(length(plaintext_dec) / depth);
matrix_decrypt = char(zeros(rows, depth));
% Fill the transposed array
index = 1;
for col = 1:depth
for row = 1:rows

5
SSUET/QR/114
Ayesha Nasim CE-094-2020

if index <= length(plaintext_dec)


matrix_decrypt(row, col) = plaintext_dec(index);
index = index + 1;
end
end
end
decrypt1_text = '';
for row = 1:rows
for col = 1:depth
if (row ~= rows) || (col <= len - (numColumn - 1) * depth)
decrypt1_text = [decrypt1_text, matrix_decrypt(row, col)];
end
end
end
display(['Decrypted Text: ', decrypt1_text]);

display('3rd Decryption round 1')


%Decryption
[a,b] = gcd(key1,26);
if (a==1)
k_inverse = mod(b,26);
for i = 1:length(decrypt1_text)
CT_no(i) = find(letter==decrypt1_text(i))-1;
end
PT_no = mod((CT_no-key2)*k_inverse,26);
PT5 = letter(PT_no+1);
display(['Decrypted Text:', PT5]);
end

% 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]

Result and Discussion


OPEN ENDED LAB

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

You might also like