From ce19147e916925945d65823543997d397af40dcb Mon Sep 17 00:00:00 2001 From: Nathan Lasseter Date: Sun, 28 Apr 2013 12:40:39 +0100 Subject: Everchanging passphrase to avoid ECB attacks --- Makefile | 13 ++++++++---- decrypt.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ encrypt.c | 4 +++- 3 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 decrypt.c diff --git a/Makefile b/Makefile index dfb3231..a7ff461 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,18 @@ CFLAGS=-Wall -Wextra -encrypt: encrypt.c +all: encrypt decrypt + +%: %.c gcc ${CFLAGS} -o $@ $^ -install: +install: encrypt decrypt cp encrypt /usr/local/bin/encrypt + cp decrypt /usr/local/bin/decrypt -uninstall: +uninstall: /usr/local/bin/encrypt /usr/local/bin/decrypt rm /usr/local/bin/encrypt + rm /usr/local/bin/decrypt -clean: +clean: encrypt decrypt rm encrypt + rm decrypt diff --git a/decrypt.c b/decrypt.c new file mode 100644 index 0000000..305e1ed --- /dev/null +++ b/decrypt.c @@ -0,0 +1,72 @@ +/* + * SimpleCrypt + * Non-ECB version + * So simple it's all one function. + * + * Nathan Lasseter (User_4574) + */ + +#include +#include +#include + +int main(int argc, char** argv) { + + // We need a file to encrypt + if (argc != 2) { + 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 such file\n"); + return EXIT_FAILURE; + } + + // Now we need the passphrase. Get it + char *passphrase = malloc(256); + do { + printf("Enter Passphrase: "); + if (fgets(passphrase, 255, stdin) == NULL) { + fclose(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"; + int tempfd = mkstemp(tpath); + FILE* temp = fdopen(tempfd, "w"); + + // Now we decrypt! + int passlen = strlen(passphrase) - 2; + int c, d, j = 0; + + while ((c=fgetc(file)) != EOF) { + d = c - passphrase[j]; + passphrase[j] = c; + fputc(d, temp); + + if (j == passlen) j=0; + else j++; + } + + // Close the files + fflush(temp); + 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); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/encrypt.c b/encrypt.c index 90ae0dc..6f2cca8 100644 --- a/encrypt.c +++ b/encrypt.c @@ -1,5 +1,6 @@ /* * SimpleCrypt + * Non-ECB version * So simple it's all one function. * * Nathan Lasseter (User_4574) @@ -45,7 +46,8 @@ int main(int argc, char** argv) { int c, d, j = 0; while ((c=fgetc(file)) != EOF) { - d = c ^ passphrase[j]; + d = c + passphrase[j]; + passphrase[j] = d; fputc(d, temp); if (j == passlen) j=0; -- cgit v1.2.1