fork download
  1. /********************************************
  2.  * Description: Simple Vigenere example program
  3.  * Licence : Public
  4.  * ******************************************/
  5. #include<stdio.h>
  6. #include<ctype.h>
  7. #include<string.h>
  8. #include<stdlib.h>
  9. #define MAX_ROWS 26
  10. #define MAX_COLS 26
  11. #define MAX_LEN_PLAINTEXT 50
  12. #define S_CHAR 'A' // Starting char of latin alphabet
  13. #define E_CHAR 'Z' // Ending char of latin alphabet
  14. #define MOD 26
  15. #if 0
  16. #define INFO "*****************************************\n"\
  17. "* Author: Kwstas Tsit *\n"\
  18. "* Email: obvius1342@yahoo.gr *\n"\
  19. "* Date: 19/12/2014 *\n"\
  20. "* Type : Encryption/Decryption *\n"\
  21. "* Method: Vigenere Algorithm *\n"\
  22. "* More info: Google Vigenere Algorithm *\n"\
  23. "*****************************************\n"\
  24.  
  25. #define CRYPTO_LABEL " ******* ****** * * ***** ******* *******\n" \
  26.   "* * * * * * * * * *\n" \
  27.   "* ****** ***** ***** * * *\n" \
  28.   "* ** * * * * *\n" \
  29.   "******* * * * * * *******\n" \
  30.  
  31. #endif
  32.  
  33. char *vigenere_crypto( char *plain, char *key);
  34. char *vigenere_decrypto( char *encrypted , char *key);
  35. void tab_recta( char tabula_recta[MAX_ROWS][MAX_COLS] );
  36. void print_tab_recta( char tabula_recta[MAX_ROWS][MAX_COLS] );
  37. int read_line( char *str , int n);
  38. void key_repeat( char *key , char *plain);
  39.  
  40. char tabula_recta[MAX_ROWS][MAX_COLS];
  41.  
  42. /*************************************************
  43.  * main function , call the subroutines
  44.  * of tab_recta table , encryption - decryption.
  45.  * ***********************************************/
  46.  
  47. int main(void)
  48. {
  49. char plain[MAX_LEN_PLAINTEXT]={'\0'};
  50. char key[MAX_LEN_PLAINTEXT]={'\0'};
  51.  
  52. //printf("%s" , CRYPTO_LABEL);
  53. //printf("\n %s" , INFO);
  54.  
  55. tab_recta( tabula_recta );
  56. //print_tab_recta( tabula_recta );
  57. printf("Give the plaintext(up to 30 characters) : ");
  58. read_line( plain , MAX_LEN_PLAINTEXT ); //read the message.
  59. printf("Give the key: ");
  60. read_line( key , MAX_LEN_PLAINTEXT ); // the actual length of user's key.
  61.  
  62. key_repeat( plain , key); // key repeat if it is necessary.
  63. //puts(key);
  64.  
  65. printf("\n Cipher: %s " , vigenere_crypto(plain, key) );
  66. printf("\n Plaintext: %s ", vigenere_decrypto( plain , key) );
  67.  
  68. return 0;
  69. }
  70. /********************************************************
  71.  * tab_recta table filling function , uses an 2D array
  72.  * in order to make the tab_recta square table.
  73.  * ******************************************************/
  74. void tab_recta( char tabula_recta[MAX_ROWS][MAX_COLS] )
  75. {
  76.  
  77. int pos , row , col;
  78.  
  79. for( row=0; row < MAX_ROWS; row++){
  80. pos = 0;
  81. for( col=0; col < MAX_COLS; col++){
  82. if( S_CHAR+row+col > E_CHAR && row > 0){
  83. tabula_recta[row][col] = S_CHAR + pos;
  84. pos++;
  85. }
  86. else
  87. tabula_recta[row][col] = S_CHAR+row+col;
  88. }
  89. }
  90.  
  91. return;
  92. }
  93. /***********************************************
  94.  * Function that prints the contents of
  95.  * tabula_recta square if you want it.
  96.  * *********************************************/
  97. #if 0
  98. void print_tab_recta( char tabula_recta[MAX_ROWS][MAX_COLS])
  99. {
  100. int row , col;
  101.  
  102. for( row=0; row<MAX_ROWS; row++) {
  103. printf("\n");
  104. for( col=0; col<MAX_COLS; col++){
  105. printf("%c" , tabula_recta[row][col]);
  106. }
  107. }
  108.  
  109. return;
  110. }
  111. #endif
  112. /********************************************
  113.  * vigenere_crypto makes the encryption
  114.  * The contents come from tabula_recta square
  115.  * it takes the plaintext as argument and the
  116.  * key in order to do the encryption procedure
  117.  * returns the encrypted string of your msg.
  118.  * ******************************************/
  119. char *vigenere_crypto(char *plain , char *key)
  120. {
  121. int row_tab_rec , col_tab_rec , i=0;
  122. char *encrypted = plain;
  123.  
  124. while( encrypted[i] ){
  125. if( !isalpha(encrypted[i])) {
  126. i++;
  127. continue;
  128. }
  129. row_tab_rec = (key[i] - S_CHAR )%MOD; // For the apropriate row.
  130. col_tab_rec = (plain[i] - S_CHAR )%MOD; // For the apropriate column.
  131. encrypted[i] = tabula_recta[row_tab_rec][col_tab_rec]; // The element of the tabula recta square.
  132. i++;
  133. }
  134.  
  135. return encrypted;
  136. }
  137. /****************************************************
  138.  * vigenere_decrypto function makes the decryption
  139.  * in order to gve you the plaintext. It takes
  140.  * the encrypted msg and the key. It produces the
  141.  * decrypted message.
  142.  * It returns the decrypted string which is the
  143.  * previous plaintext.
  144.  * *************************************************/
  145. char *vigenere_decrypto(char *encrypted , char *key)
  146. {
  147. int i=0;
  148.  
  149. char *decrypted = encrypted;
  150.  
  151. while( encrypted[i] ) {
  152. if( !isalpha(encrypted[i])) {
  153. i++;
  154. continue;
  155. }
  156. else if( encrypted[i] >= key[i] )
  157. decrypted[i] = encrypted[i] - (key[i]- S_CHAR)%MOD;
  158.  
  159. else if ( encrypted[i] < key[i] )
  160. decrypted[i] = E_CHAR + 1 - (key[i] - encrypted[i]) ;
  161. i++;
  162. }
  163.  
  164. return decrypted;
  165. }
  166. int read_line( char *str , int n)
  167. {
  168. int ch , i=0;
  169. while( !isalpha( ch = toupper(getchar() )))
  170. ;
  171. while( ch!= '\n' && ch != EOF) {
  172. if( i < n)
  173. str[i++] = ch;
  174. ch = toupper(getchar());
  175. }
  176. str[i] ='\0';
  177.  
  178. return i;
  179. }
  180. /****************************************
  181.  * key_repeat function makes the key for
  182.  * the encryption and decryption procedure.
  183.  * When the key is lower than plaintext
  184.  * the function repeat it in order to be
  185.  * the same in length.
  186.  * **************************************/
  187. void key_repeat(char *plain, char *key) {
  188. size_t plen = strlen(plain);
  189. size_t klen = strlen(key);
  190.  
  191. if ( plen <= klen ) {
  192. key[plen] = '\0';
  193. } else {
  194. char *k2 = key, *k3 = key+klen; /* k2 will continue from the first beginning of k3 (after the copy),
  195. so will have already a copy of the key and so forth until the end of plain */
  196. plain += klen;
  197. while ( *plain!= '\0') {
  198. if( !isalpha(*plain) ) {
  199. *k3++ = *plain++;
  200. k2 = key;
  201. continue;
  202. }
  203. *k3++ = *k2++; // copy the key to k3 location , repeat the key.
  204. plain++;
  205. }
  206. *k3 = '\0';
  207. }
  208. puts(key);
  209. }
Time limit exceeded #stdin #stdout 5s 2252KB
stdin
Standard input is empty
stdout
Standard output is empty