zoukankan      html  css  js  c++  java
  • 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     }
    复制代码

    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
    };

    复制代码
     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     }
    复制代码

    1、第6行,为原文;

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

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

     
  • 相关阅读:
    洛谷P1120信息奥赛一本通1442 小木棍
    洛谷P1378 油滴扩展
    洛谷P1156 垃圾陷阱
    mybatis-Plus 实践篇之逆向工程
    Interceptor的使用及探究
    mysql,oracle,sqlServer 元数据查询
    navicat premium15免费版安装说明(附工具)
    打印日志你真的会吗?
    线程基础知识-必知必会
    空间复杂度&时间复杂度
  • 原文地址:https://www.cnblogs.com/buffercache/p/10100758.html
Copyright © 2011-2022 走看看