码迷,mamicode.com
首页 > 其他好文 > 详细

Crypto++ RSA从字符串读取公私匙

时间:2015-08-31 21:07:27      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

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

Crypto++ RSA从字符串读取公私匙

标签:

原文地址:http://www.cnblogs.com/knifediy/p/4773944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!