knifediy

博客园 首页 新随笔 联系 订阅 管理

string and StringSource (load):

string spki = ...;
StringSource ss(spki, true /*pumpAll*/);

RSA::PublicKey publicKey;
publicKey.Load(ss);

vector and ArraySource (load):

vector<byte> spki = ...;
ArraySource as(&spki[0], spki.length(), true /*pumpAll*/);

RSA::PublicKey publicKey;
publicKey.Load(as);

string and StringSink (save)

string spki;
StringSink ss(spki);

RSA::PublicKey publicKey(...);
publicKey.Save(ss);

vector (save)

Below is an example of saving to and loading from a std::vector. You have to use an intermediate ByteQueue to save because you can't easily create a VectorSink.

 

AutoSeededRandomPool prng;
RSA::PrivateKey pk1, pk2;

pk1.Initialize(prng, 1024);
ByteQueue queue;
pk1.Save(queue);

vector<byte> spki;
spki.resize(queue.MaxRetrievable());

ArraySink as1(&spki[0], spki.size());
queue.CopyTo(as1);

ArraySource as2(&spki[0], spki.size(), true);
pk2.Load(as2);

bool valid = pk2.Validate(prng, 3);
if(valid)
    cout << "Validated private key" << endl;
else
    cout << "Failed to validate private key" << endl;

 

We don't have an explicit VectorSink, and we can't easily create one because of an implicit expectation of traits_type::char_type. For example:

using CryptoPP::StringSinkTemplate;
typedef StringSinkTemplate< std::vector<byte> > VectorSink;

In file included from cryptopp-test.cpp:65:
In file included from /usr/local/include/cryptopp/files.h:5:
/usr/local/include/cryptopp/filters.h:590:22: error: no member named
      'traits_type' in 'std::vector<unsigned char, std::allocator<unsigned char>
      >'
        typedef typename T::traits_type::char_type char_type;
                         ~~~^
cryptopp-test.cpp:243:20: note: in instantiation of template class
      'CryptoPP::StringSinkTemplate<std::vector<unsigned char,
      std::allocator<unsigned char> > >' requested here
        VectorSink vs(spki);

  

 

http://c/questions/29050575/how-would-i-load-a-private-public-key-from-a-string-byte-array-or-any-other

posted on 2015-08-31 19:50  knifediy  阅读(2104)  评论(0编辑  收藏  举报