mercredi 18 avril 2018

How to import GPG key and decrypt file using C++ GPGME

How do I simply load a gpg key to import into a context using GPGME C++ library?

I am trying to build an application that decrypts a file using GPGME but I am struggling to understand the GPU Documentation. The attempt I made consists of trying to create a context and loading a preexisting key into which will be used to decrypt a file but I keep getting "Invalid Input" as a response. I have included the code with output statements removed, but commented the output returns on them.

#include <gpgme.h>
#include <fcntl.h>

gpgme_ctx_t ctx;
gpgme_error_t err;
gpgme_data_t keydata;

// ATTEMPTING TO IMPORT KEY FILE
int fd = open("test.gpg", O_RDONLY);
err = gpgme_data_new_from_fd(&keydata, fd);     // returns Success

// ATTEMPTING TO IMPORT KEY INTO CONTEXT
err = gpgme_op_import(ctx, keydata);            // returns Invalid value

// ATTEMPTING TO CHECK RESULT
// accessing any member of impRes seg faults
err = gpgme_import_result_t impRes = gpgme_op_import_result(ctx); 

// DECRYPT DATA

gpgme_data_t fileDecrypted, fileEncrypted;

int fdEncrypt = open("EncryptedFile.tar.gz", O_RDONLY);
int fdDecrypt = open("DecryptedFile.tar.gz", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);

err = gpgme_data_new_from_fd(&fileEncrypted, fdEncrypt); // returns Success
err = gpgme_data_new_from_fd(&fileDecrypted, fdDecrypt); // returns Success 

gpgme_decrypt_result_t dec_result;
// returns Invalid value
err = gpgme_op_decrypt_start (ctx, fileEncrypted, fileDecrypted);
dec_result = gpgme_op_decrypt_result(ctx); // returns 0

Any input is much appreciated, thanks in advance!

Aucun commentaire:

Enregistrer un commentaire