summaryrefslogtreecommitdiff
path: root/decrypt.c
diff options
context:
space:
mode:
authorNathan Lasseter <nathan.je.lasseter@googlemail.com>2013-04-28 12:40:39 +0100
committerNathan Lasseter <nathan.je.lasseter@googlemail.com>2013-04-28 12:40:39 +0100
commitce19147e916925945d65823543997d397af40dcb (patch)
tree2d0b6055ce6a443d58256147236b18a4b762d721 /decrypt.c
parentae2890afb2bc80b986ccf4798490cd4b1208ccff (diff)
Everchanging passphrase to avoid ECB attacksnonecb
Diffstat (limited to 'decrypt.c')
-rw-r--r--decrypt.c72
1 files changed, 72 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char** argv) {
+
+ // We need a file to encrypt
+ if (argc != 2) {
+ fprintf(stderr, "Usage: encrypt <file>\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;
+}