Encrypting flexible amount of data with GPGME

188 views Asked by At

I'm currently writing a C++ application and would like to use GPGME for message signing, encryption and key management. I know I can encrypt data in this way:

    err = gpgme_op_encrypt(mContext, recipients,...);
    if(err) {
        // .. error handling
    }
    result = gpgme_op_encrypt_result(mContext);
    if(result->invalid_recipients){
        //error handling
    }

    nbytes = gpgme_data_seek(encrypted_text, 0, SEEK_SET);
    if(nbytes == -1) {
       //error handling         
    }  
    buffer = malloc(MAXLEN);
    nbytes = gpgme_data_read(encrypted_text, buffer, MAXLEN);

But as one can see I would have to use MAXLEN as limit for reading the encrypted data in my buffer. Is there a way to determine how long my encrypted data result will be in advance (given the plaintex)? Or will I have to accept the static limit?

1

There are 1 answers

1
Oncaphillis On BEST ANSWER

I'm not familiar with this particular API but the gpgme_data_seek and gpgme_data_read call look like they may behave like read() and seek() from the file I/O system.

(1) Simply allocate as much buffer as you can effort (lets say N).

(2) Call n=gpgme_data_read(...,N) until N!=n.

(3) Check for error conditions (my guess is n<0)

proceed until you have processed all data you are interested in.