Guest User

Untitled

a guest
Jul 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int encrypt(char x,int y);
  6. int encrypt_option(void);
  7. int decrypt_menu(void);
  8. int decrypt(char x, int y);
  9.  
  10. int main(void)
  11. {
  12. /* ***************** */
  13. /* MAIN MENU */
  14. /* ***************** */
  15.  
  16. char *menu;
  17. menu = malloc(64*sizeof(char));
  18.  
  19. while(strcmp(menu,"quit")!=0)
  20. {
  21. printf("Menu option: ");
  22. scanf("%s",menu);/*request menu input*/
  23.  
  24. if(strcmp(menu,"enc") == 0)
  25. {
  26. encrypt_option();
  27. }
  28.  
  29. else if(strcmp(menu,"dec") == 0)
  30. {
  31. decrypt_menu();
  32. }
  33. }
  34. return 0;
  35. }
  36.  
  37. int encrypt_option(void)
  38. {
  39. /* ***************************** */
  40. /* REQUESTS STRING TO */
  41. /* ENCRYPT, THEN LOOPS THROUGH */
  42. /* encode() */
  43. /* ***************************** */
  44.  
  45.  
  46. int ctr, shift;
  47. char *intext;
  48. char *outtext;
  49.  
  50. intext = malloc(256 * sizeof(char));
  51. outtext = malloc(256 * sizeof(char));
  52.  
  53. printf("String: ");
  54. scanf("%s",intext);
  55.  
  56. printf("Shift: ");
  57. scanf("%d",&shift);
  58.  
  59. for(ctr = 0;ctr<strlen(intext);ctr++)
  60. {
  61. outtext[ctr] = encrypt(intext[ctr],shift);
  62. }
  63.  
  64. printf("%s\n",outtext);
  65. return 0;
  66. }
  67.  
  68. int encrypt(char x,int y)
  69. {
  70. /* ************************* */
  71. /* THIS FUNCTION ENCRYPTS */
  72. /* ONE CHARACTER AT A TIME */
  73. /* ************************* */
  74.  
  75. /*x is a character */
  76. /*y is the shifting increment*/
  77.  
  78. int offset; /*offset after max value (z, or 122)*/
  79. int z; /*final variable to return*/
  80. if ((x+y) > 122)
  81. {
  82. offset = ((x+y) - 122);
  83. z = 96 + offset; /*96 rather than 97 because 97 = a;*/
  84. }
  85. else
  86. {
  87. z = x + y; /*ASCII CHARACTER + SHIFT VALUE*/
  88. }
  89. return z;
  90. }
  91.  
  92. int decrypt_menu(void)
  93. {
  94. /* ***************** */
  95. /* DECRYPTOR MENU */
  96. /* ***************** */
  97.  
  98. char *dc_menu;
  99. dc_menu = malloc(64*sizeof(char));
  100.  
  101. while(strcmp(dc_menu,"quit")!=0)
  102. {
  103. printf("Decryptor option: ");
  104. scanf("%s",dc_menu);/*request menu input*/
  105.  
  106. if(strcmp(dc_menu,"shift") == 0)
  107. {
  108. shift_decrypt_option();
  109. }
  110. }
  111.  
  112. return 0;
  113. }
  114.  
  115. int shift_decrypt_option()
  116. {
  117. int ctr,shift;
  118. char *intext;
  119. char *outtext;
  120.  
  121. intext = malloc(256*sizeof(char));
  122. outtext = malloc(256*sizeof(char));
  123.  
  124. printf("Encrypted string: ");
  125. scanf("%s",intext);
  126.  
  127. printf("Shift of previous cipher: ");
  128. scanf("%d",&shift);
  129.  
  130. for(ctr = 0;ctr<strlen(intext);ctr++)
  131. {
  132. outtext[ctr] = decrypt(intext[ctr],shift);
  133. }
  134.  
  135. printf("%s\n",outtext);
  136. return 0;
  137. }
  138.  
  139. int decrypt(char x, int y)
  140. {
  141. /*x is a character */
  142. /*y is the shifting increment*/
  143. int offset,z;
  144. if((x-y)<97)
  145. {
  146. offset = 97 - (x-y);
  147. z = 123 - offset;
  148. }
  149. else
  150. {
  151. z = x - y;
  152. }
  153. return z;
  154. }
Add Comment
Please, Sign In to add comment