0% found this document useful (0 votes)
35 views3 pages

Hill Cipher

Uploaded by

tarunudayakumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

Hill Cipher

Uploaded by

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

Shift cipher

plaintext=input('Enter the message : ','s');


key=input('Enter key value:');
%% Encryption
plainASCII=abs(plaintext);
alphapos=mod(plainASCII,65);
cipher=alphapos+key;
ciphertext=mod(cipher,26)+65;
sprintf('Encrypted text : %s',char(ciphertext))

%% Decryption
decrypos=mod(ciphertext,65);
decrypt=decrypos-key;
decrypttext=mod(decrypt,26)+65;
sprintf('Decrypted text : %s’,char(decrypttext))

Autokey
clear all;
clc;
plaintext=input('Enter the message : ','s');
key=input('Enter key value:');
%% Encryption
Encryptionkey=key;
plaintextpos=mod(plaintext,65);
for i=1:length(plaintextpos)
cipher(i)=plaintextpos(i)+Encryptionkey;
Encryptionkey=plaintextpos(i);
end
cipheralphapos=mod(cipher,26);
ciphertext=char(cipheralphapos+65);
fprintf('Encrypted text : %s\n',ciphertext)
%% Decryption algorithm
Decryptionkey=key;
ciphertextpos=mod(ciphertext,65);
for i=1:length(ciphertextpos)
decryptedvalue=ciphertextpos(i)-Decryptionkey;
decrypt(i)=mod(decryptedvalue,26);
Decryptionkey=decrypt(i);
end
decrypttext=char(decrypt+65)
fprintf('Decrypted text : %s\n’,decrypttext)

Hill cipher
clear all;
clc;
plaintext=input('Enter the message : ','s');
charpos=abs(plaintext)-65;
key=[5 8;17 3];
%key=[17 17 5; 21 18 21;2 2 19]
inversekey=[9 2; 1 15];
%inversekey=[4 9 15; 15 17 6; 24 0 17];
%% Initialize the block
Blksize=size(key,1);
msglength=length(plaintext);
% Find the number Letters to pad in a block
if mod(msglength, Blksize)== 0
pad=0;
else
pad= Blksize -mod(msglength, Blksize);
end
% Padding characters 'Z' at the end of the message
for i=1:pad
plaintext=[plaintext 'Z'];
end
%% Encryption Algorithm
plaintextblock=reshape(plaintext, Blksize,[])';
plaintextpos=mod(plaintextblock,65);
ciphervalue=mod(plaintextpos*key,26);
cipher=reshape(ciphervalue',1,[]);
ciphertext=char(cipher+65);
fprintf('Encrypted text : %s\n',ciphertext)
%% Decryption Algorithm
receiver=reshape(ciphertext, Blksize,[])';
receivertextpos=mod(receiver,65);
decipherpos=mod(receivertextpos*inversekey,26);
decipher=char(decipherpos+65);
deciphertext=reshape(decipher',1,[]);
fprintf('Decrypted text : %s\n’,deciphertext);

Transposition
clc;
clear all;
plain_text = input('Enter the plain_text','s');
key = input('Enter the key ');
l = length(key);
m=length(plain_text);

bogus_count = mod(l-mod(m,l),l);
if bogus_count
    plain_text(end+1:end+bogus_count)='~';
end

x=reshape(plain_text,l,[])';

for i=1:size(x,1)
    slice = x(i,1:l);
    cipher(i,1:l) = slice(key);
end

fprintf('Cipher Text : %s \n' ,reshape(cipher',1,[]));

inv_key = arrayfun(@(index) find(key==index),1:l);

fprintf('Inverse key ');


disp(inv_key);

for i=1:size(x,1)
    slice = cipher(i,1:l);
    decrypted_message(i,1:l) = slice(inv_key);
end

Rsa
clc;
clear all;
close all;
p=input('enter the value for p: ');
q=input('enter the value for q: ');
n=p.*q;
phi=(p-1).*(q-1);
for i=2:(phi-1)
if(gcd(i,phi)==1)
e=i;
break;
end
end
disp('the public key e is found ');
disp(e);
d=modinv(e,phi);
disp('the private key d is found');
disp(d);
m=input('enter the message: ','s');
ascii=abs(m);
disp('the ascii value of the entered data is as follows: ');
disp(ascii);
%% Encryption Algorithm
pow=mod(power(m,e),n)
c=pow;
disp('the ascii value of the encrypted message is as follows: ');
disp(c);
cipher=char(c);
disp('the encrypted message is as follows: ');
disp(cipher);
%% Decryption Algorithm
k=1;
g=0;
while(g<d)
g=g+1;
k=mod(c.*k,n);
end
deascii=k;
disp('the ascii value of the decrypted message is as follows: ');
disp(deascii);
desrypt_message=char(deascii);
disp('the decrypted message is as follows: ');
disp(desrypt_message);
%% Inverse not exist
if(isempty(d))
disp('the inverse does not exist for the data!!!!!!');
end
%% To calculate modulo inverse [ To determine decryption key ]
function [d]=modinv(e,phi)
d=[];
for k=0:(phi-1)
if(mod((e.*k),phi)==1)
d=k;
break;
end
end

You might also like