Saturday, 7 September 2013

Data Encryption and Decryption[MATLAB]

 
Data Encryption and Decryption[MATLAB]
clc;

clear all;

pt=input('Enter the PlainText: ');

asc=double(pt);

disp(' ');

disp('Monoalphabetic (Caeser Cipher)');

disp(' ');

shift=input('Enter the Number of End-Around Shifts(key): ');

%Encryption

mt=asc;

for i=1:length(mt)

mt(i)=mt(i)+shift;

if mt(i)>127

corr=mt(i)-127;

mt(i)=32+corr-1;

end

end

disp(' ');

disp('ENCRYPTED TEXT: ');

disp(char(mt));

%Decryption

for i=1:length(mt)

mt(i)=mt(i)-shift;

if mt(i)<32

corr=32-mt(i);

mt(i)=127-corr+1;

end

end

disp(' ');

disp('DECRYPTED TEXT: ');

disp(char(mt));

disp(' ');

disp('Polyalphabetic(Trithemius Progressive Key): ');

%Forming the trithemius key table

disp(' ');

type=input('Enter the Leftshift(+1) or Rightshift(-1): ');

start=32:127;

for i=1:96

if type==1

k=96-i+1;

elseif type==-1

k=i+1;

if i==96

k=1;

end

end

for j=1:96

tri(i,j)=start(k);

if k==96

k=0;

end

k=k+1;

end

end

%Encryption

mt=asc;

for i=1:length(mt)

col=mt(i)-31;

mt(i)=tri(i,col);

end

disp(' ');

disp('ENCRYPTED TEXT: ');

disp(char(mt));

%Decryption

for i=1:length(mt)

col=mt(i)-31;

mt(i)=tri(96-i,col);

end

disp(' ');

disp('DECRYPTED TEXT: ');

disp(char(mt));

disp(' ');

disp('Vignere Key Method: ');

disp(' ');

key=input('Enter the Key Word: ');

keyasc=double(key);

keylen=length(key);

%Encryption

mt=asc;

x=1;

for i=1:length(mt)

col=mt(i)-31;

if type==1

row=96-(keyasc(x)-31)+1;

if row==0

row=96;

end

else

row=keyasc(x)-31-1;

if row==0

row=96;

end

end

mt(i)=tri(row,col);

if x==keylen

x=0;

end

x=x+1;

end

disp(' ')

disp('ENCRYPTED TEXT: ');

disp(char(mt));

%Decryption

x=1;

for i=1:length(mt)

col=mt(i)-31;

if type==1

row=(keyasc(x)-31)-1;

if row==0

row=96;

end

else

row=96-(keyasc(x)-31-1);

if row==0

row=96;

end

end

mt(i)=tri(row,col);

if x==keylen

x=0;

end

x=x+1;

end

disp(' ')

disp('DECRYPTED TEXT: ');

disp(char(mt));

disp(' ');

disp('Vignere Auto(plain) Key Method: ');

disp(' ');

auto1=input('Enter the Key Letter: ');

%Encryption

autoasc=double(auto1);

mt=asc;

for i=1:length(mt)

col=mt(i)-31;

if type==1

row=96-(autoasc-31)+1;

if row==0

row=96;

end

else

row=autoasc-31-1;

if row==0

row=96;

end

end

mt(i)=tri(row,col);

autoasc=asc(i);

end

disp(' ')

disp('ENCRYPTED TEXT: ');

disp(char(mt));

%Decryption

autoasc=double(auto1);

for i=1:length(mt)

col=mt(i)-31;

if type==1

row=(autoasc-31)-1;

if row==0

row=96;

end

else

row=96-(autoasc-31-1);

if row==0

row=96;

end

end

mt(i)=tri(row,col);

autoasc=asc(i);

end

disp(' ')

disp('DECRYPTED TEXT: ');

disp(char(mt));

disp(' ');

disp('Vignere Auto(cipher) Key Method: ');

disp(' ');

auto1=input('Enter the Key Letter: ');

%Encryption

autoasc=double(auto1);

mt=asc;

for i=1:length(mt)

col=mt(i)-31;

if type==1

row=96-(autoasc-31)+1;

if row==0

row=96;

end

else

row=autoasc-31-1;

if row==0

row=96;

end

end

mt(i)=tri(row,col);

autoasc=mt(i);

end

disp(' ')

disp('ENCRYPTED TEXT: ');

disp(char(mt));

%Decryption

autoasc=double(auto1);

for i=1:length(mt)

col=mt(i)-31;

if type==1

row=(autoasc-31)-1;

if row==0

row=96;

end

else

row=96-(autoasc-31-1);

if row==0

row=96;

end

end

cipkey=mt(i);

mt(i)=tri(row,col);

autoasc=cipkey;

end

disp(' ')

disp('DECRYPTED TEXT: ');

disp(char(mt));

disp(' ')

disp('RSA Method: ');

disp(' ');

mt=asc;

n=input('Enter the value of n(public): ');

eA=input('Enter the value of eA(public): ');

dA=input('Enter the value of dA(secrer): ');

eB=input('Enter the value of eB(public): ');

dB=input('Enter the value of dB(secrer): ');

%Signing

for i=1:length(mt)

f=mt(i);

mt(i)=1;

for x=1:dA

mt(i)=mod(mt(i)*f,n);

end

end

disp(' ');

disp('SIGNED TEXT: ');

disp(mt);

%Encryption

for i=1:length(mt)

f=mt(i);

mt(i)=1;

for x=1:eB

mt(i)=mod(mt(i)*f,n);

end

end

disp(' ');

disp('ENCRYPTED TEXT: ');

disp(mt);

%Authenitication

for i=1:length(mt)

f=mt(i);

mt(i)=1;

for x=1:dB

mt(i)=mod(mt(i)*f,n);

end

end

disp(' ');

disp('AUTHENICATED TEXT: ');

disp(mt);

%Decryption

for i=1:length(mt)

f=mt(i);

mt(i)=1;

for x=1:eA

mt(i)=mod(mt(i)*f,n);

end

end

disp(' ');

disp('DECRYPTED TEXT: ');

disp(char(mt));

No comments:

Post a Comment