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; }