Mailing List Archive

Problem with public key encription
Hi,

i'm having a problem when trying to encrypt the session key with the public key.

The code in question is:

--------------
// generate session key
sessionKey = new unsigned char[16];
gcry_randomize(sessionKey, longClaveSesion, GCRY_STRONG_RANDOM);
// convert session key to S-Expression
gcry_sexp_build(&data, NULL, "(data (flags raw)(value %b))", (size_t) 16, sessionKey);
// firmamos con la clave publica del destinatario
err = gcry_pk_encrypt(&encryptedSessionKey, data, destinationKey);
--------------

and the returned error is Invalid object. But this only occurs sometimes, the rest
of the time everything works ok. I debugged it and saw the problem was dependant on
the value of the session key, so in general it works ok but with some values of the
session key it fails. I tracked it down to the function gcry_sexp_nth_mpi, which doesn't
return a correct mpi when called with some of my values, but i can't find the problem.

These are some of the values i got working and not:

-------------------
(data
(flags raw)
(value #23F9C6B31389B304F44083B1F7C4E58C#)
)
Result: Success

(data
(flags raw)
(value #81ACBB98319D5EA67F63E28FC234790B#)
)
Result: Invalid object

(data
(flags raw)
(value #D2440A26F494E23DB65F7D9794DB88CA#)
)
Result: Invalid object
-------------------

Does anybody have any clue about what I'm doing wrong?

Thanks.
Re: Problem with public key encription [ In reply to ]
On Thu, 03 Mar 2005 13:44:33 +0100, Madelman said:

> (data
> (flags raw)
> (value #81ACBB98319D5EA67F63E28FC234790B#)
> )
> Result: Invalid object

Negative number. You need to prefix it with 0x00. This is only
required because you use raw data which is expected to be a proper
(non-negative) number.

Anyway, what you are trying to do is highly insecure. You need to pad
the session key - use the pkcs#1 method.

Shalom-Salam,

Werner