Poco::Crypto--加解密(AES)

时间:2020-11-28 21:02:42
Poco::Crypto--加解密(AES)
 1     int main(const std::vector<std::string>& args)
2 {
3 /*TO DO*/
4 Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(CipherKey("aes256"));
5
6 std::string in("I love karen!");
7 std::string out = pCipher->encryptString(in, Cipher::ENC_BASE64);
8 std::string result = pCipher->decryptString(out, Cipher::ENC_BASE64);
9 std::cout<<"加密后:"<<out<<std::endl;
10 std::cout<<"解密后:"<<result<<std::endl;
11
12 return Application::EXIT_OK;
13 }
Poco::Crypto--加解密(AES)

1、第4行代码,创建一个256位的AES加密算法;

注:http://zh.wikipedia.org/wiki/%E9%AB%98%E7%BA%A7%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86  AES*

2、第7、8行代码,分别实现加密与解密过程。

3、在加密与解密过程中,代码中使用了BASE64编码方式,另外在POCO::Crypto中还有以下编码方式:

enum Encoding
/// Transport encoding to use for encryptString() and decryptString().
{
ENC_NONE = 0x00, /// Plain binary output
ENC_BASE64 = 0x01, /// Base64-encoded output
ENC_BINHEX = 0x02, /// BinHex-encoded output
ENC_BASE64_NO_LF = 0x81, /// Base64-encoded output, no linefeeds
ENC_BINHEX_NO_LF = 0x82, /// BinHex-encoded output, no linefeeds
};

Poco::Crypto--加解密(AES)
 1     int main(const std::vector<std::string>& args)
2 {
3 /*TO DO*/
4 Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(CipherKey("aes256"));
5
6 static const std::string SECRET_MESSAGE = "This is a secret message. Don't tell anyone.";
7
8 std::stringstream sstr;
9 EncryptingOutputStream encryptor(sstr, *pCipher);
10 encryptor << SECRET_MESSAGE;
11 encryptor.close();
12
13 DecryptingInputStream decryptor(sstr, *pCipher);
14 std::string iresult;
15 Poco::StreamCopier::copyToString(decryptor, iresult);
16
17 std::cout<<"解密后:"<<iresult<<std::endl;
18 return Application::EXIT_OK;
19 }
Poco::Crypto--加解密(AES)

1、第6行,为原文;

2、第9行,加密;第13行,解密;

3、第15行,得到解密结果,就是原文了。

 
标签: Crypto加解密