欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用Poco库进行加解密和签名验签

程序员文章站 2022-04-11 17:48:18
...

最近一个朋友让我帮忙找一下使用Poco库进行非对称加密的例子,通过百度,Google找到两个不错的在此总结一下:

Poco开源代码库地址:https://github.com/pocoproject/poco

在Crypto/testsuit/src/RSATest.h和RSATest..cpp有示例代码可以参考。

下面是摘自另一个网站上的部分代码,图文并茂,感觉讲得很好,拿来分享给大家。

非对称加解密过程如图:

使用Poco库进行加解密和签名验签

代码如下:

#include "Poco/Crypto/RSAKey.h"
#include "Poco/Crypto/RSADigestEngine.h“

auto& factory = CipherFactory::defaultFactory();
string transmission;

{
  Cipher* alice = factory.createCipher(RSAKey( "public_bob.pem"));
  transmission = alice->encryptString(
    "Tall tree, Spy-glass shoulder, ... Skeleton Island ESE and by E");
}

{
  Cipher* bob = factory.createCipher(RSAKey( "", "private_bob.pem", "bobbob"));
  string decrypted = bob->decryptString(transmission);
  assert(decrypted == "Tall tree, Spy-glass shoulder, ... Skeleton Island ESE and by E");
}

签名&验签过程如图:

使用Poco库进行加解密和签名验签

代码如下:

// Poco::DigestEngine::Digest is std::vector 
tuple transmission;

{
  string contract = 
    "Alice will give Bob $100 and Bob will give Alice two magic mushrooms";
  RSADigestEngine bob(RSAKey("", "private_bob.pem", "bobbob"));
  bob.update(contract);
  transmission = make_tuple(contract, bob.signature());
}

{
  RSADigestEngine alice(RSAKey("public_bob.pem"));
  alice.update(get<0>(transmission));
  bool isValid = alice.verify(get<1>(transmission));
  assert(isValid == true);
}

 

 
相关标签: C++ 区块链