[C++] 使用 cryptopp 加密解密

2023-12-17 02:48

本文主要是介绍[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 \n", en_base64.c_str());std::string de_base64 = aes_decrypt_ecb_base64(en_base64, (unsigned char*)"1234567812345678", 16);printf("de:%s \n", de_base64.c_str());// ebc hexstd::string en_hex = aes_encrypt_ecb_hex("hello cryptopp", (unsigned char*)"1234567812345678", 16);printf("en:%s \n", en_hex.c_str());std::string de_hex = aes_decrypt_ecb_hex(en_hex, (unsigned char*)"1234567812345678", 16);printf("de:%s \n", de_hex.c_str());(void)getchar();return 0;
}

这篇关于[C++] 使用 cryptopp 加密解密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/502823

相关文章

go rate 原生标准限速库的使用

《gorate原生标准限速库的使用》本文主要介绍了Go标准库golang.org/x/time/rate实现限流,采用令牌桶算法控制请求速率,提供Allow/Reserve/Wait方法,具有一定... 目录介绍安装API介绍rate.NewLimiter:创建限流器limiter.Allow():请求是否

Python使用Turtle实现精确计时工具

《Python使用Turtle实现精确计时工具》这篇文章主要为大家详细介绍了Python如何使用Turtle实现精确计时工具,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录功能特点使用方法程序架构设计代码详解窗口和画笔创建时间和状态显示更新计时器控制逻辑计时器重置功能事件

Swagger2与Springdoc集成与使用详解

《Swagger2与Springdoc集成与使用详解》:本文主要介绍Swagger2与Springdoc集成与使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1. 依赖配置2. 基础配置2.1 启用 Springdoc2.2 自定义 OpenAPI 信息3.

C++ HTTP框架推荐(特点及优势)

《C++HTTP框架推荐(特点及优势)》:本文主要介绍C++HTTP框架推荐的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Crow2. Drogon3. Pistache4. cpp-httplib5. Beast (Boos

Golang interface{}的具体使用

《Golanginterface{}的具体使用》interface{}是Go中可以表示任意类型的空接口,本文主要介绍了Golanginterface{}的具体使用,具有一定的参考价值,感兴趣的可以了... 目录一、什么是 interface{}?定义形China编程式:二、interface{} 有什么特别的?✅

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现

windows和Linux安装Jmeter与简单使用方式

《windows和Linux安装Jmeter与简单使用方式》:本文主要介绍windows和Linux安装Jmeter与简单使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows和linux安装Jmeter与简单使用一、下载安装包二、JDK安装1.windows设

Spring 缓存在项目中的使用详解

《Spring缓存在项目中的使用详解》Spring缓存机制,Cache接口为缓存的组件规范定义,包扩缓存的各种操作(添加缓存、删除缓存、修改缓存等),本文给大家介绍Spring缓存在项目中的使用... 目录1.Spring 缓存机制介绍2.Spring 缓存用到的概念Ⅰ.两个接口Ⅱ.三个注解(方法层次)Ⅲ.

PyTorch中cdist和sum函数使用示例详解

《PyTorch中cdist和sum函数使用示例详解》torch.cdist是PyTorch中用于计算**两个张量之间的成对距离(pairwisedistance)**的函数,常用于点云处理、图神经网... 目录基本语法输出示例1. 简单的 2D 欧几里得距离2. 批量形式(3D Tensor)3. 使用不

C#使用MQTTnet实现服务端与客户端的通讯的示例

《C#使用MQTTnet实现服务端与客户端的通讯的示例》本文主要介绍了C#使用MQTTnet实现服务端与客户端的通讯的示例,包括协议特性、连接管理、QoS机制和安全策略,具有一定的参考价值,感兴趣的可... 目录一、MQTT 协议简介二、MQTT 协议核心特性三、MQTTNET 库的核心功能四、服务端(BR