Search code examples
public-keycrypto++

Load public key from unsigned char array


I have a public key as an array of bytes from xxd:

unsigned char publicKey_txt[] = {
 0x30, 0x82, 0x02, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, .. }; 

From previous Stack Overflow questions I have a general understanding that in my case a StringSink followed by load should work

StringSource publicstring(publicKey_txt, true, NULL);
publicKey.Load(publicstring);

Simply loading from text file works, but when I load from StringSink I get an error:

Error: BER decode error

How do I load a public key from unsigned char array?


Solution

  • Found my answer from @jww - Load RSA PKCS#1 private key from memory?

    In my case slight modification instead of SinkSource I use the ArraySource and publicKey_txt_len is size of char array publicKey_txt.

    CryptoPP::ArraySource as( publicKey_txt, publicKey_txt_len, true);
    publicKey.Load(as);