Jay_25112

Untitled

Nov 13th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 10.20 KB | None | 0 0
  1. % CEASAR
  2. %Taking the input from the users
  3. text=input('Enter the plain text.','s');
  4. offset=input('Enter the offset value.');
  5. ciphertext=text;
  6.  
  7. for i=1:length(text)
  8.     temp = double(text(i));
  9.     if(temp>=65 && temp<=90)
  10.         %Converting character into ASCII value
  11.         currascii=temp-65;
  12.         %Shifting values by offset
  13.         encryascii=mod(currascii+offset,26);
  14.         %Converting ASCII value back into character
  15.         ciphertext(i)=char(encryascii+65);
  16.     else
  17.         currascii=double(text(i))-97;
  18.         encryascii=mod(currascii+offset,26);
  19.         ciphertext(i)=char(encryascii+97);
  20.     end
  21. end
  22. disp("After encryption:");
  23. disp(ciphertext);
  24.  
  25. %Decryption: Subtracting offset from ciphertext to decipher it.
  26. deciphertext=ciphertext;
  27. for i=1:length(ciphertext)
  28.     temp = double(ciphertext(i));
  29.     if(temp>=65 && temp<=90)
  30.         currascii=temp-65;
  31.         decryascii=mod(currascii-offset,26);
  32.         deciphertext(i)=char(decryascii+65);
  33.     else
  34.         currascii=double(ciphertext(i))-97;
  35.         decryascii=mod(currascii-offset,26);
  36.         deciphertext(i)=char(decryascii+97);
  37.     end
  38. end
  39. disp(deciphertext);
  40.  
  41.  
  42. % MONOALPHABETIC
  43. clc;
  44. clear all;
  45. close all;
  46. key=randperm(26);
  47. text='ATTACK';
  48. ciphertext=text;
  49. %Encryption
  50. for i=1:length(text)
  51.     %Subtracting 64 to get index in range [1,26]
  52.     charIndex=double(text(i))-64;
  53.     %mapping characters from text to key
  54.     ciphertext(i)=char(key(charIndex)+64);
  55. end
  56. disp(ciphertext);
  57.  
  58. %Decryption
  59. deciphertext=ciphertext;
  60. for i=1:length(ciphertext)
  61.     charIndex=double(ciphertext(i))-64;
  62.     %finding the index for ciphered character in the array key
  63.     textIndex=find(key==charIndex);
  64.     %adding 64 to map ciphered character back into original character
  65.     deciphertext(i)=char(textIndex+64);
  66. end
  67. disp(deciphertext);
  68.  
  69.  
  70.  
  71. % HILL
  72. clc;
  73. clear all;
  74. close all;
  75. text='ATTACKNOW';
  76. n=2;
  77. while(mod(length(text),n)~=0)
  78.     text=text+"Z";
  79.     text=char(text);
  80. end
  81. %Encryption
  82. key=[2 1; 3 4];
  83.  
  84. text=reshape(text,[length(text)/n,n]);
  85.  
  86. text1=transpose(text);
  87. text1=double(text1)-65;
  88.  
  89. ciphertext=mod(key*text1,26);
  90. ciphertext=char(ciphertext+65);
  91. ciphertext=transpose(ciphertext);
  92. ciphertext=reshape(ciphertext,1,[]);
  93.  
  94. disp(ciphertext);
  95.  
  96. %Decryption
  97.  
  98. key1=det(key);
  99. for i=1:1:25
  100.     a = mod(key1*i,26);
  101.     if a==1
  102.             k2=i;
  103.     end
  104. end
  105.  
  106. key_inverse=round(mod(k2*adjoint(key),26));
  107.  
  108. ciphertext=double(ciphertext)-65;
  109.  
  110. ciphertext=reshape(ciphertext,length(ciphertext)/n,[]);
  111. ciphertext=transpose(ciphertext);
  112.  
  113. deciphertext=mod(key_inverse*ciphertext,26);
  114. deciphertext=transpose(deciphertext);
  115. deciphertext=reshape(deciphertext,1,[]);
  116. deciphertext=char(deciphertext + 65);
  117. disp(deciphertext);
  118.  
  119.  
  120.  
  121.  
  122. %COLUMNAR
  123. text=input('Enter the message:','s');
  124. n=input('Enter a number:');
  125.  
  126. while(mod(length(text),n)~=0)
  127.     text=text+"Z";
  128.     text=char(text);
  129. end
  130.  
  131. text=upper(text);
  132. key=randperm(n);
  133. text=reshape(text,[length(text)/n,n]);
  134.  
  135. ciphertext=text;
  136.  
  137. [rows,cols]=size(text);
  138. for i=1:rows
  139.     for j=1:cols
  140.         ciphertext(i,j)=text(i,key(j));
  141.     end
  142. end
  143. ciphertext=transpose(ciphertext);
  144. ciphertext=reshape(ciphertext,1,[]);
  145. disp(ciphertext);
  146. %Decryption
  147. ciphertext=reshape(ciphertext,cols,[]);
  148. ciphertext=transpose(ciphertext);
  149. deciphertext=ciphertext;
  150. for i=1:rows
  151.     for j=1:cols
  152.         deciphertext(i,j)=ciphertext(i,find(key==j));
  153.     end
  154. end
  155. deciphertext=reshape(deciphertext,1,[]);
  156. disp(deciphertext);
  157.  
  158.  
  159.  
  160. %DOUBLE COLUMNAR
  161. text=input('Enter the message:','s');
  162. n1=input('Enter a number:');
  163.  
  164. while(mod(length(text),n1)~=0)
  165.     text=text+"Z";
  166.     text=char(text);
  167. end
  168.  
  169. text=upper(text);
  170. n2=length(text)/n1;
  171. key1=randperm(n1);
  172. key2=randperm(n2);
  173. text=reshape(text,n1,[]);
  174.  
  175. ciphertext=text;
  176.  
  177. [rows,cols]=size(text);
  178. for i=1:rows
  179.     for j=1:cols
  180.         ciphertext(i,j)=text(key1(i),key2(j));
  181.     end
  182. end
  183. ciphertext=transpose(ciphertext);
  184. ciphertext=reshape(ciphertext,1,[]);
  185. disp(ciphertext);
  186. %Decryption
  187. ciphertext=reshape(ciphertext,cols,[]);
  188. ciphertext=transpose(ciphertext);
  189. deciphertext=ciphertext;
  190. for i=1:rows
  191.     for j=1:cols
  192.         deciphertext(i,j)=ciphertext(find(key1==i),find(key2==j));
  193.     end
  194. end
  195. deciphertext=reshape(deciphertext,1,[]);
  196. disp(deciphertext);
  197.  
  198.  
  199.  
  200.  
  201. % AUTOKEY
  202. text="ATTACKNOW";
  203. key=round(rand(1)*26);
  204.  
  205. key=char(key+65)+text;
  206. key=char(key);
  207. text=char(text);
  208. ciphertext=text;
  209. for i=1:length(text)
  210.     a=double(text(i))-65;
  211.     b=double(key(i))-65;
  212.     ciphertext(i)=char(mod(a+b,26)+65);
  213. end
  214. disp(ciphertext);
  215. %Decryption
  216.  
  217. deciphertext=ciphertext;
  218. for i=1:length(ciphertext)
  219.     a=double(ciphertext(i))-65;
  220.     b=double(key(i))-65;
  221.     deciphertext(i)=char(mod(a-b,26)+65);
  222. end
  223. disp(deciphertext);
  224.  
  225.  
  226. % VERNAM
  227. text='ATTACKNOW';
  228. key=randperm(26,length(text));
  229.  
  230. text=double(text)-65;
  231. ciphertext=bitxor(text,key);
  232. %ciphertext=mod(ciphertext,26);
  233. ciphertext=char(ciphertext+65);
  234. disp(ciphertext);
  235. %Decryption
  236.  
  237. ciphertext=double(ciphertext)-65;
  238. deciphertext=ciphertext;
  239. for i=1:length(ciphertext)
  240.     deciphertext(i)=bitxor(key(i),ciphertext(i));
  241. end
  242. %deciphertext=mod(deciphertext,26);
  243. deciphertext=char(deciphertext+65);
  244. disp(deciphertext);
  245.  
  246.  
  247.  
  248.  
  249. % VIGNERE
  250. text='ATTACKNOW';
  251. n=3;
  252. key=randperm(26,3);
  253. key=char(key+65);
  254. while (length(key)<length(text))
  255.     key=key+string(key);
  256.     key=char(key);
  257. end
  258. key=resize(key,length(text));
  259. ciphertext=mod(double(text)-'A'+double(key)-'A',26);
  260. ciphertext=char(ciphertext+'A');
  261. disp(ciphertext);
  262. %Decryption
  263. deciphertext=ciphertext;
  264. deciphertext=mod(double(ciphertext)-'A'-double(key)+'A',26);
  265. deciphertext=char(deciphertext+'A');
  266. disp(deciphertext);
  267.  
  268.  
  269.  
  270. % EXTENDED EUCLIDIAN
  271. num1=input('Enter a number:');
  272. num2=input('Enter a number:');
  273. s1=1;
  274. s2=0;
  275. t1=0;
  276. t2=1;
  277. while(num2~=0)
  278.     r=mod(num1,num2);
  279.     q=floor(num1./num2);
  280.  
  281.     num1=num2;
  282.     num2=r;
  283.  
  284.     s=s1-q*s2;
  285.     t=t1-q*t2;
  286.     s1=s2;
  287.     s2=s;
  288.     t1=t2;
  289.     t2=t;
  290. end
  291. disp("GCD = " + num1);
  292. disp("s = " + s1);
  293. disp("t = " + t1);
  294.  
  295.  
  296.  
  297.  
  298. % PLAYFAIR
  299. clear all;
  300. key='SEKIRO';
  301. text='SHADOWSDIETWICEE';
  302. text=upper(text);
  303.  
  304. for i=1:length(text)
  305.     if(text(i)=='J')
  306.         text(i)='I';
  307.     end
  308. end
  309. for i=1:length(text)-1
  310.     if(text(i)==text(i+1))
  311.         temp=text(i+1:length(text));
  312.         text(i+1)='X';
  313.         text=resize(text,i+1);
  314.         text=string(text);
  315.         text=text+temp;
  316.         text=char(text);
  317.     end
  318. end
  319.  
  320. if(mod(length(text),2)~=0)
  321.     text=text+"Z";
  322.     text=char(text);
  323. end
  324.  
  325. remainingchars=1:1:26;
  326. remainingchars(10)=0;
  327.  
  328. for i=1:26
  329.     for j=1:length(key)
  330.         if(remainingchars(i)==(double(key(j))-64))
  331.             remainingchars(i)=0;
  332.         end
  333.     end
  334. end
  335.  
  336. matrix(5,5)=0;
  337.  
  338. k=1;
  339. for i=1:5
  340.     for j=1:5
  341.         matrix(i,j)=double(key(k))-64;
  342.         k=k+1;
  343.         if(k>length(key))
  344.             break;
  345.         end
  346.     end
  347.     if(k>length(key))
  348.         break;
  349.     end
  350. end
  351.  
  352. ch=1;
  353. for i=1:5
  354.     for j=1:5
  355.         if(matrix(i,j)~=0)
  356.             continue;
  357.         end
  358.         while(remainingchars(ch)==0)
  359.             ch=ch+1;
  360.         end
  361.         matrix(i,j)=ch;
  362.         ch=ch+1;
  363.     end
  364. end
  365.  
  366. textnum=double(text)-64;
  367.  
  368. ar1=0;
  369. ar2=0;
  370. ac1=0;
  371. ac2=0;
  372. ciphertext=textnum;
  373. for k=1:2:length(text)-1
  374.     p1=find(matrix==textnum(k));
  375.     r1=mod(p1,5);
  376.     c1=ceil(p1./5);
  377.     p2=find(matrix==textnum(k+1));
  378.     r2=mod(p2,5);
  379.     c2=ceil(p2./5);
  380.  
  381.     if(r1==0)
  382.         r1=5;
  383.     end
  384.  
  385.     if(r2==0)
  386.         r2=5;
  387.     end
  388.  
  389.     if(r1==r2)
  390.         ac1=mod(c1+1,5);
  391.         if(ac1==0)
  392.             ac1=5;
  393.         end
  394.         ac2=mod(c2+1,5);
  395.         if(ac2==0)
  396.             ac2=5;
  397.         end
  398.         ar1=r1;
  399.         ar2=r2;
  400.     elseif(c1==c2)
  401.         ar1=mod(r1+1,5);
  402.         if(ar1==0)
  403.             ar1=5;
  404.         end
  405.         ar2=mod(r2+1,5);
  406.         if(ar2==0)
  407.             ar2=5;
  408.         end
  409.         ac2=c2;
  410.         ac1=c1;
  411.     else
  412.         ar1=r1;
  413.         ac1=c2;
  414.         ar2=r2;
  415.         ac2=c1;
  416.     end
  417.     ciphertext(k)=matrix(ar1,ac1);
  418.     ciphertext(k+1)=matrix(ar2,ac2);
  419. end
  420. disp(char(ciphertext+64));
  421. %Decryption
  422.  
  423. br1=0;
  424. br2=0;
  425. bc1=0;
  426. bc2=0;
  427. deciphertext=ciphertext;
  428. for k=1:2:length(ciphertext)-1
  429.     p1=find(matrix==ciphertext(k));
  430.     r1=mod(p1,5);
  431.     c1=ceil(p1./5);
  432.     p2=find(matrix==ciphertext(k+1));
  433.     r2=mod(p2,5);
  434.     c2=ceil(p2./5);
  435.     ciphertext(k)=0;
  436.     ciphertext(k+1)=0;
  437.  
  438.     if(r1==0)
  439.         r1=5;
  440.     end
  441.  
  442.     if(r2==0)
  443.         r2=5;
  444.     end
  445.  
  446.     if(r1==r2)
  447.         bc1=mod(c1-1,5);
  448.         if(bc1==0)
  449.             bc1=5;
  450.         end
  451.         bc2=mod(c2-1,5);
  452.         if(bc2==0)
  453.             bc2=5;
  454.         end
  455.         br1=r1;
  456.         br2=r2;
  457.     elseif(c1==c2)
  458.         br1=mod(r1-1,5);
  459.         if(br1==0)
  460.             br1=5;
  461.         end
  462.         br2=mod(r2-1,5);
  463.         if(br2==0)
  464.             br2=5;
  465.         end
  466.         bc2=c2;
  467.         bc1=c1;
  468.     else
  469.         br1=r1;
  470.         bc1=c2;
  471.         br2=r2;
  472.         bc2=c1;
  473.     end
  474.     deciphertext(k)=matrix(br1,bc1);
  475.     deciphertext(k+1)=matrix(br2,bc2);
  476. end
  477. disp(char(deciphertext+64));
  478.  
  479.  
  480.  
  481. %FIESTEL
  482. clear all;
  483. text='SHADOWSDIETWICE';
  484. text=upper(text);
  485.  
  486. if(mod(length(text),2)~=0)
  487.     text=text+"Z";
  488.     text=char(text);
  489. end
  490.  
  491. lefthalf=text(1:length(text)/2);
  492. righthalf=text(length(text)/2+1:length(text));
  493.  
  494. cipherrighthalf=lefthalf;
  495. key=randperm(26,length(text)/2);
  496.  
  497. righthalf=double(righthalf)-64;
  498. cipherlefthalf=bitxor(righthalf,key);
  499. ciphertext=string(char(cipherlefthalf+64))+cipherrighthalf;
  500. disp(ciphertext);
  501.  
  502. %Decryption
  503. ciphertext=char(ciphertext);
  504. lefthalf=ciphertext(1:length(ciphertext)/2);
  505. righthalf=ciphertext(length(ciphertext)/2+1:length(ciphertext));
  506.  
  507. decipherleft=righthalf;
  508. lefthalf=double(lefthalf)-64;
  509. decipherright=bitxor(lefthalf,key);
  510. deciphertext=decipherleft+string(char(decipherright+64));
  511. disp(deciphertext);
  512.  
  513.  
  514.  
  515.  
  516. % FAST EXPONENTIATION
  517. clear all;
  518. x2=input('Enter the base of the number:');
  519. x1=input('Enter the exponent of the number');
  520. n=input('Enter the value for mod function:');
  521. bits=dec2bin(x1);
  522. result=x2;
  523. if(length(bits)==1)
  524.     result=mod(x2^x1,n);
  525. end
  526.  
  527. for i=2:length(bits)
  528.     if(bits(i)=='1')
  529.         result=mod(result^2,n);
  530.         result=mod(result*x2,n);
  531.     else
  532.         result=mod(result^2,n);
  533.     end
  534. end
  535. disp(result);
Advertisement
Add Comment
Please, Sign In to add comment