C++实现AES加密

参考 https://zhuanlan.zhihu.com/p/567141666

本人开发环境:vs2022,平台工具集vs2015(v140)
要做到的如下图 image

需要下载Cryptopphttps://github.com/weidai11/cryptopp
打开默认工程,生成即可,选择cryptlib项目点击生成,产生头文件和lib文件

实现代码:

#include <Cryptopp/aes.h>
#include <Cryptopp/filters.h>
#include <Cryptopp/modes.h>
#include <Cryptopp/base64.h>

using namespace std;
using namespace CryptoPP;

string EncryptUtil::AES_encrypt(const char* key, string& content)
{
    string cipher;
    try {
        ECB_Mode< AES >::Encryption	ecb_encription((const unsigned char*)key, 16);
        StringSource(content, true,
            new StreamTransformationFilter(ecb_encription,
                new Base64Encoder(new StringSink(cipher)),
                BlockPaddingSchemeDef::PKCS_PADDING));
    }
    catch (const Exception& e) {
        cout << e.what() << endl;
    }
    return cipher;
}

posted @ 2023-04-04 18:35  SpringBreath  阅读(327)  评论(0编辑  收藏  举报