C/C++ 使用 cryptopp 加密解密
2023-02-18 16:46:18 时间
CryptLib 是新西兰奥克兰大学的Peter Gutmann先生花费了将近五年时间开发而成的一个加密安全工具包,它基于传统的计算机安全模型,并涉及到一个安全核心,各种抽象化了的对象位于核心之上。CRYPTLIB利用此加密库不同层次的接口,可以很容易地为各种应用系统提供安全服务,如加/解密、数字签名、认证等。
Github地址:https://github.com/LYingSiMon/cryptopp
引入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 hex
std::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;
}
相关文章
- Linux系列(37) - 源码包与RPM包区别(1)
- Linux系列(36) - yum命令安装(3)
- Linux系列(35) - 光盘yum源搭建(2)
- Linux系列(34) - yum源文件(1)
- Linux系列(33)- rpm命令管理之RPM包校验提取(5)
- Linux系列(32) - rpm命令管理之RPM查询(4)
- 创建Linux虚拟机(CentOS 7)
- vSphere 6.5虚拟机模板的创建和使用(windows与linux)
- 无人值守批量部署Linux操作系统
- 使用Kali Linux虚拟机破解WiFi密码的一波三折及详细操作步骤
- linux 下统计一个文件夹下文件的个数
- C++ 基础篇之如何进行数据封装
- 论c++中的文件操作(竞赛必看)通俗易懂
- C++中小数点后几位的表达
- 论c++中的数组,vector和array的区别及用法
- C++中的const究竟有什么好处呢?(必须干货)
- DNS服务器搭建(Linux版本)
- algorithm中的排序算法详解
- 我的go+初体验——大一从c++到go+的快速上手
- 蓝桥杯2020c++B组省赛