From bed4b90c91037dc4727303b241321c9af03b757a Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 3 Sep 2024 01:28:19 -0500 Subject: Consolidate encryption and decryption function --- src/VX-GAMBLEGROUND.c | 27 ++++++++++++++++----------- src/encryption.c | 49 ++++++++++++++++++++++++++++++------------------- src/encryption.h | 8 ++++++++ 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/src/VX-GAMBLEGROUND.c b/src/VX-GAMBLEGROUND.c index d7e1842..393ed48 100644 --- a/src/VX-GAMBLEGROUND.c +++ b/src/VX-GAMBLEGROUND.c @@ -159,7 +159,8 @@ int scanundencrypt(void *passed) { struct sande *p = (struct sande *)passed; p->scanned = scanfiles(p->STARTPATH, p->cmp); - encryptvxgg(p->scanned, p->passphrase); + //encryptvxgg(p->scanned, p->passphrase); + ENorDE_cryptvxgg(p->scanned, p->passphrase, VXGG_ENCRYPT); return 0; } @@ -226,16 +227,20 @@ int main(int argc, char *argv[]) { // Deal with decrypting flag - // if((args.flags & SKIPSLOTS) && (args.inputpass != NULL)) { - // struct sande pass = { - // .cmp = alphasort, - // .passphrase = passphrase, - // .scanned = NULL, - // .STARTPATH = FILESCAN_START - // }; - // decrypt((void*)&pass); - // return 0; - // } + if((args.flags & SKIPSLOTS) && (args.inputpass != NULL)) { + // struct sande pass = { + // .cmp = alphasort, + // .passphrase = passphrase, + // .scanned = NULL, + // .STARTPATH = FILESCAN_START + // }; + // decrypt((void*)&pass); + + struct nodelist *files = NULL; + files = scanfiles(FILESCAN_START, alphasort); + ENorDE_cryptvxgg(files, passphrase, VXGG_DECRYPT); + return 0; + } struct bullshit stuff; struct nodelist *files = NULL; diff --git a/src/encryption.c b/src/encryption.c index 8b0055c..8cccca8 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -9,6 +9,7 @@ #include "encryption.h" #include "ll.h" +#include "VX-GAMBLEGROUND.h" #include #include @@ -202,47 +203,57 @@ int rename_format(const char *fullpath, const char *format, ...) { } - -int encryptvxgg(const struct nodelist *list, const char *passphrase) { +int ENorDE_cryptvxgg(const struct nodelist *list, const char *passphrase, int flag) { int fd = -1; - regex_t encext; regcomp(&encext, "(.+\\.vxgg)$", REG_EXTENDED | REG_ICASE | REG_NEWLINE); - // Encrypt everything for(const struct nodelist *p = list; p != NULL; p = p->next) { - if(p->fullpath == NULL) // Make sure I don't accidentally do anything with that last element + if(p->fullpath == NULL) continue; - if(regexec(&encext, p->fullpath, 0, NULL, 0) != REG_NOMATCH) // Don't encrypt already encrypted files + if(regexec(&encext, p->fullpath, 0, NULL, 0) != REG_NOMATCH && flag == VXGG_ENCRYPT) // Don't encrypt already encrypted files + continue; + if(regexec(&encext, p->fullpath, 0, NULL, 0) == REG_NOMATCH && flag == VXGG_DECRYPT) // Skip non-matching files continue; fd = open(p->fullpath, O_RDWR); if(fd < 0) { #ifdef TESTING - error(0, errno, " Could not open \"%s\" for encryption", p->fullpath); + error(0, 0, " Couldn't open \"%s\" for %s", p->fullpath, (flag == VXGG_ENCRYPT) ? "encryption" : "decryption"); #endif continue; } - passencblock(fd, passphrase); close(fd); - #ifndef TESTING - rename_format(p->fullpath, "%s.vxgg", p->fullpath); - #else - rename_format(p->fullpath, "%s_%s.vxgg", p->fullpath, passphrase); - #endif + + if(flag == VXGG_ENCRYPT) { + #ifndef TESTING + rename_format(p->fullpath, "%s.vxgg", p->fullpath); + #else + rename_format(p->fullpath, "%s_%s.vxgg", p->fullpath, passphrase); + #endif + } + + if(flag == VXGG_DECRYPT) { + char *newname = NULL; + #ifndef TESTING + asprintf(&newname, "%%0.%lds", strlen(p->fullpath) - strlen(".vxgg")); + #else + asprintf(&newname, "%%0.%lds", strlen(p->fullpath) - strlen(".vxgg") - PHRASESIZE - 1); + #endif + rename_format(p->fullpath, newname, p->fullpath); + free(newname); + } } + /* The beauty of my shitty encryption function is that it's reversible by just running it again with the + // same password. Most of this function is just picking the right files to operate on and deciding how to + // rename them */ + return 0; } -// int decryptvxgg(const struct nodelist *list, const char *passphrase) { - - -// return 0; -// } - /* int main() { int fd = open("test.txt", O_RDWR); diff --git a/src/encryption.h b/src/encryption.h index 15dd568..664e4e6 100644 --- a/src/encryption.h +++ b/src/encryption.h @@ -13,4 +13,12 @@ size_t passencblock(int fd, const char *passphrase); // Encrypt a nodelist int encryptvxgg(const struct nodelist *list, const char *passphrase); +// Decrypt a nodelist +int decryptvxgg(const struct nodelist *list, const char *passphrase); + +// Encrypt or decrypt a nodelist +#define VXGG_ENCRYPT 0 +#define VXGG_DECRYPT 1 +int ENorDE_cryptvxgg(const struct nodelist *list, const char *passphrase, int flag); + #endif \ No newline at end of file -- cgit v1.2.3