Advertisement
Jwheeler9

Vigenere

Jun 2nd, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.92 KB | None | 0 0
  1. /*
  2. Author: Justin Wheeler
  3. Date: March 7th, 2016
  4. Purpose: This script will take in a keyword and ask for the user to type in a string once executed.
  5. Purpose: After the user complies the program will shuffle the letters in the typed in string by the
  6. Purpose: values in the passed in keyword.
  7. */
  8.  
  9. //------------------------------------------------------------
  10. //Libraries
  11. #include <cs50.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. //------------------------------------------------------------
  17. //Method Signatures
  18. void killProgram(string _output);
  19. void printCipherLetter(int _arr1[], int _arr2[], int _letter, int _keyIndex, string _keycode);
  20. int checkAlpha(string _value);
  21.  
  22. //------------------------------------------------------------
  23. //Main
  24. int main(int argc, string argv[])
  25. {  
  26.     if(argc == 2)
  27.     {
  28.         //Grab Keycode
  29.         string keycode = argv[1];
  30.        
  31.         if(checkAlpha(keycode) == 0)
  32.         {
  33.             //Alphabets
  34.             int alphabet1[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  35.                   'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  36.             int alphabet2[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
  37.                   'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  38.                
  39.             //Get String to Encrypt
  40.             string userStr;
  41.             userStr = GetString();
  42.            
  43.             //KeyCode Variables
  44.             int keyBase = strlen(keycode);
  45.             int keyCeil = strlen(keycode);
  46.             int keyIndex = 0;
  47.                  
  48.             //Cycle Through 'userStr' and Cipher Letters
  49.             for(int i = 0; i < strlen(userStr) + 1; i++)
  50.             {
  51.                 if(i == strlen(userStr))
  52.                 {
  53.                     printf("\n");
  54.                 }
  55.                    
  56.                 else
  57.                 {
  58.                     if(isalpha(userStr[i]))
  59.                     {
  60.                         keyIndex = keyBase % keyCeil;
  61.                        
  62.                         //Uppercase/Uppercase Condition
  63.                         if(isupper(userStr[i]) && isupper(keycode[keyIndex]))
  64.                         {
  65.                             printCipherLetter(alphabet1, alphabet1, userStr[i], keyIndex, keycode);
  66.                         }
  67.                        
  68.                         //Uppercase/Lowercase Condition
  69.                         else if(isupper(userStr[i]) && islower(keycode[keyIndex]))
  70.                         {
  71.                             printCipherLetter(alphabet1, alphabet2, userStr[i], keyIndex, keycode);
  72.                         }
  73.                        
  74.                         //Lowercase/Uppercase Condition
  75.                         else if (islower(userStr[i]) && isupper(keycode[keyIndex]))
  76.                         {
  77.                             printCipherLetter(alphabet2, alphabet1, userStr[i], keyIndex, keycode);
  78.                         }
  79.                            
  80.                         //Lowercase/Lowercase Condition
  81.                         else
  82.                         {
  83.                             printCipherLetter(alphabet2, alphabet2, userStr[i], keyIndex, keycode);
  84.                         }
  85.                        
  86.                         keyBase++;
  87.                     }
  88.                        
  89.                     //Special Characters Condition
  90.                     else
  91.                     {
  92.                         printf("%c", userStr[i]);
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.        
  98.         else
  99.         {
  100.             killProgram("Provide an Alpha Key!\n");
  101.             return 1;
  102.         }
  103.     }
  104.    
  105.     else
  106.     {
  107.         killProgram("Useage ./vigenere {string key}!\n");
  108.         return 1;
  109.     }
  110.    
  111.     return 0;
  112. }
  113.  
  114. //------------------------------------------------------------
  115. //Methods
  116. void killProgram(string _output)
  117. {
  118.     printf("%s\n", _output);
  119. }
  120.  
  121. void printCipherLetter(int _arr1[], int _arr2[], int _letter, int _keyIndex, string _keycode)
  122. {
  123.     //Cipher Variables
  124.     int indexLetter = 0;
  125.     int indexKey = 0;
  126.     int cipherLetter = 0;
  127.    
  128.     //Letter Conversion
  129.     for(int j = 0; j < 26; j++)
  130.     {
  131.         if(_letter == _arr1[j])
  132.         {
  133.             indexLetter = j;
  134.             break;
  135.         }
  136.     }
  137.    
  138.     //Key Conversion
  139.     for(int k = 0; k < 26; k++)
  140.     {
  141.         if(_keycode[_keyIndex] == _arr2[k])
  142.         {
  143.             indexKey = k;
  144.             break;
  145.         }
  146.     }
  147.    
  148.     //Cipher Output
  149.     cipherLetter = indexLetter + indexKey;
  150.    
  151.     while(cipherLetter > 25)
  152.     {
  153.         cipherLetter -= 26;
  154.     }
  155.    
  156.     printf("%c", _arr1[cipherLetter]);
  157. }
  158.  
  159. int checkAlpha(string _val)
  160. {
  161.     for(int p = 0, len = strlen(_val); p < len; p++)
  162.     {
  163.         if(isalpha(_val[p]) == 0)
  164.         {
  165.             return 1;
  166.         }
  167.     }
  168.    
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement