Advertisement
Alexlizx

lab12

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