I'm trying to extract the encrypted RSA data from a gcry_sexp_t object (which I assume is an MPI), so I could store the encrypted data as an encrypted file (without the other gcrypt metadata in the sexp object).
I've tried using gcry_sexp_sprint, which serialized the whole sexp object and which kind of works, but I assume that is not the intended way for storing RSA encrypted bytes on the disk. I've also tried extracting data using gcry_sexp_nth_buffer and gcry_sexp_nth_mpi, both of which just return the string enc-val.
This code is the relevant part:
gcry_sexp_t ciph;
err = gcry_pk_encrypt(&ciph, msgSexp, this->publicKey);
if(err){
throw std::runtime_error("gcry_pk_encrypt error: " + std::string(gcry_strerror(err)));
}
After this, I have a gcry_sexp_t object holding all of the relevant data, of which I would like to extract the encrypted data.
Thank you!
EDIT: I ended up calling gcry_sexp_nth two times to traverse the nested S expression object and then using gcry_sexp_nth_data to get the raw encrypted data. I'm sure there's a better way, though.
Libgcrypt API to deal with SExpression is very powerful. You can get data from the expression easily. Here is a sample code which:
Generate key pair
Encrypt a random data
Extract cipher text value
And this is the output: