[Botan-devel] Getting the binary form of the key

Jack Lloyd lloyd at randombit.net
Wed Oct 7 23:10:41 EDT 2009

On Wed, Oct 07, 2009 at 11:03:03PM -0400, Z. S. O. wrote:
> I've had an easy enough job getting the PKCS8-encoded version of
> an RSA_PrivateKey and its X509-encoded equivalent, as seen below, but I'd
> prefer just getting the raw binary version of the keys encoded as a byte
> array. Is there a simple function with this purpose that I am missing? I'm
> thinking something along the lines of the bits_of function for a
> SymmetricKey.

The functions PKCS8::encode() and PKCS8::encrypt_key() will produce
the raw binary PKCS #8 serialization, sending the data into a Pipe&.
X509::encode() will do the same for a public key.

I'm not sure if this exactly what you mean by 'raw binary' - the
output will be the same as the PEM encode variants (except, obviously,
with the PEM encoding). The PKCS #8 and subjectPublicKeyInfo formats
have the advantage of being portable across systems and
implementations, though.

You could also roll something on your own using BigInt::encode(), but
of course you'd have to deal with all the length fields, etc yourself
and nothing else will be able to understand it without special help.


More information about the botan-devel mailing list