diff --git a/decrypt.c b/decrypt.c index 64b4b66b2a75a240b8e608621f03ef86c690bad9..895bbadc39e3a7d73a92e1c6f4d550f1918388ab 100644 --- a/decrypt.c +++ b/decrypt.c @@ -2,6 +2,10 @@ #include <string.h> #include <stdlib.h> #include <assert.h> + + +#include "hashtable.h" +#include "proj-2_sha256.h" char** store_password_hashes(char* hashes){ char **password_hashes = (char**)malloc((strlen(hashes)/64)*sizeof(char*)); assert(password_hashes); @@ -14,6 +18,63 @@ char** store_password_hashes(char* hashes){ } return password_hashes; } +char *sha256_byteToHexString(BYTE data[]) { + char *hexC = "0123456789abcdef"; + char *hexS = malloc(65); + if (!hexS) return NULL; + for(BYTE i; i<32; i++) { + hexS[i*2] = hexC[data[i]>>4]; + hexS[i*2+1] = hexC[data[i]&0xF]; + } + hexS[64] = 0; + return hexS; +} +void brute_force(HashTable *ht){ + char brute_guess[4]; + SHA256_CTX ctx; + for(int i=33;i < 127; i++){ + brute_guess[0] = (char)i; + for(int i=33;i < 127; i++){ + brute_guess[1] = (char)i; + for(int i=33;i < 127; i++){ + brute_guess[2] = (char)i; + for(int i=33;i < 127; i++){ + brute_guess[3] = (char)i; + brute_guess[4] = '\0'; + sha256_init(&ctx); + sha256_update(&ctx, brute_guess, strlen(brute_guess)); + BYTE guess[32]; + sha256_final(&ctx, guess); + char* hex_guess = sha256_byteToHexString(guess); + if (hash_table_has(ht, hex_guess)){ + printf("Password is %s\n", brute_guess); + } + } + } + } + } +} +void generate_guesses(int n_guesses, HashTable *ht){ + FILE* file = fopen("proj-2_common_passwords.txt", "r"); + char line[20]; + // SHA256_CTX ctx; + // while (fgets(line, sizeof(line), file)){ + // line[4] = '\0'; + // // printf("%s\n", line); + // sha256_init(&ctx); + // sha256_update(&ctx, line, strlen(line)); + // BYTE guess[32]; + // sha256_final(&ctx, guess); + // char* hex_guess = sha256_byteToHexString(guess); + // if (hash_table_has(ht, hex_guess)){ + // printf("Password is %s\n", line); + // } + // // printf("%s\n", hex_guess); + // } + fclose(file); +} + + void read_hash_file(FILE *file){ if (file){ @@ -25,16 +86,23 @@ void read_hash_file(FILE *file){ } // printf("%s",hashes); char **words = store_password_hashes(hashes); + + HashTable *password_hashes = new_hash_table(20); + for(int i=0; i < strlen(hashes)/64; i++){ + printf("%d: %s\n", i, words[i]); + hash_table_put(password_hashes, words[i], 1); + }; + generate_guesses(10, password_hashes); + brute_force(password_hashes); for(int i=0; i < strlen(hashes)/64; i++){ printf("%d: %s\n", i, words[i]); free(words[i]); }; free(words); + free_hash_table(password_hashes); fclose(file); } } -void generate_guesses(int n_guesses){ -} int main(int argc, char *argv[]){ FILE *file;