sheyshya1

play fair cipher

Mar 28th, 2022 (edited)
285
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 1
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #define MX 5
  6. int choice;
  7. void playfair(char ch1, char ch2, char key[MX][MX]) {
  8.     int i, j, w, x, y, z;
  9.     //find row and col position of pair text
  10.     for (i = 0; i < MX; i++) {
  11.         for (j = 0; j < MX; j++) {
  12.             if (ch1 == key[i][j]) {
  13.                 w = i;
  14.                 x = j;
  15.             } else if (ch2 == key[i][j]) {
  16.                 y = i;
  17.                 z = j;
  18.             }
  19.         }
  20.     }
  21.     //wand y row
  22.     //x and z col
  23.  
  24.     if (w == y) {
  25.             //encryption
  26.         if(choice==1){
  27.             x = (x + 1) % 5;
  28.             z = (z + 1) % 5;
  29.         }
  30.         //decryptionm
  31.         else{
  32.             x = ((x - 1) % 5+5)%5;
  33.             z = ((z - 1) % 5+5)%5;
  34.         }
  35.         printf("%c%c", key[w][x], key[y][z]);
  36.     } else if (x == z) {
  37.         if(choice==1){
  38.             w = (w + 1) % 5;
  39.             y = (y + 1) % 5;
  40.         }
  41.         else{
  42.             w = ((w - 1) % 5+5)%5;
  43.             y = ((y - 1) % 5+5)%5;
  44.         }
  45.         printf("%c%c", key[w][x], key[y][z]);
  46.     }
  47.     else {
  48.         printf("%c%c", key[w][z], key[y][x]);
  49.     }
  50. }
  51. void removeDuplicates(char str[]){
  52.     int hash[256]        =  {0};
  53.     int currentIndex     = 0;
  54.     int lastUniqueIndex  = 0;
  55.     while(*(str+currentIndex)){
  56.         char temp = *(str+currentIndex);
  57.         if(0 == hash[temp]){
  58.             hash[temp] = 1;
  59.             *(str+lastUniqueIndex) = temp;
  60.             lastUniqueIndex++;
  61.         }
  62.         currentIndex++;
  63.     }
  64.     *(str+lastUniqueIndex) = '\0';
  65.  
  66. }
  67. void main() {
  68.     int i, j, k = 0, l, m = 0, n;
  69.     char key[MX][MX], keyminus[25], keystr[10], str[25] = {
  70.         0
  71.     };
  72.  
  73.     char alpa[26] = {
  74.         'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
  75.     };
  76.     printf("\n1.Encryption\n2.Decryption\n\nChoice(1 or 2):");
  77.     scanf("%d",&choice);
  78.     if(choice!=1 && choice!=2){ printf("Invalid Choice"); return;}
  79.     fflush(stdin);
  80.     printf("\nEnter key:");
  81.     gets(keystr);
  82.     printf("Enter the text:");
  83.     gets(str);
  84.     removeDuplicates(keystr);
  85.     n = strlen(keystr);
  86.     //convert the key character to uppertext
  87.     for (i = 0; i < n; i++) {
  88.         if (keystr[i] == 'j') keystr[i] = 'i';
  89.         else if (keystr[i] == 'J') keystr[i] = 'I';
  90.         keystr[i] = toupper(keystr[i]);
  91.     }
  92.     //convert all the characters of plaintext to uppertext
  93.     for (i = 0; i < strlen(str); i++) {
  94.         if (str[i] == 'j') str[i] = 'i';
  95.         else if (str[i] == 'J') str[i] = 'I';
  96.         str[i] = toupper(str[i]);
  97.     }
  98.     // store all characters except key
  99.     j = 0;
  100.     for (i = 0; i < 26; i++) {
  101.         for (k = 0; k < n; k++) {
  102.             if (keystr[k] == alpa[i]) break;
  103.             else if (alpa[i] == 'J') break;
  104.         }
  105.         if (k == n) {
  106.             keyminus[j] = alpa[i];
  107.             j++;
  108.         }
  109.     }
  110.     //construct key keymatrix
  111.     k = 0;
  112.     for (i = 0; i < MX; i++) {
  113.         for (j = 0; j < MX; j++) {
  114.             if (k < n) {
  115.                 key[i][j] = keystr[k];
  116.                 k++;
  117.             } else {
  118.                 key[i][j] = keyminus[m];
  119.                 m++;
  120.             }
  121.             printf("%c ", key[i][j]);
  122.         }
  123.         printf("\n");
  124.     }
  125.     // construct diagram and convert to cipher text
  126.     printf("\nEntered text :%s\nOutput Text :", str);
  127.     for (i = 0; i < strlen(str); i++) {
  128.         if (str[i] == 'J') str[i] = 'I';
  129.         if (str[i + 1] == '\0') playfair(str[i], 'X', key);
  130.         else {
  131.             if (str[i + 1] == 'J') str[i + 1] = 'I';
  132.             if (str[i] == str[i + 1]) playfair(str[i], 'X', key);
  133.             else {
  134.                 playfair(str[i], str[i + 1], key);
  135.                 i++;
  136.             }
  137.         }
  138.     }
  139.     if(choice==2) printf(" (Remove unnecessary X)");
  140. }
  141.  
Add Comment
Please, Sign In to add comment