Main Script:
clear;
clc; close all;
%% Message Input
message=input('Enter a message to encode: ', 's');
encryption_message=message;
%To call encryption function
en=encryption_fun(encryption_message);
%% To call decryption function
dn=decryption_fun(en);
%% Prints the output
fprintf('\nMessage: %s\n\n',message);
fprintf('Encrypted: %s\n\n',en);
fprintf('Decrypted: %s\n\n',dn);
Save the encryption function in differnt file (Must be in current directory, save file name as encryption_fun.m)
function encryption=encryption_fun(encryption)
%Sets the terms and conditions for encoding characters, and if else then
%mapping them to themselves
for i=1:length(encryption)
x=encryption(i);
if x >= 'A' && x <= 'Z'
if x >= 'U'
x = x - 20;
else
x = x + 6;
end
elseif x>= 'a' && x <= 'z'
if x >= 'u'
x = x - 20;
else
x = x + 6;
end
end
encryption(i) = x;
end
end
Save the decryption function in differnt file (Must be in current directory, save file name as decryption_fun.m)
function decryption=decryption_fun(encryption)
decryption=encryption;
for i=1:length(decryption)
x = decryption(i);
if x >= 'A' && x <= 'Z'
if x <= 'F'
x = x + 20;
else
x = x - 6;
end
elseif x>= 'a' && x <= 'z'
if x <= 'f'
x = x + 20;
else
x = x - 6;
end
end
decryption(i) = x;
end
end
Now run the main code (First one) and results:
Enter a message to encode: Kalyan
Message: Kalyan
Encrypted: Qgregt
Decrypted: Kalyan