Crypto++ AES 加密解密流程
版本号
Crypto++ 8.5
编译工具
msvc 2017 64bit
编译注意事项
生成动态库时,需要注意base64加解密源文件加入以及类的导出标志
使用注意事项
使用时,需要加入宏定义 CRYPTOPP_IMPORTS,否则链接错误
类封装参考
h文件
#ifndef XCRYPTO_H #define XCRYPTO_H #include <QObject> typedef std::string strbase64; class XCrypto { public: XCrypto(); virtual~ XCrypto(); //in:string out:base64 string strbase64 encrypt(std::string plain); std::string decrypt(strbase64 str); private: unsigned char ucKey[16]; unsigned char ucIV[16]; }; #endif // XCRYPTO_H
cpp文件
#include "xcrypto.h" #include <aes.h> #include <filters.h> #include <modes.h> #include <base64.h> using namespace CryptoPP; XCrypto::XCrypto() { memcpy(ucKey, "1234567890123456", 16); memcpy(ucIV, "1234567890123456", 16); } XCrypto::~XCrypto() { } strbase64 XCrypto::encrypt(std::string plain) { CBC_Mode< AES >::Encryption e; e.SetKeyWithIV(ucKey, sizeof(ucKey), ucIV); StreamTransformationFilter stf(e, nullptr); for(int i = 0; i < plain.size(); i++){ stf.Put((byte)plain[i]); } stf.MessageEnd(); size_t ready = stf.MaxRetrievable(); std::string cipher(ready, 0); stf.Get((byte*)&cipher[0], ready); Base64Encoder b64e; b64e.Put((byte*)cipher.c_str(), cipher.size()); b64e.MessageEnd(); ready = b64e.MaxRetrievable(); strbase64 res(ready, 0); b64e.Get((byte*)&res[0], ready); return res; } std::string XCrypto::decrypt(strbase64 str) { Base64Decoder b64d; b64d.Put((byte*)str.c_str(), str.size()); b64d.MessageEnd(); size_t ready = b64d.MaxRetrievable(); std::string sby(ready, 0); b64d.Get((byte*)&sby[0], ready); CBC_Mode<AES>::Decryption d; d.SetKeyWithIV(ucKey, sizeof(ucKey), ucIV); StreamTransformationFilter stfd(d, nullptr); for(int i = 0; i < sby.size(); i++){ stfd.Put((byte)sby[i]); } stfd.MessageEnd(); ready = stfd.MaxRetrievable(); std::string res(ready, 0); stfd.Get((byte*)&res[0], ready); return res; }
#include "xcrypto.h" #include <aes.h> #include <filters.h> #include <modes.h> #include <base64.h> using namespace CryptoPP; XCrypto::XCrypto() { memcpy(ucKey, "1234567890123456", 16); memcpy(ucIV, "1234567890123456", 16); } XCrypto::~XCrypto() { } strbase64 XCrypto::encrypt(std::string plain) { CBC_Mode< AES >::Encryption e; e.SetKeyWithIV(ucKey, sizeof(ucKey), ucIV); StreamTransformationFilter stf(e, nullptr); for(int i = 0; i < plain.size(); i++){ stf.Put((byte)plain[i]); } stf.MessageEnd(); size_t ready = stf.MaxRetrievable(); std::string cipher(ready, 0); stf.Get((byte*)&cipher[0], ready); Base64Encoder b64e; b64e.Put((byte*)cipher.c_str(), cipher.size()); b64e.MessageEnd(); ready = b64e.MaxRetrievable(); strbase64 res(ready, 0); b64e.Get((byte*)&res[0], ready); return res; } std::string XCrypto::decrypt(strbase64 str) { Base64Decoder b64d; b64d.Put((byte*)str.c_str(), str.size()); b64d.MessageEnd(); size_t ready = b64d.MaxRetrievable(); std::string sby(ready, 0); b64d.Get((byte*)&sby[0], ready); CBC_Mode<AES>::Decryption d; d.SetKeyWithIV(ucKey, sizeof(ucKey), ucIV); StreamTransformationFilter stfd(d, nullptr); for(int i = 0; i < sby.size(); i++){ stfd.Put((byte)sby[i]); } stfd.MessageEnd(); ready = stfd.MaxRetrievable(); std::string res(ready, 0); stfd.Get((byte*)&res[0], ready); return res; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战