munira kamari Posted October 20, 2013 Posted October 20, 2013 I have to find the encryption key from a list of words, I have the plain and cipher text and have written a code which decrypts the cipher text using the words from the file and then compares the plain text, if its same it should print the key used.I used the EVP_BytesToKey to convert the word into key. The IV used for encryption was 0 and the Key is not more than 16 characters long. Problem is i get error Segmentation fault(core dumped). Can anyone help me with this? #include <stdio.h> #include <assert.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #include <openssl/aes.h> #include <openssl/evp.h> int main(int argc, char *argv[]) { int ret = 0; unsigned char pltmp[1024]; unsigned char citmp[1024]; unsigned char iv2[16] = {0}; unsigned char plaintext[1024] ; FILE *fp; /* opening file for reading plaintext*/ fp = fopen("some.txt" , "rb"); if(fp == NULL) { perror("Error opening file"); return(-1); } fgets (plaintext, 1024, fp); fclose(fp); /*opening file to read key*/ FILE *keyf; keyf = fopen("words.txt", "r"); /*opening file to read cipher text*/ FILE *ct; ct = fopen("some.aes-128-cbc" , "rb"); if(ct == NULL) { perror("Error opening file"); return(-1); } fgets (citmp, 1024, ct); fclose(ct); const char *password; /* Creating while loop to read keys until correct key found*/ while(fgets((unsigned char *)password,16,keyf)) { const EVP_CIPHER *cipher; const EVP_MD *dgst = NULL; unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH]; const unsigned char *salt = NULL; int i; OpenSSL_add_all_algorithms(); cipher = EVP_get_cipherbyname("aes-128-cbc"); if(!cipher) { fprintf(stderr, "no such cipher\n"); return 1; } dgst=EVP_get_digestbyname("md5"); if(!dgst) { fprintf(stderr, "no such digest\n"); return 1; } if(!EVP_BytesToKey(cipher, dgst, salt, (unsigned char *) password, strlen(password), 1, key, iv)) { fprintf(stderr, "EVP_BytesToKey failed\n"); return 1; } //printf("Key: "); for(i=0; i<cipher->key_len; ++i) { printf("%02x", key[i]); } printf("\n"); //printf("IV: "); for(i=0; i<cipher->iv_len; ++i) { printf("%02x", iv[i]); } printf("\n"); /*Decrypting using the key generated by EVP_BytesToKey and IV is 0*/ EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init(&ctx); EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(),NULL, key, iv2, 0); printf("dec\n"); EVP_Cipher(&ctx, pltmp, citmp, 1024); printf("compare\n"); if (strcmp(pltmp, plaintext)==0) {printf("success"); printf("%s Key: ",(unsigned char *)password); break;} else {printf("prob");} EVP_CIPHER_CTX_cleanup(&ctx); } } Quote
digip Posted October 20, 2013 Posted October 20, 2013 c++? Try http://www.compileonline.com/compile_cpp_online.php and see what the errors are when compiling. Even if not c++ and strait C, should show you compile errors. You can choose the drop down menu, pick your language, paste the above in, hit compile and it should show where most the issues are. I'm not a programmer by nature, but its a half decent site to get an idea whats happening to debug the code above. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.