Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple program that encrypts files in a directory. I can iterate through and everything works perfectly. This is using pub/priv key pair. When decrypting one file at a time, it works as it should. However, if there are multiple files in a directory, or even if I put the filenames in a vector and fopen them for reading/writing respectively, it ONLY decrypts the LAST file in the vector/directory. How is this even possible? It fails on everyone of them on OpenFinal(). Here is the function and heart of the program. Everything else is solid. As stated, it works as a standalone program if I just decrypt one file manually or if there is just ONE file in the directory or vector.

Any help would be appreciated. This makes no sense at all. It seems like an implementation issue on their end.


void handleErrors(void)
{
    // perror("Error: ");
    ERR_print_errors_fp(stderr);
    abort();
}


int envelope_open(EVP_PKEY *priv_key, unsigned char *ciphertext,
                  int ciphertext_len, unsigned char *encrypted_key,
                  int encrypted_key_len, unsigned char *iv,
                  unsigned char **plaintext, int *plaintext_len)
{
    EVP_CIPHER_CTX *ctx;
    int len = 0, ret = 0;
    unsigned char *tmpptxt = NULL;

    if((ctx = EVP_CIPHER_CTX_new()) == NULL)
        return 0;

    if ((tmpptxt = (unsigned char*)malloc(ciphertext_len)) == NULL)
    {
        printf("tmptxt error!\n");
        handleErrors();
    }

    if(EVP_OpenInit(ctx, EVP_aes_256_cbc(), encrypted_key, encrypted_key_len,
                    iv, priv_key) != 1)
    {
        printf("OpenInit error\n");
        handleErrors();
    }

    if(EVP_OpenUpdate(ctx, tmpptxt, &len, ciphertext, ciphertext_len) != 1)
            {
        printf("OpenUpdate error\n");
        handleErrors();
    }
    *plaintext_len = len;

    if(EVP_OpenFinal(ctx, tmpptxt + len, &len) != 1)
            {
        printf("OpenFinal error\n");
                handleErrors();
    }
    *plaintext_len += len;

    *plaintext = tmpptxt;
    tmpptxt = NULL;
    ret = 1;
 err:
    EVP_CIPHER_CTX_free(ctx);
    free(tmpptxt);

    return ret;
}


What I have tried:

Manually decrypting it in a separate program without a vector and works perfectly so the envelope_open() function does work. It just doesn't work with multiple files for some strange reason.
Posted
Updated 28-Nov-22 10:56am
v4
Comments
Shao Voon Wong 28-Nov-22 0:20am    
There is a memory leak in plaintext. plaintext is allocated with new keyword but in the envelope_open(), plaintext is overwritten with tmpptxt. plaintext is now pointing to another memory and the original new'ed memory is lost.
Shao Voon Wong 28-Nov-22 1:00am    
Please call fflush(f_dec) after fwrite()

const char *filename_crypt = t[i].c_str();

should be
const char *filename_crypt = t->c_str();
 
Share this answer
 
v2
Comments
Jason Smith 3 26-Nov-22 20:17pm    
steveb, thanks for that clarification but that does not change anything. The original way I had still worked on iterating the files. Every file is processed in the vector, its just that I get this error on OpenFinal() for all preceding files, except the final file, which gets decrypted:

6C0E0000:error:1C800064:Provider routines:ossl_cipher_unpadblock:bad decrypt:providers\implementations\ciphers\ciphercommon_block.c:124:
Shao Voon Wong 27-Nov-22 3:31am    
if i has the count of files minus 1 in the directory and t is the beginning of the vector, t[i] refers to the last file. Whenever ++t is called, t[i] refers to an non-existent file.
Jason Smith 3 27-Nov-22 16:03pm    
actually that isn't the case because it iterates through every single file regardless. it throws an error for each file so the iteration works. however, i modified the code inside the vector but still has the same behavior
Shao Voon Wong 27-Nov-22 20:04pm    
There is still t[i] in your updated for-loop. Do this:
removeSubstrs (*t, ".crypt");
i was overwriting the key and iv each time. i knew i needed these but looked it over and kept re-using the same one. that is why i was able to only decrypt one file in the directory. only the last file had the correct matching key and iv.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900