I'm trying to encrypt a message using asymmetric private-public keys.
In Botan
, using Load_key()
functions, I read the private key and want to extract it's public key from it. For constructing of an RSA public key in it's constructor, I'll need a "Algorithm Identifier" object and "key bits" which I have. The algorithm identifier object using pcks8_algorithm_identifier()
function.
The problem is the "Key Bits" which returns a secure_vector<unsigned char>
instead of a vector<unsigned char>
and I encounter a bad::alloc exception
when I want to pass it to RSA_PublicKey constructor.
Does anyone encounter such problem? If there is an alternative way of asymmetric encryption by loading keys from an input file in Botan I'll appreciate that
Botan uses two interfaces to represent asymmetric key pairs: Public_Key
and Private_Key
. The Private_Key
interface inherits from Public_Key
. Therefore, when you obtained e.g. an RSA_PrivateKey
via PKCS8::load_key
, this object already represents both the public and the private key. That is, you can plug this object into other methods that expect a Public_Key
.
For accessing the raw key bits, the Public_Key
interface defines a std::vector<uint8_t> public_key_bits()
. The Private_Key
interface has an additional secure_vector<uint8_t> private_key_bits()
. Therefore, every Private_Key
instance should have both public_key_bits
and private_key_bits
available.
Reference: https://github.com/randombit/botan/blob/master/src/lib/pubkey/pk_keys.h
Additional note: The secure_vector
class is a std::vector
with a special allocator that ensures the underlying memory is overwritten when the object is destructed, so that sensitive information like private key bits are not remaining in memory. If you actually have to convert a secure_vector
to a normal vector, the convenience function Botan::unlock
is available (https://github.com/randombit/botan/blob/master/src/lib/base/secmem.h).