[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

相关文章

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是