summaryrefslogtreecommitdiff
path: root/25_break_encr/encryption.c
diff options
context:
space:
mode:
Diffstat (limited to '25_break_encr/encryption.c')
-rw-r--r--25_break_encr/encryption.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/25_break_encr/encryption.c b/25_break_encr/encryption.c
new file mode 100644
index 0000000..3e3b6b1
--- /dev/null
+++ b/25_break_encr/encryption.c
@@ -0,0 +1,40 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+void encrypt(FILE * f, int key) {
+ int c;
+ while ((c = fgetc(f)) != EOF) {
+ if (isalpha(c)) {
+ c = tolower(c);
+ c -= 'a';
+ c += key;
+ c %= 26;
+ c += 'a';
+ }
+ printf("%c", c);
+ }
+}
+
+int main(int argc, char ** argv) {
+ if (argc != 3) {
+ fprintf(stderr,"Usage: encrypt key inputFileName\n");
+ return EXIT_FAILURE;
+ }
+ int key = atoi(argv[1]);
+ if (key == 0) {
+ fprintf(stderr,"Invalid key (%s): must be a non-zero integer\n", argv[1]);
+ return EXIT_FAILURE;
+ }
+ FILE * f = fopen(argv[2], "r");
+ if (f == NULL) {
+ perror("Could not open file");
+ return EXIT_FAILURE;
+ }
+ encrypt(f,key);
+ if (fclose(f) != 0) {
+ perror("Failed to close the input file!");
+ return EXIT_FAILURE;
+ }
+ return EXIT_SUCCESS;
+}