From a969d8f041c54314d0f311df67919341c58038b5 Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Tue, 5 Jun 2012 19:43:19 +0100 Subject: SimpleCrypt 0.5 (copied in for historical reasons) --- encrypt.c | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) (limited to 'encrypt.c') diff --git a/encrypt.c b/encrypt.c index 172fbbd..b19015e 100644 --- a/encrypt.c +++ b/encrypt.c @@ -1,37 +1,39 @@ #include #include #include -#include -#include int main(int argc, char** argv) { - char * junk; - /* We need a file to encrypt */ + + // We need a file to encrypt if(argc==1) { - fprintf(stderr, "Arguments fool!"); + fprintf(stderr, "Usage: encrypt "); return 1; } - /* Now lets open that file */ - FILE* file = fopen(argv[1], "r"); - /* And a temporary file to hold the encrypted data */ - char tpath[255] = "/tmp/"; - char timeS[32]; - sprintf(timeS, "%d", (int) time(NULL)); - strcat(tpath, timeS); - FILE* temp = fopen(tpath, "w"); - char passphrase[255]; + // Now lets open that file + FILE* file = fopen(argv[1], "r"); + + // Now we need the passphrase. Get it + char *passphrase = malloc(256); do { - /* Now we need the passphrase. Get it */ - printf("Enter Passphrase:\n"); - junk = fgets(passphrase, 255, stdin); + printf("Enter Passphrase: "); + if ( fgets(passphrase, 255, stdin) == NULL ) { + fclose(file); + fprintf(stderr, "Read error or end of file"); + exit(EXIT_FAILURE); + } } while (strlen(passphrase) <= 0); + // And a temporary file to hold the encrypted data + char tpath[] = "/tmp/SimCr.XXXXXX"; + int tempfd = mkstemp(tpath); + FILE* temp = fdopen(tempfd, "w"); + + // Now we encrypt! int passlen = strlen(passphrase) - 1; int c,j=0; - /* Now we encrypt! */ - while((c=fgetc(file)) != EOF) { - if(j==passlen) { + while ( (c=fgetc(file)) != EOF ) { + if (j == passlen) { fputc(c ^ passphrase[0], temp); j=1; } else { @@ -39,17 +41,19 @@ int main(int argc, char** argv) { j++; } } - /* Close the files */ + + // Close the files fflush(temp); fclose(temp); fclose(file); - /* Move the file back */ - char cmd[255]; + + // Move the file back + char *cmd = malloc(strlen(tpath) + strlen(argv[1]) + 11); sprintf(cmd, "%s %s %s", "/bin/mv", tpath, argv[1]); if (system(cmd) != 0) { fprintf(stderr, "Your encrypted file is located in %s.", tpath); return EXIT_FAILURE; } + return EXIT_SUCCESS; } - -- cgit v1.2.1