[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

相关文章

Python数据验证神器Pydantic库的使用和实践中的避坑指南

《Python数据验证神器Pydantic库的使用和实践中的避坑指南》Pydantic是一个用于数据验证和设置的库,可以显著简化API接口开发,文章通过一个实际案例,展示了Pydantic如何在生产环... 目录1️⃣ 崩溃时刻:当你的API接口又双叒崩了!2️⃣ 神兵天降:3行代码解决验证难题3️⃣ 深度

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco