Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2018
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4.  
  5. // Forward Declarations
  6. char * encodeCaesar(char * src, char * dest);
  7. char * decodeCaesar(char * src, char * dest);
  8. char * encodeSubstitution(char * lcEncodingKey, char * src, char * dest);
  9. char * decodeSubstitution(char * lcEncodingKey, char * src, char * dest);
  10. int testEncodeDecode(char * p1, char * p2);
  11.  
  12.  
  13. int main(int argc, char * argv[])
  14. {
  15. // Test strings to be encoded
  16. char src1[50] = "abcdwxyz";
  17. char src2[50] = "My secret password is: l3t ME PL@y!";
  18. char src3[50] = "Ciphers are fun!";
  19.  
  20. // lowercase encoding key for substitution cipher
  21. char lcEncodingKey[] = "qwertyuiopasdfghjklzxcvbnm";
  22.  
  23. // buffers to hold encoded and decoded strings
  24. char encoded[50];
  25. char decoded[50];
  26.  
  27.  
  28. // *****************************************
  29. // DO NOT CHANGE ANYTHING IN THIS FUNCTION!
  30. // *****************************************
  31.  
  32. printf("Begin test with src1...\n");
  33.  
  34. // Encode the string in src1 using Caesar encoding, and print the result
  35. printf("Caesar encoding '%s' yields '%s'\n",
  36. src1, encodeCaesar(src1, encoded));
  37.  
  38. // Decode the encoded string and print the result.
  39. printf("Caesar decoding '%s' yields '%s'\n",
  40. encoded, decodeCaesar(encoded, decoded));
  41.  
  42. // Confirm that the decoded string matches the original
  43. if (testEncodeDecode(decoded, src1) != 0)
  44. {
  45. printf("Error: Caesar encode/decode src1 does not work properly!\n");
  46. return 1;
  47. }
  48.  
  49. printf("Successfully encoded/decoded src1 using Caesar cipher.\n\n");
  50.  
  51. printf("Begin test with src2...\n");
  52.  
  53. // Encode the string in src2 using Caesar encoding, and print the result
  54. printf("Caesar encoding '%s' yields '%s'\n",
  55. src2, encodeCaesar(src2, encoded));
  56.  
  57. // Decode the encoded string and print the result.
  58. printf("Caesar decoding '%s' yields '%s'\n",
  59. encoded, decodeCaesar(encoded, decoded));
  60.  
  61. // Confirm that the decoded string matches the original
  62. if (testEncodeDecode(decoded, src2) != 0)
  63. {
  64. printf("Error: Caesar encode/decode src2 does not work properly!\n");
  65. return 1;
  66. }
  67.  
  68. printf("Successfully encoded/decoded src2 using Caesar cipher.\n\n");
  69.  
  70. printf("Begin test with src3...\n");
  71.  
  72. // Encode the string in src3 using the substitution cipher, and print the result
  73. printf("Substitution encoding '%s' yields '%s'\n",
  74. src3, encodeSubstitution(lcEncodingKey, src3, encoded));
  75.  
  76. // Decode the encoded string and print the result
  77. printf("Substitution decoding '%s' yields '%s'\n",
  78. encoded, decodeSubstitution(lcEncodingKey, encoded, decoded));
  79.  
  80. // Confirm that the decoded string matches the original
  81. if (testEncodeDecode(decoded, src3) != 0)
  82. {
  83. printf("Error: Substitution encode/decode src3 does not work properly!\n");
  84. return 1;
  85. }
  86.  
  87. printf("Successfully encoded/decoded src2 using Substitution cipher.\n\n");
  88.  
  89. return 0;
  90. }
  91.  
  92.  
  93.  
  94. /**
  95. * Encode a string using Caesar encoding.
  96. *
  97. * @param src
  98. * Address of the string to be encoded
  99. *
  100. * @param dest
  101. * Address of a memory buffer at least as long as the src string, into
  102. * which the Caesar-encoded (encrypted) string will be placed.
  103. *
  104. * @return
  105. * The address of the first character of the Caesar-encoded (encrypted) string
  106. */
  107. char * encodeCaesar(char * src, char * dest)
  108. {
  109. int offset;
  110. char * destStart = dest;
  111.  
  112. // lowercase encoding key
  113. char lcEncodingKey[] = "defghijklmnopqrstuvwxyzabc";
  114.  
  115. // For each character in the source string...
  116. for (; *src; ++src, ++dest)
  117. {
  118. // Determine the offset of this character from the beginning of
  119. // the alphabet, by subtracting the ASCII value of 'a' from this
  120. // character. For example, if *src is 'a', then (*src - 'a') will
  121. // yield 0 because the ASCII values of *src (which is 'a') and 'a'
  122. // are both 97. If *src is 'd', then (*src - 'a') will yield 3,
  123. // because the ASCII value of 'd' is 100 and the ascii value of 'a'
  124. // is 97, and (100 - 97) is 3.
  125. offset = *src - 'a';
  126.  
  127. // Obtain the lowercase replacement character and assign
  128. // it to the current position in the destination.
  129. *dest = lcEncodingKey[offset];
  130. }
  131.  
  132. // Null-terminate the destination buffer
  133. *dest = '\0';
  134.  
  135. // Return the beginning of the destination buffer
  136. return destStart;
  137. }
  138.  
  139. /**
  140. * Decode a string that was encoded using Caesar encoding.
  141. *
  142. * @param src
  143. * Address of the string to be decoded
  144. *
  145. * @param dest
  146. * Address of a memory buffer at least as long as the src string, into
  147. * which the decoded (decrypted) string will be placed.
  148. *
  149. * @return
  150. * The address of the first character of the decoded (decrypted) string
  151. */
  152. char * decodeCaesar(char * src, char * dest)
  153. {
  154. int offset;
  155. char lcDecodingKey[] = "xyzabcdefghijklmnopqrstuvw";
  156. char * destStart = dest;
  157.  
  158. // For each character in the source string...
  159. for (; *src; ++src, ++dest)
  160. {
  161. // Determine the offset of this character from the beginning of
  162. // the alphabet, by subtracting the ASCII value of 'a' from this
  163. // character. For example, if *src is 'a', then (*src - 'a') will
  164. // yield 0 because the ASCII values of *src (which is 'a') and 'a'
  165. // are both 97. If *src is 'd', then (*src - 'a') will yield 3,
  166. // because the ASCII value of 'd' is 100 and the ascii value of 'a'
  167. // is 97, and (100 - 97) is 3.
  168. offset = *src - 'a';
  169.  
  170. // Obtain the lowercase replacement character and assign
  171. // it to the current position in the destination.
  172. *dest = lcDecodingKey[offset];
  173. }
  174.  
  175. // Null-terminate the destination buffer
  176. *dest = '\0';
  177.  
  178. // Return the beginning of the destination buffer
  179. return destStart;
  180. }
  181.  
  182. /**
  183. * Encode a string using substitution cipher encoding
  184. *
  185. * @param lcEncodingKey
  186. * Address of the string containing the lowercase encoding key
  187. *
  188. * @param src
  189. * Address of the string to be encoded
  190. *
  191. * @param dest
  192. * Address of a memory buffer at least as long as the src string, into
  193. * which the substitution-cipher-encoded (encrypted) string will be placed.
  194. *
  195. * @return
  196. * The address of the first character of the encoded (encrypted) string
  197. */
  198. char * encodeSubstitution(char * lcEncodingKey, char * src, char * dest)
  199. {
  200. char ucEncodingKey[26];
  201.  
  202. // You were given the lowercase encoding key. From it, create the
  203. // uppercase encoding key
  204. // (WRITE CODE HERE)
  205.  
  206.  
  207. // Implementation of the generalized substitution encoder.
  208. // (WRITE CODE HERE)
  209. }
  210.  
  211. /**
  212. * Decode a string using substitution cipher decoding
  213. *
  214. * @param lcEncodingKey
  215. * Address of the string containing the lowercase encoding key.
  216. *
  217. * @param src
  218. * Address of the string to be decoded
  219. *
  220. * @param dest
  221. * Address of a memory buffer at least as long as the src string, into
  222. * which the substitution-cipher-decoded (decrypted) string will be placed.
  223. *
  224. * @return
  225. * The address of the first character of the decoded (decrypted) string
  226. */
  227. char * decodeSubstitution(char * lcEncodingKey, char * src, char * dest)
  228. {
  229. char lcDecodingKey[26];
  230. char ucDecodingKey[26];
  231.  
  232. // Create the lower case decoding key from the lower case encoding key
  233. // (WRITE CODE HERE)
  234.  
  235.  
  236. // Create the uppercase decoding
  237. // (WRITE CODE HERE)
  238.  
  239.  
  240. // Implementation of the generalized substitution decoder.
  241. // (WRITE CODE HERE)
  242. }
  243.  
  244. /**
  245. * Determine if the encoded/decoded string is the same as the original string.
  246. */
  247. int testEncodeDecode(char * p1, char * p2)
  248. {
  249. // *****************************************
  250. // DO NOT CHANGE ANYTHING IN THIS FUNCTION!
  251. // *****************************************
  252.  
  253. while (*p1 != '\0' && *p2 != '\0' && *p1 == *p2)
  254. {
  255. ++p1, ++p2;
  256. }
  257.  
  258. return *p1 != *p2;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement