fork(2) 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 give you the plaintext. It takes
  140.  * the encrypted msg and the key. It produces the
  141.  * decrypted message.It returns the decrypted string which is the
  142.  * previous plaintext.
  143.  * *************************************************/
  144. char *vigenere_decrypto(char *encrypted , char *key)
  145. {
  146. int i=0;
  147.  
  148. char *decrypted = encrypted;
  149.  
  150. while( encrypted[i] ) {
  151. if( !isalpha(encrypted[i])) {
  152. i++;
  153. continue;
  154. }
  155. else if( encrypted[i] >= key[i] )
  156. decrypted[i] = encrypted[i] - (key[i]- S_CHAR)%MOD;
  157.  
  158. else if ( encrypted[i] < key[i] )
  159. decrypted[i] = E_CHAR + 1 - (key[i] - encrypted[i]) ;
  160. i++;
  161. }
  162.  
  163. return decrypted;
  164. }
  165. /**************************************
  166.  * It reads the line from the user.
  167.  * It converts all the letters into capitals
  168.  * Throw away the first lettes if they aren't
  169.  * valid letters (alphabetic)
  170.  * *************************************/
  171. int read_line( char *str , int n)
  172. {
  173. int ch , i=0;
  174. while( !isalpha( ch = toupper(getchar() )))
  175. ;
  176. while( ch!= '\n' && ch != EOF) {
  177. if( i < n)
  178. str[i++] = ch;
  179. ch = toupper(getchar());
  180. }
  181. str[i] ='\0';
  182.  
  183. return i;
  184. }
  185. /****************************************
  186.  * key_repeat function makes the key for
  187.  * the encryption and decryption procedure.
  188.  * It repeats the key again when its length is over.
  189.  * Even if has non alphabetic letters or not.
  190.  * **************************************/
  191. void key_repeat(char * plain, char * key)
  192. {
  193. int len = strlen(key);
  194. char * copy = malloc(len + 1);
  195. int i;
  196.  
  197. /* Copy to internal buf or memory buf if possible */
  198. if (!copy)
  199. return;
  200. strcpy(copy, key);
  201.  
  202. for (i = 0; *plain; ++plain, ++key)
  203. *key = !isalpha(*plain) ? *plain : copy[i++ % len];
  204. *key = '\0';
  205.  
  206. free(copy);
  207. }
Time limit exceeded #stdin #stdout 5s 2296KB
stdin
Standard input is empty
stdout
Standard output is empty