Created
November 24, 2017 13:21
-
-
Save biscuitrainbow/3c50fbea1134d1cd6db31d2189866677 to your computer and use it in GitHub Desktop.
This file contains 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
#include<stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
#define ROT 13 | |
#define MAXCHAR 1000 | |
char *readfile(FILE *fp){ | |
char *str = malloc(sizeof(char) * 255); | |
while (fgets(str, MAXCHAR, fp) != NULL){} | |
return str; | |
} | |
char *encrypt(char *text,int rot) { | |
int c, e; | |
char *encrypted_text = malloc(sizeof(char) * strlen(text)); | |
int i; | |
for ( i = 0; i < strlen(text); i++) { | |
if (isupper(text[i])) { | |
if ((e = text[i] + rot) <= 'Z') | |
encrypted_text[i] = e; | |
else { | |
int tmp = e - 'Z' - 1; | |
e = 65 + tmp; | |
encrypted_text[i] = e; | |
} | |
} else if (islower(text[i])) { | |
if ((e = text[i] + rot) <= 'z') | |
encrypted_text[i] = e; | |
else { | |
int tmp = e - 'z' - 1; | |
e = 97 + tmp; | |
encrypted_text[i] = e; | |
} | |
} else | |
encrypted_text[i] = text[i]; | |
} | |
return encrypted_text; | |
} | |
int main(int argc,char *argv[]) { | |
FILE *plain_fp; | |
FILE *encrypted_fp; | |
int rot = 13; | |
int mode = 1; | |
char *encrypted_text; | |
char *plain_text; | |
if(argv[1] != NULL){ | |
plain_fp = fopen(argv[1],"r"); | |
}else{ | |
printf("Invalid Argument : please input file name"); | |
return -1; | |
} | |
if(argv[2] != NULL){ | |
rot = atoi(argv[2]); | |
}else { | |
printf("Invalid Argument : ROT number"); | |
return -1; | |
} | |
if(argv[3] != NULL){ | |
mode = atoi(argv[3]); | |
}else { | |
encrypted_fp = fopen("encypted.txt","w"); | |
} | |
plain_text = readfile(plain_fp); | |
encrypted_text = encrypt(plain_text,rot); | |
if(mode == 2){ | |
printf("Decrypted text : %s",encrypted_text); | |
}else{ | |
fprintf(encrypted_fp,"%s",encrypted_text); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment