zoukankan      html  css  js  c++  java
  • C/C++ 使用 cryptopp 加密解密

    官网:https://www.cryptopp.com/docs/ref/index.html

    github:https://github.com/LYingSiMon/cryptopp

    文档:https://www.cryptopp.com/docs/ref/

    环境搭建
    引入 cryptlib.lib , 以及所有项目中的头文件

    AES 加密测试(ECB 模式为例)

    #include <iostream>
     
    #include "cryptlib.h"
    #include "rijndael.h"
    #include "modes.h"
    #include "files.h"
    #include "osrng.h"
    #include "hex.h"
    #include "base64.h"
     
    using namespace CryptoPP;
     
    // aes ebc 加密(输出 base64)
    std::string aes_encrypt_ecb_base64(std::string data , unsigned char* key, int keylen)
    {
        std::string encrypt_str;
     
        try 
        {
            CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption ecb_encription(key, keylen);
            CryptoPP::StreamTransformationFilter stf_encription(
                ecb_encription,
                new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encrypt_str)),
                CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
            );
            stf_encription.Put(reinterpret_cast<const unsigned char*>(data.c_str()), data.length() + 1);
            stf_encription.MessageEnd();
        }
        catch (std::exception e) {
            std::cout << e.what() << std::endl;
        }
     
        return encrypt_str;
    }
     
    // aes ebc 加密(输出 hex) 
    std::string aes_encrypt_ecb_hex(std::string data , unsigned char* key, int keylen)
    {
        std::string encrypt_str;
     
        try 
        {
            CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption ecb_encription(key, keylen);
            CryptoPP::StreamTransformationFilter stf_encription(
                ecb_encription,
                new CryptoPP::HexEncoder(new CryptoPP::StringSink(encrypt_str)),
                CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
            );
            stf_encription.Put(reinterpret_cast<const unsigned char*>(data.c_str()), data.length() + 1);
            stf_encription.MessageEnd();
        }
        catch (std::exception e) {
            std::cout << e.what() << std::endl;
        }
     
        return encrypt_str;
    }
     
    // aes ebc 解密(输出 base64)
    std::string aes_decrypt_ecb_base64(std::string base64_data, unsigned char* key, int keylen)
    {
        try 
        {
            std::string aes_encrypt_data;
            CryptoPP::Base64Decoder decoder;
            decoder.Attach(new CryptoPP::StringSink(aes_encrypt_data));
            decoder.Put(reinterpret_cast<const unsigned char*>(base64_data.c_str()), base64_data.length());
            decoder.MessageEnd();
     
            std::string decrypt_data;
            CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption ebc_description(key, keylen);
            CryptoPP::StreamTransformationFilter stf_description(
                ebc_description,
                new CryptoPP::StringSink(decrypt_data), 
                CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
            );
     
            stf_description.Put(
                reinterpret_cast<const unsigned char*>(aes_encrypt_data.c_str()), 
                aes_encrypt_data.length()
            );
            stf_description.MessageEnd();
     
            return decrypt_data;
        }
        catch (std::exception e) {
            std::cout << e.what() << std::endl;
            return "";
        }
    }
     
    // aes ebc 解密(输出 hex)
    std::string aes_decrypt_ecb_hex(std::string hex_data, unsigned char* key, int keylen)
    {
        try
        {
            std::string aes_encrypt_data;
            CryptoPP::HexDecoder decoder;
            decoder.Attach(new CryptoPP::StringSink(aes_encrypt_data));
            decoder.Put(reinterpret_cast<const unsigned char*>(hex_data.c_str()), hex_data.length());
            decoder.MessageEnd();
     
            std::string decrypt_data;
            CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption ebc_description(key, keylen);
            CryptoPP::StreamTransformationFilter stf_description(
                ebc_description,
                new CryptoPP::StringSink(decrypt_data),
                CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
            );
     
            stf_description.Put(
                reinterpret_cast<const unsigned char*>(aes_encrypt_data.c_str()),
                aes_encrypt_data.length()
            );
            stf_description.MessageEnd();
     
            return decrypt_data;
        }
        catch (std::exception e) {
            std::cout << e.what() << std::endl;
            return "";
        }
    }
     
     
    int main()
    {
        // ebc base64 
        std::string en_base64 = aes_encrypt_ecb_base64("hello cryptopp",(unsigned char*)"1234567812345678", 16);
        printf("en:%s 
    ", en_base64.c_str());
        std::string de_base64 = aes_decrypt_ecb_base64(en_base64, (unsigned char*)"1234567812345678", 16);
        printf("de:%s 
    ", de_base64.c_str());
     
        // ebc hex
        std::string en_hex = aes_encrypt_ecb_hex("hello cryptopp", (unsigned char*)"1234567812345678", 16);
        printf("en:%s 
    ", en_hex.c_str());
        std::string de_hex = aes_decrypt_ecb_hex(en_hex, (unsigned char*)"1234567812345678", 16);
        printf("de:%s 
    ", de_hex.c_str());
        
     
     
        (void)getchar();
        return 0;
    }
    

    版权声明: 本博客,文章与代码均为学习时整理的笔记,博客中除去明确标注有参考文献的文章,其他文章【均为原创】作品,转载请务必【添加出处】,您添加出处是我创作的动力!

    警告:如果您恶意转载本人文章,则您的整站文章,将会变为我的原创作品,请相互尊重!
  • 相关阅读:
    Java--环境变量(jdk、jre)配置
    Jsoup爬取数据设置代理IP
    数据库连接的莫名问题:create connection error
    springBoot使用过程的小感悟
    Activiti工作流引擎数据库表结构
    IDEA创建Activiti工作流开发
    Java开源爬虫框架WebCollector设置代理
    使用OkHttpClient爬取json数据
    雪中的杭州
    StringBuffer中的append方法
  • 原文地址:https://www.cnblogs.com/LyShark/p/15086644.html
Copyright © 2011-2022 走看看