Unexpected result when encrypting using gcry_pk_encrypt

Werner Koch wk at gnupg.org
Fri Apr 15 14:01:44 CEST 2016


here are a few quick comments:

> void print_mpi(gcry_mpi_t mpi){
> 	unsigned char *returned_string;
> 	int returned_string_length;
> 	gcry_error_t rc = gcry_mpi_aprint(GCRYMPI_FMT_USG, &returned_string, &returned_string_length, mpi);
> 	if(rc){
> 		fprintf(stderr, "File: %s, Line: %i.\n", __FILE__, __LINE__);
> 		exit(-1);
> 	}
> 	printf("length of returned string: %i\n", returned_string_length);
> 	printf("returned string: ");
> 	int i;
> 	for(i = 0; i < returned_string_length; i++){
> 		printf("%c", returned_string[i]);

You are printing binary data here.  The printing is actually the same as

      fputs ("returned string: ", stdout);
      fwrite (returned_string, returned_string_length, 1, stdout);

If you want a hex presentation you need to use 

      rc = gcry_mpi_aprint(GCRYMPI_FMT_HEX, &returned_string, ....)

GCRYMPI_FMT_USG returns an unsigned integer binary representation.
Given that this is just for debug output, the use of

     gcry_log_debugmpi ("returned string", mpi);

is probably easier (goes by default to stderr, though).

> void print_sexp(gcry_sexp_t sexp){
> 	unsigned char buffer[BUFFER_SIZE];
> 	size_t bytes_printed = gcry_sexp_sprint(sexp, GCRYSEXP_FMT_DEFAULT, buffer, BUFFER_SIZE);

Here you should use GCRYSEXP_FMT_ADVANCED to get a printable output.  Or

     gcry_log_debugsxp ("returned sexp", sexp);



Die Gedanken sind frei.  Ausnahmen regelt ein Bundesgesetz.

More information about the Gcrypt-devel mailing list