ash_wath

Crypto

Nov 7th, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.30 KB | None | 0 0
  1. Hill Chrome:
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <math.h>
  6. using namespace std;
  7. int main()
  8. {
  9. char plaintext[3];
  10. int key[3][3];
  11. cout<<"Enter the elements for the key(integers only):"<<endl;
  12. for(int i=0;i<3;i++)
  13. for(int j=0;j<3;j++)
  14. cin>>key[i][j];
  15. cout<<"The entered key is :"<<endl;
  16. for(int i=0;i<3;i++)
  17. {
  18. cout<<"\n";
  19. for(int j=0;j<3;j++)
  20. cout<<key[i][j]<<" ";
  21. }
  22. cout<<"\n";
  23. cout<<"Enter a three letter word:"<<endl;
  24. cin>>plaintext;
  25. int message[3][1];
  26. for(int k=0;k<3;k++)
  27. for(int j=0;j<1;j++)
  28. message[k][j]=((int)plaintext[k]-97);
  29. cout<<"The message matrix is:"<<endl;
  30. for(int i=0;i<3;i++)
  31. {
  32. cout<<"\n";
  33. for(int j=0;j<1;j++)
  34. cout<<message[i][j];
  35. }
  36. cout<<"\n";
  37. cout<<"The multiplied matrix is:"<<endl;
  38. int k,i,j;
  39. int c[3][1];
  40. for(i=0;i<3;++i)
  41. {
  42. for(j=0;j<1;++j)
  43. {
  44. c[i][j]=0;
  45. for(k=0;k<3;++k)
  46. c[i][j]=c[i][j]+(key[i][k]*message[k][j]);
  47. cout<<c[i][j]<<" ";
  48. }
  49. cout<<"\n";
  50. }
  51. cout<<"The encrypted data is:"<<endl;
  52. for(int i=0;i<1;i++)
  53. {
  54. cout<<"\n";
  55. for(int j=0;j<3;j++)
  56. cout<<(char)((fmod(c[i][j],26)+97));
  57. }
  58. cout<<"\n";
  59. float det=0;
  60. float ikey[3][3];
  61. for(i = 0; i < 3; i++)
  62. det=det + (key[0][i] * (key[1][(i+1)%3] * key[2][(i+2)%3] - key[1][(i+2)%3] * key[2][(i+1)%3]));
  63. cout<<"Inverse of matrix is:"<<endl;
  64. for(i = 0; i < 3; i++){
  65. for(j = 0; j < 3; j++)
  66. {
  67. cout<<((key[(j+1)%3][(i+1)%3] * key[(j+2)%3][(i+2)%3]) - (key[(j+1)%3][(i+2)%3] * key[(j+2)%3][(i+1)%3]))/det<<"\t";
  68. ikey[i][j]=((key[(j+1)%3][(i+1)%3] * key[(j+2)%3][(i+2)%3]) - (key[(j+1)%3][(i+2)%3] * key[(j+2)%3][(i+1)%3]))/det;
  69. }
  70. cout<<"\n";
  71. }
  72. float decrypt[3][1];
  73. for(i=0;i<3;i++)
  74. {
  75. for(j=0;j<1;j++)
  76. {
  77. decrypt[i][j]=0;
  78. for(k=0;k<3;k++)
  79. decrypt[i][j]=decrypt[i][j]+(ikey[i][k]*c[k][j]);
  80. cout<<decrypt[i][j]<<" ";
  81. }
  82. cout<<"\n";
  83. }
  84. cout<<"The decrypted data is:"<<endl;
  85. for(i=0;i<3;i++)
  86. cout<<(char)(fmod(decrypt[i][0],26)+97);
  87. }
  88. -----------------------------------------------------------------------------------
  89. Affine:
  90. #include <iostream>
  91. #include <string>
  92. #include <math.h>
  93. using namespace std;
  94. int main()
  95. {
  96. string plaintext,encrypt,decrypt;
  97. int inv;
  98. cout<<"Enter the message to be encrypted:"<<endl;
  99. getline(cin,plaintext);
  100. float a,b;
  101. cout<<"Enter the values for a and b:"<<endl;
  102. cin>>a>>b;
  103. int length=(int)plaintext.length();
  104. for(int i=0;i<length;i++)
  105. {
  106. encrypt[i]=(char)(fmod((a*(plaintext[i]-97)+b),26)+97);
  107. }
  108. for(int i=0;i<length;i++)
  109. {
  110. if(encrypt[i]=='V')
  111. {
  112. cout<<" ";
  113. }
  114. else
  115. {
  116. cout<<encrypt[i];
  117. }
  118. }
  119. cout<<"\n";
  120. cout<<"The deciphered text is:"<<endl;
  121. int f=0;
  122. inv=0;
  123. for(int i=0;i<26;i++)
  124. {
  125. f=fmod((a*i),26);
  126. if(f==1)
  127. {
  128. inv=i;
  129. }
  130. }
  131. for(int i=0;i<length;i++)
  132. {
  133. decrypt[i]=(char)(fmod(inv*((encrypt[i]-97)-b),26)+97);
  134. }
  135. for(int i=0;i<length;i++)
  136. {
  137. if(decrypt[i]=='T')
  138. {
  139. cout<<" ";
  140. }
  141. else
  142. {
  143. cout<<decrypt[i];
  144. }
  145. }
  146. }
  147. ---------------------------------------------------------------------------------------------------------
  148.  
  149. Play Fair Cipher:
  150.  
  151. #include <stdio.h>
  152. #define siz 5
  153. void encrypt(int *i, int *j)
  154. {
  155. (*i)++,(*j)++;
  156. if((*i)==siz) *i=0;
  157. else if((*j)==siz) *j=0;
  158. }
  159. void playfair(char ch1,char ch2, char mat[siz][siz])
  160. {
  161. int j,m,n,p,q,c,k;
  162. for(j=0,c=0;(c<2)||(j<siz);j++)
  163. for(k=0;k<siz;k++)
  164. if(mat[j][k] == ch1)
  165. m=j,n=k,c++;
  166. else if(mat[j][k] == ch2)
  167. p=j,q=k,c++;
  168. if(m==p)
  169. encrypt(&n,&q);
  170. else if(n==q)
  171. encrypt(&m,&p);
  172. else
  173. n+=q,q=n-q,n-=q;
  174. printf("%c%c",mat[m][n],mat[p][q]);
  175. }
  176. void main()
  177. {
  178. char mat[siz][siz],key[10],str[25]={0};
  179. int m,n,i,j;
  180. char temp;
  181.  
  182. printf("Enter Key:");
  183. gets(key);
  184. m=n=0;
  185. for(i=0;key[i]!='\0';i++)
  186. {
  187. for(j=0;j<i;j++)
  188. if(key[j] == key[i]) break;
  189. if(key[i]=='j') key[i]='i';
  190. if(j>=i)
  191. {
  192. mat[m][n++] = key[i];
  193. if(n==siz)
  194. n=0,m++;
  195. }
  196. }
  197. for(i=97;i<=122;i++)
  198. {
  199. for(j=0;key[j]!='\0';j++)
  200. if(key[j] == i)
  201. break;
  202. else if(i=='j')
  203. break;
  204.  
  205. if(key[j]=='\0')
  206. {
  207. mat[m][n++] = i;
  208. if(n==siz) n=0,m++;
  209. }
  210. }
  211. printf("Enter input String:");
  212. gets(str);
  213. printf("\n\nMatrix :\n");
  214. for(i=0;i<siz;i++)
  215. {
  216. for(j=0;j<siz;j++)
  217. printf("%c\t",mat[i][j]);
  218. printf("\n");
  219. }
  220. printf("\n\nEntered text :%s\nCipher Text :",str);
  221.  
  222. for(i=0;str[i]!='\0';i++)
  223. {
  224. temp = str[i++];
  225. if(temp == 'j') temp='i';
  226. if(str[i]=='\0')
  227. playfair(temp,'x',mat);
  228. else
  229. {
  230. if(str[i]=='j') str[i]='i';
  231. if(temp == str[i])
  232. {
  233. playfair(temp,'x',mat);
  234. playfair('x',str[i],mat);
  235. }
  236. else
  237. playfair(temp,str[i],mat);
  238. }
  239. }
  240. }
  241.  
  242. ---------------------------------------------------------------------------
  243. Ceaser Cipher:
  244.  
  245. #include <iostream>
  246. #include <string>
  247. using namespace std;
  248. int key;
  249. char caesar(char);
  250. int main()
  251. {
  252. string input;
  253. cout<<"Enter the key:";
  254. cin>>key;
  255. cout << "Enter the text: " << endl;
  256. cin>>input;
  257. string output = "";
  258. for (int x = 0; x < input.length(); x++)
  259. {
  260. output += caesar(input[x]);
  261. }
  262. cout << output << endl;
  263. }
  264.  
  265. char caesar(char c)
  266. {
  267. if (isalpha(c))
  268. {
  269. c = toupper(c);
  270. c = (((c - 65) + key) % 26) + 65;
  271. }
  272. return c;
  273. }
  274. ------------------------------------------------------------------------------
  275. RSA:
  276.  
  277. #include<stdio.h>
  278. #include<conio.h>
  279. #include<stdlib.h>
  280. #include<math.h>
  281. #include<string.h>
  282.  
  283. long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;
  284. char msg[100];
  285. int prime(long int);
  286. void ce();
  287. long int cd(long int);
  288. void encrypt();
  289. void decrypt();
  290. void main()
  291. {
  292. clrscr();
  293. printf("\nENTER FIRST PRIME NUMBER\n");
  294. scanf("%d",&p);
  295. flag=prime(p);
  296. if(flag==0)
  297. {
  298. printf("\nWRONG INPUT\n");
  299. getch();
  300. exit(1);
  301. }
  302. printf("\nENTER ANOTHER PRIME NUMBER\n");
  303. scanf("%d",&q);
  304. flag=prime(q);
  305. if(flag==0||p==q)
  306. {
  307. printf("\nWRONG INPUT\n");
  308. getch();
  309. exit(1);
  310. }
  311. printf("\nENTER MESSAGE\n");
  312. fflush(stdin);
  313. scanf("%s",msg);
  314. for(i=0;msg[i]!=NULL;i++)
  315. m[i]=msg[i];
  316. n=p*q;
  317. t=(p-1)*(q-1);
  318. ce();
  319. printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
  320. for(i=0;i<j-1;i++)
  321. printf("\n%ld\t%ld",e[i],d[i]);
  322. encrypt();
  323. decrypt();
  324. getch();
  325. }
  326. int prime(long int pr)
  327. {
  328. int i;
  329. j=sqrt(pr);
  330. for(i=2;i<=j;i++)
  331. {
  332. if(pr%i==0)
  333. return 0;
  334. }
  335. return 1;
  336. }
  337. void ce()
  338. {
  339. int k;
  340. k=0;
  341. for(i=2;i<t;i++)
  342. {
  343. if(t%i==0)
  344. continue;
  345. flag=prime(i);
  346. if(flag==1&&i!=p&&i!=q)
  347. {
  348. e[k]=i;
  349. flag=cd(e[k]);
  350. if(flag>0)
  351. {
  352. d[k]=flag;
  353. k++;
  354. }
  355. if(k==99)
  356. break;
  357. }
  358. }
  359. }
  360. long int cd(long int x)
  361. {
  362. long int k=1;
  363. while(1)
  364. {
  365. k=k+t;
  366. if(k%x==0)
  367. return(k/x);
  368. }
  369. }
  370. void encrypt()
  371. {
  372. long int pt,ct,key=e[0],k,len;
  373. i=0;
  374. len=strlen(msg);
  375. while(i!=len)
  376. {
  377. pt=m[i];
  378. pt=pt-96;
  379. k=1;
  380. for(j=0;j<key;j++)
  381. {
  382. k=k*pt;
  383. k=k%n;
  384. }
  385. temp[i]=k;
  386. ct=k+96;
  387. en[i]=ct;
  388. i++;
  389. }
  390. en[i]=-1;
  391. printf("\nTHE ENCRYPTED MESSAGE IS\n");
  392. for(i=0;en[i]!=-1;i++)
  393. printf("%c",en[i]);
  394. }
  395. void decrypt()
  396. {
  397. long int pt,ct,key=d[0],k;
  398. i=0;
  399. while(en[i]!=-1)
  400. {
  401. ct=temp[i];
  402. k=1;
  403. for(j=0;j<key;j++)
  404. {
  405. k=k*ct;
  406. k=k%n;
  407. }
  408. pt=k+96;
  409. m[i]=pt;
  410. i++;
  411. }
  412. m[i]=-1;
  413. printf("\nTHE DECRYPTED MESSAGE IS\n");
  414. for(i=0;m[i]!=-1;i++)
  415. printf("%c",m[i]);
  416. }
  417. ----------------------------------------------------------------------
  418. Vigenere
  419.  
  420. #include <iostream>
  421. #include <string>
  422. #include <math.h>
  423. using namespace std;
  424. int main()
  425. {
  426. char plaintext[3];
  427. int key[3][3];
  428. cout<<"Enter the elements for the key(integers only):"<<endl;
  429. for(int i=0;i<3;i++)
  430. for(int j=0;j<3;j++)
  431. cin>>key[i][j];
  432. cout<<"The entered key is :"<<endl;
  433. for(int i=0;i<3;i++)
  434. {
  435. cout<<"\n";
  436. for(int j=0;j<3;j++)
  437. cout<<key[i][j]<<" ";
  438. }
  439. cout<<"\n";
  440. cout<<"Enter a three letter word:"<<endl;
  441. cin>>plaintext;
  442. int message[3][1];
  443. for(int k=0;k<3;k++)
  444. for(int j=0;j<1;j++)
  445. message[k][j]=((int)plaintext[k]-97);
  446. cout<<"The message matrix is:"<<endl;
  447. for(int i=0;i<3;i++)
  448. {
  449. cout<<"\n";
  450. for(int j=0;j<1;j++)
  451. cout<<message[i][j];
  452. }
  453. cout<<"\n";
  454. cout<<"The multiplied matrix is:"<<endl;
  455. int k,i,j;
  456. int c[3][1];
  457. for(i=0;i<3;++i)
  458. {
  459. for(j=0;j<1;++j)
  460. {
  461. c[i][j]=0;
  462. for(k=0;k<3;++k)
  463. c[i][j]=c[i][j]+(key[i][k]*message[k][j]);
  464. cout<<c[i][j]<<" ";
  465. }
  466. cout<<"\n";
  467. }
  468. cout<<"The encrypted data is:"<<endl;
  469. for(int i=0;i<1;i++)
  470. {
  471. cout<<"\n";
  472. for(int j=0;j<3;j++)
  473. cout<<(char)((fmod(c[i][j],26)+97));
  474. }
  475. cout<<"\n";
  476. float det=0;
  477. float ikey[3][3];
  478. for(i = 0; i < 3; i++)
  479. det=det + (key[0][i] * (key[1][(i+1)%3] * key[2][(i+2)%3] - key[1][(i+2)%3] * key[2][(i+1)%3]));
  480. cout<<"Inverse of matrix is:"<<endl;
  481. for(i = 0; i < 3; i++){
  482. for(j = 0; j < 3; j++)
  483. {
  484. cout<<((key[(j+1)%3][(i+1)%3] * key[(j+2)%3][(i+2)%3]) - (key[(j+1)%3][(i+2)%3] * key[(j+2)%3][(i+1)%3]))/det<<"\t";
  485. ikey[i][j]=((key[(j+1)%3][(i+1)%3] * key[(j+2)%3][(i+2)%3]) - (key[(j+1)%3][(i+2)%3] * key[(j+2)%3][(i+1)%3]))/det;
  486. }
  487. cout<<"\n";
  488. }
  489. float decrypt[3][1];
  490. for(i=0;i<3;i++)
  491. {
  492. for(j=0;j<1;j++)
  493. {
  494. decrypt[i][j]=0;
  495. for(k=0;k<3;k++)
  496. decrypt[i][j]=decrypt[i][j]+(ikey[i][k]*c[k][j]);
  497. cout<<decrypt[i][j]<<" ";
  498. }
  499. cout<<"\n";
  500. }
  501. cout<<"The decrypted data is:"<<endl;
  502. for(i=0;i<3;i++)
  503. cout<<(char)(fmod(decrypt[i][0],26)+97);
  504. }
  505. ---------------------------------------------------------------------------------
  506.  
  507. Vignere Cipher:
  508.  
  509. #include <stdio.h>
  510. #include <ctype.h>
  511. #include <string.h>
  512. #include <process.h>
  513. void vigenereCipher(char *,char *);
  514. void encipher();
  515. void decipher();
  516. int main()
  517. {
  518. int choice;
  519. while(1)
  520. {
  521. printf("1. Encrypt Text \n");
  522. printf("2. Decrypt Text \n");
  523. printf("3. Exit \n");
  524. printf("Enter Your Choice : ");
  525. scanf("%d",&choice);
  526. fflush(stdin);
  527. if(choice == 3)
  528. exit(0);
  529. else if(choice == 1)
  530. encipher();
  531. else if(choice == 2)
  532. decipher();
  533. else
  534. printf("Please Enter Valid Option.");
  535. }
  536. return 0;
  537. }
  538. void encipher()
  539. {
  540. unsigned int i,j;
  541. char input[257],key[33];
  542. printf("Enter Text to be Encrypted [Max. 256 characters/ only alphabets]:n ");
  543. gets(input);
  544. printf("Enter Encryption Key [Max. 32 Characters/ only alphabets]: ");
  545. gets(key);
  546. for(i=0,j=0;i<strlen(input);i++,j++)
  547. {
  548. if(j>=strlen(key))
  549. {
  550. j=0;
  551. }
  552. printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65))%26));
  553. }
  554. }
  555. void decipher()
  556. {
  557. unsigned int i,j;
  558. char input[257],key[33];
  559. int value;
  560. printf("Enter Text to be Decrypted [Max. 256 characters/ only alphabets]:n ");
  561. gets(input);
  562. printf("Enter Decryption Key [Max. 32 Characters/ only aphabets]: ");
  563. gets(key);
  564. for(i=0,j=0;i<strlen(input);i++,j++)
  565. {
  566. if(j>=strlen(key))
  567. {
  568. j=0;
  569. }
  570. value = (toupper(input[i])-64)-(toupper(key[j])-64);
  571. if( value < 0)
  572. {
  573. value = value * -1;
  574. }
  575. printf("%c",65 + (value % 26));
  576. }
  577. }
Add Comment
Please, Sign In to add comment