Guest User

lab12.c

a guest
Dec 7th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.85 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.     char        ucEncodingKey[] = "DEFGHIJKLMNOPQRSTUVWXYZABC";
  115.        
  116.     // For each character in the source string...
  117.     for (; *src; ++src, ++dest)
  118.     {
  119.         // Determine the offset of this character from the beginning of
  120.         // the alphabet, by subtracting the ASCII value of 'a' from this
  121.         // character. For example, if *src is 'a', then (*src - 'a') will
  122.         // yield 0 because the ASCII values of *src (which is 'a') and 'a'
  123.         // are both 97. If *src is 'd', then (*src - 'a') will yield 3,
  124.         // because the ASCII value of 'd' is 100 and the ascii value of 'a'
  125.         // is 97, and (100 - 97) is 3.
  126.         if (islower(*src) == 1) {
  127.             offset = *src - 'a';
  128.         }
  129.        
  130.         // Obtain the lowercase replacement character and assign
  131.         // it to the current position in the destination.
  132.         *dest = lcEncodingKey[offset];
  133.        
  134.         if (isupper(*src) == 1) {
  135.             offset = *src - 'A';
  136.             *dest = ucEncodingKey[offset];
  137.         }
  138.         if (isdigit(*src) == 1) {
  139.             *dest = *src;
  140.         }
  141.         if (isspace(*src) == 1) {
  142.             *dest = *src;
  143.         }
  144.         if (ispunct(*src) == 1) {
  145.             *dest = *src;
  146.         }
  147.     }
  148.    
  149.     // Null-terminate the destination buffer
  150.     *dest = '\0';
  151.    
  152.     // Return the beginning of the destination buffer
  153.     return destStart;
  154. }
  155.  
  156. /**
  157.  * Decode a string that was encoded using Caesar encoding.
  158.  *
  159.  * @param src
  160.  *   Address of the string to be decoded
  161.  *
  162.  * @param dest
  163.  *   Address of a memory buffer at least as long as the src string, into
  164.  *   which the decoded (decrypted) string will be placed.
  165.  *
  166.  * @return
  167.  *   The address of the first character of the decoded (decrypted) string
  168.  */
  169. char * decodeCaesar(char * src, char * dest)
  170. {
  171.     int         offset;
  172.     char        lcDecodingKey[] = "xyzabcdefghijklmnopqrstuvw";
  173.     char        ucDecodingKey[] = "XYZABCDEFGHIJKLMNOPQRSTUVW";
  174.     char *      destStart = dest;
  175.    
  176.     // For each character in the source string...
  177.     for (; *src; ++src, ++dest)
  178.     {
  179.         // Determine the offset of this character from the beginning of
  180.         // the alphabet, by subtracting the ASCII value of 'a' from this
  181.         // character. For example, if *src is 'a', then (*src - 'a') will
  182.         // yield 0 because the ASCII values of *src (which is 'a') and 'a'
  183.         // are both 97. If *src is 'd', then (*src - 'a') will yield 3,
  184.         // because the ASCII value of 'd' is 100 and the ascii value of 'a'
  185.         // is 97, and (100 - 97) is 3.
  186.         if (islower(*src) == 1) {
  187.             offset = *src - 'a';
  188.         }
  189.        
  190.         // Obtain the lowercase replacement character and assign
  191.         // it to the current position in the destination.
  192.         *dest = lcDecodingKey[offset];
  193.  
  194.         if (isupper(*src) == 1) {
  195.             offset = *src - 'A';
  196.             *dest = ucDecodingKey[offset];
  197.         }
  198.         if (isdigit(*src) == 1) {
  199.             *dest = *src;
  200.         }
  201.         if (isspace(*src) == 1) {
  202.             *dest = *src;
  203.         }
  204.         if (ispunct(*src) == 1) {
  205.             *dest = *src;
  206.         }
  207.     }
  208.    
  209.     // Null-terminate the destination buffer
  210.     *dest = '\0';
  211.    
  212.     // Return the beginning of the destination buffer
  213.     return destStart;
  214. }
  215.  
  216. /**
  217.  * Encode a string using substitution cipher encoding
  218.  *
  219.  * @param lcEncodingKey
  220.  *   Address of the string containing the lowercase encoding key
  221.  *
  222.  * @param src
  223.  *   Address of the string to be encoded
  224.  *
  225.  * @param dest
  226.  *   Address of a memory buffer at least as long as the src string, into
  227.  *   which the substitution-cipher-encoded (encrypted) string will be placed.
  228.  *
  229.  * @return
  230.  *   The address of the first character of the encoded (encrypted) string
  231.  */
  232. char * encodeSubstitution(char * lcEncodingKey, char * src, char * dest)
  233. {
  234.     char        ucEncodingKey[26], currentChar;
  235.     int         i = 0;
  236.     int         offset = 0;
  237.     char *      destStart = dest;
  238.  
  239.     // You were given the lowercase encoding key. From it, create the
  240.     // uppercase encoding key
  241.     // (WRITE CODE HERE)
  242.     while (lcEncodingKey[i] != '\0') {
  243.         if (isalpha(lcEncodingKey[i]) == 1) {
  244.             currentChar = lcEncodingKey[i];
  245.             if (islower(currentChar) == 1) {
  246.                 currentChar = toupper(currentChar);
  247.             }
  248.             ucEncodingKey[i] = currentChar;
  249.         }
  250.         i++;
  251.     }
  252.    
  253.  
  254.     // Implementation of the generalized substitution encoder.
  255.     // (WRITE CODE HERE)
  256.     for (; *src; ++src, ++dest) {
  257.         if (isalpha(*src) == 0) {
  258.             *dest = *src;
  259.         }
  260.         else {
  261.             if (isupper(*src) == 1) {
  262.                 offset = *src - 'A';
  263.                 *dest = ucEncodingKey[offset];
  264.             }
  265.             else {
  266.                 offset= *src - 'a';
  267.                 *dest = lcEncodingKey[offset];
  268.             }
  269.         }
  270.     }
  271.     *dest = '\0';
  272.     return destStart;
  273. }
  274.  
  275. /**
  276.  * Decode a string using substitution cipher decoding
  277.  *
  278.  * @param lcEncodingKey
  279.  *   Address of the string containing the lowercase encoding key.
  280.  *
  281.  * @param src
  282.  *   Address of the string to be decoded
  283.  *
  284.  * @param dest
  285.  *   Address of a memory buffer at least as long as the src string, into
  286.  *   which the substitution-cipher-decoded (decrypted) string will be placed.
  287.  *
  288.  * @return
  289.  *   The address of the first character of the decoded (decrypted) string
  290.  */
  291. char * decodeSubstitution(char * lcEncodingKey, char * src, char * dest)
  292. {
  293.     char        lcDecodingKey[26];
  294.     char        ucDecodingKey[26];
  295.     char        alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
  296.     int         i;
  297.     int         j;
  298.     char        currentChar;
  299.     int         offset = 0;
  300.     char *      destStart = dest;
  301.    
  302.     // Create the lower case decoding key from the lower case encoding key
  303.     // (WRITE CODE HERE)
  304.     for (i = 0; i<26; i++) {
  305.         for (j = 0; j<26; j++) {
  306.             if (lcEncodingKey[j] == alphabet[i]) {
  307.                 lcDecodingKey[i] = alphabet[j];
  308.             }
  309.         }
  310.     }
  311.  
  312.     i = 0;
  313.     // Create the uppercase decoding
  314.     for (i = 0; i<26; i++) {
  315.         ucDecodingKey[i] = toupper(lcDecodingKey[i]);
  316.     }
  317.  
  318.  
  319.     // Implementation of the generalized substitution decoder.
  320.     // (WRITE CODE HERE)
  321.     for (; *src; ++src, ++dest) {
  322.         if (isalpha(*src) == 0) {
  323.             *dest = *src;
  324.         }
  325.         else {
  326.             if (isupper(*src) == 1) {
  327.                 offset = *src - 'A';
  328.                 *dest = ucDecodingKey[offset];
  329.             }
  330.             else {
  331.                 offset = *src - 'a';
  332.                 *dest = lcDecodingKey[offset];
  333.             }
  334.         }
  335.     }
  336.     *dest = '\0';
  337.     return destStart;
  338. }
  339.  
  340. /**
  341.  * Determine if the encoded/decoded string is the same as the original string.
  342.  */
  343. int testEncodeDecode(char * p1, char * p2)
  344. {
  345.     // *****************************************
  346.     // DO NOT CHANGE ANYTHING IN THIS FUNCTION!
  347.     // *****************************************
  348.    
  349.     while (*p1 != '\0' && *p2 != '\0' && *p1 == *p2)
  350.     {
  351.         ++p1, ++p2;
  352.     }
  353.    
  354.     return *p1 != *p2;
  355. }
Add Comment
Please, Sign In to add comment