C++ Program to Perform Cryptography using Transposition Technique

This is a C++ Program to implement transposition technique. In cryptography, a transposition cipher is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered). Mathematically a bijective function is used on the characters’ positions to encrypt and an inverse function to decrypt.

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

Output:

$ g++ TranspositionTechnique.cpp
$ 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----
------------------
(program exited with code: 0)
Press return to continue

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.