Last active
September 29, 2016 13:01
-
-
Save JamesCalleja/6b480fdb95142be0c4d6 to your computer and use it in GitHub Desktop.
vigenere.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*#################################### | |
| #vigenere.c # | |
| #Written by James Calleja, 2016 # | |
| # # | |
| #Encrypts a string of text by a # | |
| #cypher word provided by the user # | |
| ###################################### | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <stddef.h> | |
| #include <math.h> | |
| #include <string.h> | |
| #include <ctype.h> | |
| //declair global variables | |
| char *string; //string to be passed in by the usere | |
| char *cipherKey; //holds to cipher key | |
| char alphaLower[] = "abcdefghijklmnopqrstuvwxyz"; //an array to hold the full alphabet | |
| char alphaUpper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
| //declair funtions | |
| void cipher (char*); //cipher the text using the key pased in to the command line | |
| char* getText (); //prompt user for text to pass to cipher | |
| int errorCheckingArgc (int); //vet command line argument argc, must be 2 | |
| int errorCheckingArgv (char*); //vet command line argument argv,must be postitive int | |
| int cipherKeyToInt (char); //convert the cipherKey[x] char to int accounting for upper and lower case | |
| //int argvToInt (char**); //covert the string argv[1] to an int | |
| //main, application logic | |
| int main (int argc, char* argv[]) | |
| { | |
| errorCheckingArgc(argc); | |
| errorCheckingArgv(argv[1]); | |
| cipherKey = argv[1]; | |
| string = getText(); | |
| cipher(string); | |
| return 0; | |
| } | |
| //define functions | |
| int errorCheckingArgc(int argc) | |
| { | |
| if (argc != 2) | |
| { | |
| printf("You have passed inproper arguments\n"); | |
| exit(1); | |
| } | |
| return 0; | |
| } | |
| int errorCheckingArgv(char* argv) | |
| { | |
| for (int count = 0; argv[count] != '\0'; count ++) | |
| { | |
| if (isalpha(argv[count])) | |
| { | |
| continue; | |
| } | |
| else | |
| { | |
| printf("You just fucked up son\n"); | |
| exit(1); | |
| } | |
| } | |
| return 0; | |
| } | |
| char* getText () | |
| { | |
| char *str,c; | |
| int i=0,j=1; | |
| str = (char*)malloc(sizeof(char)); | |
| //printf("Enter the secret message:\n"); | |
| while(c!='\n') | |
| { | |
| c = getc(stdin); //read the input from keyboard standard input | |
| str = (char*)realloc(str,j*sizeof(char)); //re-allocate (resize) memory for character read to be stored | |
| str[i] = c; //store read character by making pointer point to c | |
| i++; | |
| j++; | |
| } | |
| str[i]='\0'; //at the end append null character to mark end of string` | |
| return str; | |
| free(str); //important step the pointer declared must be made free | |
| } | |
| int cipherKeyToInt (char cipherKey) | |
| { | |
| int x; | |
| if (isupper(cipherKey)) | |
| { | |
| x = cipherKey - 65; | |
| } | |
| else | |
| { | |
| x = cipherKey - 97; | |
| } | |
| return x; | |
| } | |
| void cipher (char* string) | |
| { | |
| int k = strlen(cipherKey); //get the length of the cipherKey | |
| int o = -1; //set counter var for the ciperhKey | |
| for (int x = 0 ; string[x] != '\0'; x++ ) | |
| { | |
| if(isalpha(string[x])) //is the ASCii value in the aplabe range | |
| { | |
| o = o + 1; //itterarte through the cipherKey | |
| int i = string[x]; //convert char to ascii value | |
| if(i > 64 && i < 91) //check ASCii value to see if it's an upper case char | |
| { | |
| int u = o % k; //use mod to determind the position in the array to work with | |
| int j = cipherKeyToInt(cipherKey[u]);//get the ASCii value for the oth char in the array -65 for the cipher shift | |
| int z = i + j; //move the char up the aplphabet as per cipherkey | |
| if(z >= 91) //check to see if the char needs to wrap from Z to A | |
| { | |
| z = z - 65; //convert z to array position in alphaUpper[] | |
| int y = (z % 26); //wrap around the aplabet from Z to A | |
| printf("%c",alphaUpper[y]); //print the ciphered char | |
| continue; | |
| } | |
| char cipher = z; //convert z value back to char | |
| printf("%c",cipher); //print the ascii value char | |
| } | |
| if(i > 96 && i < 123) //check ASCii value to see if it's a lower case char | |
| { | |
| int u = o % k; //use mod to determind the position in the array to work with | |
| int j = cipherKeyToInt(cipherKey[u]);//get the ASCii value for the oth char in the array -65 for the cipher shift | |
| int z = i + j; //move the char up the aplphabet as per cipherkey | |
| if(z >= 123) | |
| { | |
| z = z - 97; //convert z to array position in alphaLower[] | |
| int y = (z % 26); | |
| printf("%c",alphaLower[y]); | |
| continue; | |
| } | |
| char cipher = z; //convert z value back to char | |
| printf("%c", cipher); //print the ascii value char | |
| } | |
| } | |
| else | |
| { | |
| printf("%c", string[x]); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment