daniel_lawson9999

Vigenere Cipher

Oct 8th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. void getCipherVaried(string text, string shifts);
  7. int getCaseValue(int originalChar);
  8.  
  9. int main (int argc, string argv[])
  10. {
  11.     if (argc != 2) //check for two arguments
  12.     {
  13.         printf("please enter two arguments (./vignere ABC)\n");
  14.         return 1;
  15.     }
  16.     string shifts = argv[1];//get the shift arguments
  17.     for (int i = 0; i < strlen(shifts); i++) //make sure each character in the shifts is in the alphabet
  18.     {
  19.         if (!isalpha(shifts[i]))
  20.         {
  21.             printf("please only enter letters!!!!! >:(\n");
  22.             return 1;
  23.         }
  24.     }
  25.     printf("plaintext: "); //ask and store plain text
  26.     string text = get_string();
  27.     getCipherVaried(text,
  28.                     shifts); //send plaintext and shifts to GetCipherVaried, which modifies the text into cyphertext based off the shifts
  29.     printf("ciphertext:%s\n", text ); //print ciphertext
  30.     return 0;
  31.  
  32. }
  33.  
  34. void getCipherVaried(string text, string shifts)
  35. {
  36.     int length = strlen(text); //store the length of the text
  37.     int counter = 0; //counter for changing shifts
  38.     int shiftLength = strlen(shifts); //store the length of the shifts
  39.     for (int i = 0; i < length; i++) //iterate over each character of the text
  40.     {
  41.         if (text[i] != ' '
  42.             &&  isalpha(text[i])) //don't do anything if the current character is a char or not alpha
  43.         {
  44.             if (counter > shiftLength - 1) //reset the counter if outsie shifts length
  45.             {
  46.                 counter = 0;
  47.             }
  48.             int shift = (int)(shifts[counter]);//get the current shift at the counter location
  49.             shift -= getCaseValue(shift); //get the 0 base value of shift
  50.             int originalChar = (int) text[i]; //get the char, and conver to decimal
  51.             int fromZero = originalChar - getCaseValue(originalChar);
  52.             fromZero = (fromZero + shift) % 26; //add shift to fromZero, then % 26
  53.             text[i] = (char) (getCaseValue(originalChar) +
  54.                               fromZero); //add fromZero to caseValue and return to char, and replace the former value of text
  55.             counter++;
  56.         }
  57.     }
  58. }
  59.  
  60.  
  61. int getCaseValue(int
  62.                  originalChar)//get the original char value and return it's case value (97 for lowercase, 65 capital)
  63. {
  64.     int caseValue = 0;
  65.     if (originalChar >= 97)
  66.     {
  67.         caseValue = 97;
  68.     }
  69.     else if (originalChar >= 65)
  70.     {
  71.         caseValue =  65;
  72.     }
  73.     return caseValue;
  74. }
  75.  
  76.  
  77.  
  78.  
  79. /*$ ./vigenere ABC
  80. plaintext:  HELLO
  81. ciphertext: HFNLP*/
Advertisement
Add Comment
Please, Sign In to add comment