Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- char encrypt(char * ch, char * key);
- char decrypt(char * ch, char * key);
- char generateKey(char* ch);
- int main(int argc, char * argv[])
- {
- int choice;
- char ch, j, k;
- bool loop = true;
- FILE *fin, *fout;
- if (argc != 5)
- {
- printf ("Usage: cipher option key infile, outfile\n");
- printf ("Option 1 for encryption and 2 for decryption\n");
- exit(1);
- }
- choice = atoi(argv[1]);
- k = atoi(argv[2]);
- fin = fopen(argv[3], "r");
- fout = fopen(argv[4], "w");
- if (fin == NULL || fout == NULL)
- {
- printf("File could not be opened\n");
- exit(1);
- }
- while ( fscanf(fin, "%c", &ch) != EOF )
- {
- if (choice == 1){
- fprintf(fout, "%c", encrypt(ch, k));
- }else if (choice == 2){
- fprintf(fout, "%c", decrypt(ch, k));
- }else {
- printf ("Usage: cipher option key infile, outfile\n");
- printf ("Option 1 for encryption and 2 for decryption\n");
- exit(1);
- }
- }
- fclose(fin);
- fclose(fout);
- return 0;
- }
- char encrypt(char * word, char * key)
- {
- printf("Entered encrypt method.\n");
- printf("%s\n", *word);
- char * alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- int i, j;
- char result = "";
- for (i = 0; i < sizeof(word); i++){
- for (j = 0; j < sizeof(alphabet); j++){
- if (word[i] == alphabet[j]){
- printf("Checking word against alphabet.\n");
- strcat(key[j], result);
- printf("Finished check; appended to result.\n");
- printf("%s\n", result);
- }else if (word[i] == " "){
- strcat(" ", result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement