Unexpected result when encrypting using gcry_pk_encrypt

Erik Nellessen erik.nellessen at informatik.hu-berlin.de
Fri Apr 15 18:31:24 CEST 2016


Ah, now I see! I misunderstood the gcry_mpi_scan function. I had to convert my strings to a binary representation, before passing them to gcry_mpi_scan. Then, also the mpis consist mostly of cryptographic signs, if printed directly. When using the functions you supposed, of course a nicer representation is shown.

I was able to encrypt some data now, and also to decrypt it, everything went fine.

Thank you very much for your help!
Erik

Werner Koch:
> Hi,
> 
> 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
> use
> 
>      gcry_log_debugsxp ("returned sexp", sexp);
> 
> 
> 
> 
> Salam-Shalom,
> 
>    Werner
> 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: OpenPGP digital signature
URL: </pipermail/attachments/20160415/35d9b239/attachment-0001.sig>


More information about the Gcrypt-devel mailing list