C Program to Perform Cryptography using Transposition Technique

This is a C Program to implement transposition technique. A transposition cipher is a method of encryption, where the positions held by plaintext characters or group of characters are shifted as per a system, such that ciphertext is a permutation of the plaintext. That means only the order of the character or group of characters is changed.

Here is source code of the C Program to Perform Cryptography Using Transposition Technique. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. void cipher(int i, int c);
  5. int findMin();
  6. void makeArray(int, int);
  7.  
  8. char arr[22][22], darr[22][22], emessage[111], retmessage[111], key[55];
  9. char temp[55], temp2[55];
  10. int k = 0;
  11.  
  12. int main() {
  13.     char *message, *dmessage;
  14.  
  15.     int i, j, klen, emlen, flag = 0;
  16.     int r, c, index, min, rows;
  17.  
  18.     printf("Enetr the key\n");
  19.     fflush(stdin);
  20.     gets(key);
  21.  
  22.     printf("\nEnter message to be ciphered\n");
  23.     fflush(stdin);
  24.     gets(message);
  25.  
  26.     strcpy(temp, key);
  27.     klen = strlen(key);
  28.  
  29.     k = 0;
  30.     for (i = 0;; i++) {
  31.         if (flag == 1)
  32.             break;
  33.  
  34.         for (j = 0; key[j] != NULL; j++) {
  35.             if (message[k] == NULL) {
  36.                 flag = 1;
  37.                 arr[i][j] = '-';
  38.             } else {
  39.                 arr[i][j] = message[k++];
  40.             }
  41.         }
  42.     }
  43.     r = i;
  44.     c = j;
  45.  
  46.     for (i = 0; i < r; i++) {
  47.         for (j = 0; j < c; j++) {
  48.             printf("%c ", arr[i][j]);
  49.         }
  50.         printf("\n");
  51.     }
  52.  
  53.     k = 0;
  54.  
  55.     for (i = 0; i < klen; i++) {
  56.         index = findMin();
  57.         cipher(index, r);
  58.     }
  59.  
  60.     emessage[k] = '\0';
  61.     printf("\nEncrypted message is\n");
  62.     for (i = 0; emessage[i] != NULL; i++)
  63.         printf("%c", emessage[i]);
  64.  
  65.     printf("\n\n");
  66.     //deciphering
  67.  
  68.     emlen = strlen(emessage);
  69.     //emlen is length of encrypted message
  70.  
  71.     strcpy(temp, key);
  72.  
  73.     rows = emlen / klen;
  74.     //rows is no of row of the array to made from ciphered message
  75.     rows;
  76.     j = 0;
  77.  
  78.     for (i = 0, k = 1; emessage[i] != NULL; i++, k++) {
  79.         //printf("\nEmlen=%d",emlen);
  80.         temp2[j++] = emessage[i];
  81.         if ((k % rows) == 0) {
  82.             temp2[j] = '\0';
  83.             index = findMin();
  84.             makeArray(index, rows);
  85.             j = 0;
  86.         }
  87.     }
  88.  
  89.     printf("\nArray Retrieved is\n");
  90.  
  91.     k = 0;
  92.     for (i = 0; i < r; i++) {
  93.         for (j = 0; j < c; j++) {
  94.             printf("%c ", darr[i][j]);
  95.             //retrieving message
  96.             retmessage[k++] = darr[i][j];
  97.  
  98.         }
  99.         printf("\n");
  100.     }
  101.     retmessage[k] = '\0';
  102.  
  103.     printf("\nMessage retrieved is\n");
  104.  
  105.     for (i = 0; retmessage[i] != NULL; i++)
  106.         printf("%c", retmessage[i]);
  107.  
  108.     getch();
  109.     return (0);
  110. }
  111.  
  112. void cipher(int i, int r) {
  113.     int j;
  114.     for (j = 0; j < r; j++) {
  115.         {
  116.             emessage[k++] = arr[j][i];
  117.         }
  118.     }
  119.     // emessage[k]='\0';
  120. }
  121.  
  122. void makeArray(int col, int row) {
  123.     int i, j;
  124.  
  125.     for (i = 0; i < row; i++) {
  126.         darr[i][col] = temp2[i];
  127.     }
  128. }
  129.  
  130. int findMin() {
  131.     int i, j, min, index;
  132.  
  133.     min = temp[0];
  134.     index = 0;
  135.     for (j = 0; temp[j] != NULL; j++) {
  136.         if (temp[j] < min) {
  137.             min = temp[j];
  138.             index = j;
  139.         }
  140.     }
  141.  
  142.     temp[index] = 123;
  143.     return (index);
  144. }

Output:

$ gcc TranspositionTechnique.c
$ ./a.out
 
Enter the key
hello
 
Enter the message to be ciphered
how are you
 
h o w   a
r e   y o
u - - - -
 
Encrypted message is
oe-hruw - y-ao-
 
Array Retrieved is
h o w   a
r e   y o
u - - - -
 
Message retrieved is
how are you----

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.