[OTR-dev] working with the libgcrypt mpi type and getting the actual integer value
Kjell Braden
kb at pentabarf.de
Thu Feb 28 07:19:55 EST 2013
On 2013-02-27 00:04, Bjorn Kuiper wrote:
> Hi,
>
> I'm trying to develop a C# implementation of the Off-the-record
> library, but I'm having some problems with the libgcrypt library.
>
> Can somebody explain to me how you can print out the actual integer
> value of an hex'd MPI ?
>
> Basically i want to know the integer value of this 'private' key
>
> 48BFDA215C31A9F0B226B3DB11F862450A0F30DA
That *is* the integer value. It's in base 16 though.
>
> I think it is
>
> 415325779662433871844955547383752003988573073626
In base 10, yes.
> but I'm unable to find a way to confirm this using libgcrypt
>
> I tried the following code:
>
> -- code snippit gcry_mpi_t cript_prime; char buffer[50] = {0}; char
> number[50] = {0};
>
> cript_prime = gcry_mpi_new(50);
>
> strcpy(number,"415325779662433871844955547383752003988573073626");
> gcry_mpi_scan(&cript_prime,GCRYMPI_FMT_USG,number,sizeof(number),NULL);
>
>
> gcry_mpi_print(GCRYMPI_FMT_USG,buffer,sizeof(buffer),NULL,cript_prime);
>
> printf("The number tested is: %s\n",buffer);
>
> printf("trying to convert to HEX\n");
>
> /* get actual value */ gcry_mpi_print (GCRYMPI_FMT_HEX, buffer,
> sizeof(buffer), NULL, cript_prime); /* print actual value */
> printf("result: %s\n", buffer); -- end of code snippit
>
> which results in:
>
> -- output The number tested is:
> 415325779662433871844955547383752003988573073626 trying to convert to
> HEX result: 415325779662433871844955547383752003988573073626 -- end
> of output
>
That's because your buffer is too small and the second gcry_mpi_print
fails (which you never realize because you don't check the return code).
The buffer remains unmodified.
If you initialized your buffer with:
> char buffer[101] = {0};
you get this output:
> The number tested is: 415325779662433871844955547383752003988573073626
> trying to convert to HEX
> result: 3431353332353737393636323433333837313834343935353534373338333735323030333938383537333037333632360000
0x34 => ascii 4
0x31 => ascii 1
0x35 => ascii 5
... you get the idea
Conclusion:
GRYMPI_FMT_USG does not print the integer in a string representation but
instead uses the bytes to store a variable length unsigned integer.
GCRYMPI_FMT_HEX does indeed print the integer in a string
representation, but in hexadecimal instead of decimal.
libgcrypt does not seem to offer decimal string output.
--
Kjell
More information about the OTR-dev
mailing list