Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % CEASAR
- %Taking the input from the users
- text=input('Enter the plain text.','s');
- offset=input('Enter the offset value.');
- ciphertext=text;
- for i=1:length(text)
- temp = double(text(i));
- if(temp>=65 && temp<=90)
- %Converting character into ASCII value
- currascii=temp-65;
- %Shifting values by offset
- encryascii=mod(currascii+offset,26);
- %Converting ASCII value back into character
- ciphertext(i)=char(encryascii+65);
- else
- currascii=double(text(i))-97;
- encryascii=mod(currascii+offset,26);
- ciphertext(i)=char(encryascii+97);
- end
- end
- disp("After encryption:");
- disp(ciphertext);
- %Decryption: Subtracting offset from ciphertext to decipher it.
- deciphertext=ciphertext;
- for i=1:length(ciphertext)
- temp = double(ciphertext(i));
- if(temp>=65 && temp<=90)
- currascii=temp-65;
- decryascii=mod(currascii-offset,26);
- deciphertext(i)=char(decryascii+65);
- else
- currascii=double(ciphertext(i))-97;
- decryascii=mod(currascii-offset,26);
- deciphertext(i)=char(decryascii+97);
- end
- end
- disp(deciphertext);
- % MONOALPHABETIC
- clc;
- clear all;
- close all;
- key=randperm(26);
- text='ATTACK';
- ciphertext=text;
- %Encryption
- for i=1:length(text)
- %Subtracting 64 to get index in range [1,26]
- charIndex=double(text(i))-64;
- %mapping characters from text to key
- ciphertext(i)=char(key(charIndex)+64);
- end
- disp(ciphertext);
- %Decryption
- deciphertext=ciphertext;
- for i=1:length(ciphertext)
- charIndex=double(ciphertext(i))-64;
- %finding the index for ciphered character in the array key
- textIndex=find(key==charIndex);
- %adding 64 to map ciphered character back into original character
- deciphertext(i)=char(textIndex+64);
- end
- disp(deciphertext);
- % HILL
- clc;
- clear all;
- close all;
- text='ATTACKNOW';
- n=2;
- while(mod(length(text),n)~=0)
- text=text+"Z";
- text=char(text);
- end
- %Encryption
- key=[2 1; 3 4];
- text=reshape(text,[length(text)/n,n]);
- text1=transpose(text);
- text1=double(text1)-65;
- ciphertext=mod(key*text1,26);
- ciphertext=char(ciphertext+65);
- ciphertext=transpose(ciphertext);
- ciphertext=reshape(ciphertext,1,[]);
- disp(ciphertext);
- %Decryption
- key1=det(key);
- for i=1:1:25
- a = mod(key1*i,26);
- if a==1
- k2=i;
- end
- end
- key_inverse=round(mod(k2*adjoint(key),26));
- ciphertext=double(ciphertext)-65;
- ciphertext=reshape(ciphertext,length(ciphertext)/n,[]);
- ciphertext=transpose(ciphertext);
- deciphertext=mod(key_inverse*ciphertext,26);
- deciphertext=transpose(deciphertext);
- deciphertext=reshape(deciphertext,1,[]);
- deciphertext=char(deciphertext + 65);
- disp(deciphertext);
- %COLUMNAR
- text=input('Enter the message:','s');
- n=input('Enter a number:');
- while(mod(length(text),n)~=0)
- text=text+"Z";
- text=char(text);
- end
- text=upper(text);
- key=randperm(n);
- text=reshape(text,[length(text)/n,n]);
- ciphertext=text;
- [rows,cols]=size(text);
- for i=1:rows
- for j=1:cols
- ciphertext(i,j)=text(i,key(j));
- end
- end
- ciphertext=transpose(ciphertext);
- ciphertext=reshape(ciphertext,1,[]);
- disp(ciphertext);
- %Decryption
- ciphertext=reshape(ciphertext,cols,[]);
- ciphertext=transpose(ciphertext);
- deciphertext=ciphertext;
- for i=1:rows
- for j=1:cols
- deciphertext(i,j)=ciphertext(i,find(key==j));
- end
- end
- deciphertext=reshape(deciphertext,1,[]);
- disp(deciphertext);
- %DOUBLE COLUMNAR
- text=input('Enter the message:','s');
- n1=input('Enter a number:');
- while(mod(length(text),n1)~=0)
- text=text+"Z";
- text=char(text);
- end
- text=upper(text);
- n2=length(text)/n1;
- key1=randperm(n1);
- key2=randperm(n2);
- text=reshape(text,n1,[]);
- ciphertext=text;
- [rows,cols]=size(text);
- for i=1:rows
- for j=1:cols
- ciphertext(i,j)=text(key1(i),key2(j));
- end
- end
- ciphertext=transpose(ciphertext);
- ciphertext=reshape(ciphertext,1,[]);
- disp(ciphertext);
- %Decryption
- ciphertext=reshape(ciphertext,cols,[]);
- ciphertext=transpose(ciphertext);
- deciphertext=ciphertext;
- for i=1:rows
- for j=1:cols
- deciphertext(i,j)=ciphertext(find(key1==i),find(key2==j));
- end
- end
- deciphertext=reshape(deciphertext,1,[]);
- disp(deciphertext);
- % AUTOKEY
- text="ATTACKNOW";
- key=round(rand(1)*26);
- key=char(key+65)+text;
- key=char(key);
- text=char(text);
- ciphertext=text;
- for i=1:length(text)
- a=double(text(i))-65;
- b=double(key(i))-65;
- ciphertext(i)=char(mod(a+b,26)+65);
- end
- disp(ciphertext);
- %Decryption
- deciphertext=ciphertext;
- for i=1:length(ciphertext)
- a=double(ciphertext(i))-65;
- b=double(key(i))-65;
- deciphertext(i)=char(mod(a-b,26)+65);
- end
- disp(deciphertext);
- % VERNAM
- text='ATTACKNOW';
- key=randperm(26,length(text));
- text=double(text)-65;
- ciphertext=bitxor(text,key);
- %ciphertext=mod(ciphertext,26);
- ciphertext=char(ciphertext+65);
- disp(ciphertext);
- %Decryption
- ciphertext=double(ciphertext)-65;
- deciphertext=ciphertext;
- for i=1:length(ciphertext)
- deciphertext(i)=bitxor(key(i),ciphertext(i));
- end
- %deciphertext=mod(deciphertext,26);
- deciphertext=char(deciphertext+65);
- disp(deciphertext);
- % VIGNERE
- text='ATTACKNOW';
- n=3;
- key=randperm(26,3);
- key=char(key+65);
- while (length(key)<length(text))
- key=key+string(key);
- key=char(key);
- end
- key=resize(key,length(text));
- ciphertext=mod(double(text)-'A'+double(key)-'A',26);
- ciphertext=char(ciphertext+'A');
- disp(ciphertext);
- %Decryption
- deciphertext=ciphertext;
- deciphertext=mod(double(ciphertext)-'A'-double(key)+'A',26);
- deciphertext=char(deciphertext+'A');
- disp(deciphertext);
- % EXTENDED EUCLIDIAN
- num1=input('Enter a number:');
- num2=input('Enter a number:');
- s1=1;
- s2=0;
- t1=0;
- t2=1;
- while(num2~=0)
- r=mod(num1,num2);
- q=floor(num1./num2);
- num1=num2;
- num2=r;
- s=s1-q*s2;
- t=t1-q*t2;
- s1=s2;
- s2=s;
- t1=t2;
- t2=t;
- end
- disp("GCD = " + num1);
- disp("s = " + s1);
- disp("t = " + t1);
- % PLAYFAIR
- clear all;
- key='SEKIRO';
- text='SHADOWSDIETWICEE';
- text=upper(text);
- for i=1:length(text)
- if(text(i)=='J')
- text(i)='I';
- end
- end
- for i=1:length(text)-1
- if(text(i)==text(i+1))
- temp=text(i+1:length(text));
- text(i+1)='X';
- text=resize(text,i+1);
- text=string(text);
- text=text+temp;
- text=char(text);
- end
- end
- if(mod(length(text),2)~=0)
- text=text+"Z";
- text=char(text);
- end
- remainingchars=1:1:26;
- remainingchars(10)=0;
- for i=1:26
- for j=1:length(key)
- if(remainingchars(i)==(double(key(j))-64))
- remainingchars(i)=0;
- end
- end
- end
- matrix(5,5)=0;
- k=1;
- for i=1:5
- for j=1:5
- matrix(i,j)=double(key(k))-64;
- k=k+1;
- if(k>length(key))
- break;
- end
- end
- if(k>length(key))
- break;
- end
- end
- ch=1;
- for i=1:5
- for j=1:5
- if(matrix(i,j)~=0)
- continue;
- end
- while(remainingchars(ch)==0)
- ch=ch+1;
- end
- matrix(i,j)=ch;
- ch=ch+1;
- end
- end
- textnum=double(text)-64;
- ar1=0;
- ar2=0;
- ac1=0;
- ac2=0;
- ciphertext=textnum;
- for k=1:2:length(text)-1
- p1=find(matrix==textnum(k));
- r1=mod(p1,5);
- c1=ceil(p1./5);
- p2=find(matrix==textnum(k+1));
- r2=mod(p2,5);
- c2=ceil(p2./5);
- if(r1==0)
- r1=5;
- end
- if(r2==0)
- r2=5;
- end
- if(r1==r2)
- ac1=mod(c1+1,5);
- if(ac1==0)
- ac1=5;
- end
- ac2=mod(c2+1,5);
- if(ac2==0)
- ac2=5;
- end
- ar1=r1;
- ar2=r2;
- elseif(c1==c2)
- ar1=mod(r1+1,5);
- if(ar1==0)
- ar1=5;
- end
- ar2=mod(r2+1,5);
- if(ar2==0)
- ar2=5;
- end
- ac2=c2;
- ac1=c1;
- else
- ar1=r1;
- ac1=c2;
- ar2=r2;
- ac2=c1;
- end
- ciphertext(k)=matrix(ar1,ac1);
- ciphertext(k+1)=matrix(ar2,ac2);
- end
- disp(char(ciphertext+64));
- %Decryption
- br1=0;
- br2=0;
- bc1=0;
- bc2=0;
- deciphertext=ciphertext;
- for k=1:2:length(ciphertext)-1
- p1=find(matrix==ciphertext(k));
- r1=mod(p1,5);
- c1=ceil(p1./5);
- p2=find(matrix==ciphertext(k+1));
- r2=mod(p2,5);
- c2=ceil(p2./5);
- ciphertext(k)=0;
- ciphertext(k+1)=0;
- if(r1==0)
- r1=5;
- end
- if(r2==0)
- r2=5;
- end
- if(r1==r2)
- bc1=mod(c1-1,5);
- if(bc1==0)
- bc1=5;
- end
- bc2=mod(c2-1,5);
- if(bc2==0)
- bc2=5;
- end
- br1=r1;
- br2=r2;
- elseif(c1==c2)
- br1=mod(r1-1,5);
- if(br1==0)
- br1=5;
- end
- br2=mod(r2-1,5);
- if(br2==0)
- br2=5;
- end
- bc2=c2;
- bc1=c1;
- else
- br1=r1;
- bc1=c2;
- br2=r2;
- bc2=c1;
- end
- deciphertext(k)=matrix(br1,bc1);
- deciphertext(k+1)=matrix(br2,bc2);
- end
- disp(char(deciphertext+64));
- %FIESTEL
- clear all;
- text='SHADOWSDIETWICE';
- text=upper(text);
- if(mod(length(text),2)~=0)
- text=text+"Z";
- text=char(text);
- end
- lefthalf=text(1:length(text)/2);
- righthalf=text(length(text)/2+1:length(text));
- cipherrighthalf=lefthalf;
- key=randperm(26,length(text)/2);
- righthalf=double(righthalf)-64;
- cipherlefthalf=bitxor(righthalf,key);
- ciphertext=string(char(cipherlefthalf+64))+cipherrighthalf;
- disp(ciphertext);
- %Decryption
- ciphertext=char(ciphertext);
- lefthalf=ciphertext(1:length(ciphertext)/2);
- righthalf=ciphertext(length(ciphertext)/2+1:length(ciphertext));
- decipherleft=righthalf;
- lefthalf=double(lefthalf)-64;
- decipherright=bitxor(lefthalf,key);
- deciphertext=decipherleft+string(char(decipherright+64));
- disp(deciphertext);
- % FAST EXPONENTIATION
- clear all;
- x2=input('Enter the base of the number:');
- x1=input('Enter the exponent of the number');
- n=input('Enter the value for mod function:');
- bits=dec2bin(x1);
- result=x2;
- if(length(bits)==1)
- result=mod(x2^x1,n);
- end
- for i=2:length(bits)
- if(bits(i)=='1')
- result=mod(result^2,n);
- result=mod(result*x2,n);
- else
- result=mod(result^2,n);
- end
- end
- disp(result);
Advertisement
Add Comment
Please, Sign In to add comment