From ae2890afb2bc80b986ccf4798490cd4b1208ccff Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Sun, 28 Apr 2013 12:17:25 +0100 Subject: Tidying messages and tidying --- encrypt.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/encrypt.c b/encrypt.c index 880e2c9..90ae0dc 100644 --- a/encrypt.c +++ b/encrypt.c @@ -1,3 +1,10 @@ +/* + * SimpleCrypt + * So simple it's all one function. + * + * Nathan Lasseter (User_4574) + */ + #include #include #include @@ -6,14 +13,14 @@ int main(int argc, char** argv) { // We need a file to encrypt if (argc != 2) { - fprintf(stderr, "Usage: encrypt "); + fprintf(stderr, "Usage: encrypt \n"); return EXIT_FAILURE; } // Now lets open that file FILE* file = fopen(argv[1], "r"); if (file == NULL) { - fprintf(stderr, "No file specified\n"); + fprintf(stderr, "No such file\n"); return EXIT_FAILURE; } @@ -23,27 +30,26 @@ int main(int argc, char** argv) { printf("Enter Passphrase: "); if (fgets(passphrase, 255, stdin) == NULL) { fclose(file); - fprintf(stderr, "Read error or end of file"); + fprintf(stderr, "Read error or end of file\n"); return EXIT_FAILURE; } } while (strlen(passphrase) <= 0); // And a temporary file to hold the encrypted data - char tpath[] = "SimCr.XXXXXX"; + char tpath[] = ".SimCr.XXXXXX"; int tempfd = mkstemp(tpath); FILE* temp = fdopen(tempfd, "w"); // Now we encrypt! - int passlen = strlen(passphrase) - 1; - int c,j=0; + int passlen = strlen(passphrase) - 2; + int c, d, j = 0; + while ((c=fgetc(file)) != EOF) { - if (j == passlen) { - fputc(c ^ passphrase[0], temp); - j=1; - } else { - fputc(c ^ passphrase[j], temp); - j++; - } + d = c ^ passphrase[j]; + fputc(d, temp); + + if (j == passlen) j=0; + else j++; } // Close the files @@ -51,6 +57,9 @@ int main(int argc, char** argv) { fclose(temp); fclose(file); + // Free used memory + free(passphrase); + // Move the file back if (rename(tpath, argv[1]) != 0) { fprintf(stderr, "Your encrypted file is located in %s.", tpath); -- cgit v1.2.1