Cryptography & Network Security LAB#2 2021F-BCE-029
LAB#02
Classical Encryption Techniques
(Mono alphabetic Ciphers)
OBJECTIVE To implement the following algorithms in MATLAB:
1. Monoalphabetic cipher
• Additive Cipher
• Multiplicative Cipher
• Affine Cipher
2. Monoalphabetic Substitution cipher
Task1:
Implement Ceasar Cipher both encryption and decryption on the plaintext
“cryptographyandnetworksecurity” by taking key is equal to your roll number.
Code:
plaintext = 'cryptographyandnetworksecurity';
key = 29; end
ciphertext = char(zeros(1,
length(plaintext))); for i = 1:length(ciphertext)
decrypted = char(zeros(1, idx = mod((i - key - 1),
length(plaintext))); length(plaintext)) + 1;
decrypted(i) = ciphertext(idx);
for i = 1:length(plaintext) end
idx = mod((i + key - 1),
length(plaintext)) + 1; disp(['Encrypted: ', ciphertext]);
ciphertext(i) = plaintext(idx); disp(['Decrypted: ', decrypted]);
Output:
Task2:
Implement multiplicative Cipher both encryption and decryption on the plaintext
“themessageisnoteasytodecrypt” by taking any key by the user.
Code
plaintext = key_inv = -1;
'themessageisnoteasytodecrypt'; for i = 1:n
n = length(plaintext); if mod(key * i, n) == 1
prompt = "Plz enter any key "; key_inv = i;
key = input(prompt); break;
end
% Find multiplicative inverse of end
key modulo n
Cryptography & Network Security LAB#2 2021F-BCE-029
if key_inv == -1 % Decryption
disp('Multiplicative inverse for i = 1:n
does not exist. Choose a different idx = mod((i * key_inv -
key.'); 1), n) + 1;
else decrypted(idx) =
ciphertext = char(zeros(1, n)); ciphertext(i);
decrypted = char(zeros(1, n)); end
% Encryption disp(['Plaintext: ',
for i = 1:n plaintext]);
idx = mod((i * key - 1), n) disp(['Encrypted: ',
+ 1; ciphertext]);
ciphertext(idx) = disp(['Decrypted: ',
plaintext(i); decrypted]);
end end
Output:
Task3:
Implement Affine Cipher both encryption and decryption on the plaintext “thekeyshouldbeasecretkey”
by taking any pair of key.
Code:
plaintext =
'thekeyshouldbeasecretkey'; % Decryption - Inverse of k1
k1 = 5; k2 = 8; n = 26; k1_inv = -1;
plaintext_upper = upper(plaintext); for i = 1:n, if mod(k1 * i, n) ==
ciphertext = char(zeros(1, 1, k1_inv = i; break; end, end
length(plaintext)));
decrypted = char(zeros(1, if k1_inv ~= -1
length(plaintext))); for i = 1:length(ciphertext)
char_c = ciphertext(i);
% Encryption if 'A'<=char_c &&
for i = 1:length(plaintext) char_c<='Z'
char_p = plaintext_upper(i); C = char_c - 'A';
if 'A'<=char_p && char_p<='Z' P = mod(((C - k2) *
P = char_p - 'A'; k1_inv), n);
C = mod((P * k1 + k2), n); decrypted(i) = char(P +
ciphertext(i) = char(C + 'A');
'A'); else, decrypted(i) =
else, ciphertext(i) = char_p; char_c; end
end end
end
Cryptography & Network Security LAB#2 2021F-BCE-029
disp(['Encrypted: ', else, disp('Inverse of k1 does not
ciphertext]); exist!'); end
disp(['Decrypted: ',
decrypted]);
Output:
Task4:
Encrypt and decrypt the text using monoalphabetic substitution cipher. The key arrangement should be
different for each student. The plain text is same as in task 1.
Code:
plaintext = if isletter(char_c)
'cryptographyandnetworksecurity'; is_upper = (char_c >= 'A'
alphabet = && char_c <= 'Z');
'abcdefghijklmnopqrstuvwxyz'; char_lower = lower(char_c);
original_char_lower =
rng('default'); alphabet(key_alphabet ==
key_alphabet = char_lower);
alphabet(randperm(26)); decryptedtext =
[decryptedtext,
ciphertext = ''; char_case(original_char_lower,
for char_p = plaintext is_upper)];
if isletter(char_p) else, decryptedtext =
is_upper = (char_p >= 'A' [decryptedtext, char_c]; end
&& char_p <= 'Z'); end
char_lower = lower(char_p);
cipher_char_lower = disp(['Key Alphabet: ',
key_alphabet(alphabet == key_alphabet]);
char_lower); disp(['Encrypted: ', ciphertext]);
ciphertext = [ciphertext, disp(['Decrypted: ',
char_case(cipher_char_lower, decryptedtext]);
is_upper)];
else, ciphertext = [ciphertext, function char_out =
char_p]; end char_case(char_in, is_upper)
end if is_upper, char_out =
upper(char_in); else, char_out =
decryptedtext = ''; char_in; end
for char_c = ciphertext end
Output:
Cryptography & Network Security LAB#2 2021F-BCE-029