0

I'm trying to encrypt files through openssl command line and decrypting them through c++. I'm running the command:

openssl enc -aes-256-cbc -e -in test.txt -out test.txt.enc -K (64 character length key) -iv (32 character length iv)

and heres my c++ code for decrypting:

typedef struct cipher_params_t {
unsigned char *key;
unsigned char *iv;
unsigned int encrypt;
const EVP_CIPHER *cipher_type;
}

 void file_decrypt(cipher_params_t *params, FILE *ifp, FILE *ofp){

    int cipher_block_size = EVP_CIPHER_block_size(params->cipher_type);
    unsigned char in_buf[BUFSIZE], out_buf[BUFSIZE + cipher_block_size];

    int num_bytes_read, out_len;
    EVP_CIPHER_CTX *ctx;

    ctx = EVP_CIPHER_CTX_new();
    if(ctx == NULL){
        fprintf(stderr, "ERROR: EVP_CIPHER_CTX_new failed. OpenSSL error: %s\n", ERR_error_string(ERR_get_error(), NULL));
        cleanup(params, ifp, ofp, ERR_EVP_CTX_NEW);
    }



    if(!EVP_DecryptInit_ex(ctx, params-> cipher_type, NULL, params->key, params->iv)){
        fprintf(stderr, "ERROR: EVP_CipherInit_ex failed. OpenSSL error: %s\n", ERR_error_string(ERR_get_error(), NULL));
        EVP_CIPHER_CTX_cleanup(ctx);
        cleanup(params, ifp, ofp, ERR_EVP_CIPHER_INIT);
    }

    while(1){

        num_bytes_read = fread(in_buf, sizeof(unsigned char), BUFSIZE, ifp);
        if (ferror(ifp)){
            fprintf(stderr, "ERROR: fread error: %s\n", strerror(errno));
            EVP_CIPHER_CTX_cleanup(ctx);
            cleanup(params, ifp, ofp, errno);
        }
        if(!EVP_DecryptUpdate(ctx, out_buf, &out_len, in_buf, num_bytes_read)){
            fprintf(stderr, "ERROR: EVP_CipherUpdate failed. OpenSSL error: %s\n", ERR_error_string(ERR_get_error(), NULL));
            EVP_CIPHER_CTX_cleanup(ctx);
            cleanup(params, ifp, ofp, ERR_EVP_CIPHER_UPDATE);
        }
        fwrite(out_buf, sizeof(unsigned char), out_len, ofp);
        if (ferror(ofp)) {
            fprintf(stderr, "ERROR: fwrite error: %s\n", strerror(errno));
            EVP_CIPHER_CTX_cleanup(ctx);
            cleanup(params, ifp, ofp, errno);
        }
        if (num_bytes_read < BUFSIZE) {
            /* Reached End of file */
            break;
        }
    }

    /* Now cipher the final block and write it out to file */
    if(!EVP_DecryptFinal_ex(ctx, out_buf, &out_len)){
        fprintf(stderr, "ERROR: EVP_CipherFinal_ex failed. OpenSSL error: %s\n", ERR_error_string(ERR_get_error(), NULL));
        EVP_CIPHER_CTX_cleanup(ctx);
        cleanup(params, ifp, ofp, ERR_EVP_CIPHER_FINAL);
    }
    fwrite(out_buf, sizeof(unsigned char), out_len, ofp);
    if (ferror(ofp)) {
        fprintf(stderr, "ERROR: fwrite error: %s\n", strerror(errno));
        EVP_CIPHER_CTX_cleanup(ctx);
        cleanup(params, ifp, ofp, errno);
    }
    EVP_CIPHER_CTX_cleanup(ctx);
}



int main()
{
FILE *f_input, *f_dec;

cipher_params_t *params = (cipher_params_t *) malloc(sizeof(cipher_params_t));
unsigned char key[] = "(my 64 character length key"
unsigned char iv[] = "(my 64 character length IV)"


f_input = fopen("encrypted_file", "rb");
if (!f_input) {
    /* Unable to open file for reading */
    fprintf(stderr, "ERROR: fopen error: %s\n", strerror(errno));
    return errno;
}

f_dec = fopen("decrypted_file", "wb");
if (!f_dec) {
    /* Unable to open file for writing */
    fprintf(stderr, "ERROR: fopen error: %s\n", strerror(errno));
    return errno;
}

file_decrypt(params, f_input, f_dec);


fclose(f_input);
fclose(f_dec);


free(params);

return 0;
}

I'm getting the error: ERROR: EVP_CipherFinal_ex failed. OpenSSL error: error:06065064:lib(6):func(101):reason(100)

When I flip the decrypt function into encrypt in order to encrypt a file and then switch everything back to decrypt on that file, I'm able to successfully decrypt the file, but not when it's done through the command line. If it helps, i'm using OpenSSL version 1.0.1e-fips

I'm running this on eclipse with the crypto library.

Could this be because of maybe different versions through commandline vs what eclipse is using? or maybe is there some other options set through the command line that i'm not including in my decrypt function?

1 Answers1

0

I found 2 problems right now:

  1. You have not assigned key and iv in your main to params structure.
  2. Remember that in your code key and iv should be 32 byte and 16 bytes key ad iv, not the hex string that you send to command line. convert command line to real byte array key and iv.
Afshin
  • 8,839
  • 1
  • 18
  • 53
  • so i've been looking into converting the command line hex string into a real byte array and can't seem to quite figure it out, any resources you'd be able to share would be really appreciated – mycsacc Oct 29 '18 at 14:35
  • @mycsacc converting hex string to byte array is simple. you can find it easily if you search in google. – Afshin Oct 29 '18 at 21:24