QT C/C++ 使用 CryptoPP加密解密

1、介绍


        CryptoPP 是一个开源的密码学加密库,提供了大量的密码学算法和功能,包括对称加密、非对称加密、哈希函数、消息认证码 (MAC)、数字签名等。这里重点介绍一下对称加密算法(AES)的使用和注意事项。

2、AES特点


(1)加密和解密的密钥相同。
(2)分组密码:AES将明文数据分成固定大小的块,然后对这个独立的块进行加密,这个固定大小的块称为分组。在AES标准规范中,分组长度只能是128位。
(3)轮数:AES加密算法的安全性和其轮数相关,轮数表示对数据块的处理循环次数。不同密钥长度的AES使用不同数量的轮数。通常128比特密钥使用10轮,192比特密钥使用12轮,256比特密钥使用14轮。
(4)密钥长度:AES支持多种密钥长度,包括128比特、192比特和256比特。密钥长度的选择直接影响加密算法的安全性。
(5)强安全性:AES被广泛认为是一种安全、可靠的加密算法。它经过广泛的密码分析和评估,并且在许多应用中得到了广泛的应用,包括加密通信、文件加密和硬件加密。

3、加密模式

AES的加密模式有以下几种

  • 电码本模式(ECB)
  • 密码分组链接模式(CBC)
  • 计算器模式(CTR)
  • 密码反馈模式(CFB)
  • 输出反馈模式(OFB)

4种分别是:CBC、CFB、OFB、ECB;
3种需要向量参数:CBC、CFB、OFB模式;
1种不需要向量参数:ECB模式;
ECB模式是最基本的加密模式,最容易被破解,CBC、CFB、OFB模式的加密过程添加向量后会更加安全。ECB和CBC两种模式,其输出密文长度永远都是分块大小(128bits)的整数倍,而其他模式都是和输入明文等长。

4、加解密步骤


1、加密

(1)数据分块:将明文分成固定大小的数据块(128位)。
(2)初始轮密钥加密:将明文和初始密钥进行一次简单的混淆操作。
(3)轮加密:通过多轮的替代和置换操作(SubBytes、ShiftRows、MixColumns、AddRoundKey),对数据块进行混淆。
(4)最终轮:在最后一轮中,省略MixColumns操作。
(5)得到密文。

2、解密

(1)初始轮密钥解密:将密文和初始密钥进行一次简单的混淆操作。
(2)轮解密:通过多轮的逆操作(InvSubBytes、InvShiftRows、InvMixColumns、AddRoundKey),对数据块进行逆操作。
(3)最终轮:在最后一轮中,省略InvMixColumns操作。
(4)得到明文。

5、一些参数配置

填充模式

如电子密码本(ECB)和密文块链接(CBC)。 为对称密钥加密设计的块密码工作模式要求输入明文长度必须是块长度的整数倍,因此信息必须填充至满足要求。

常见填充模式

偏移量


偏移量 的添加一般是为了增加 AES 加密的复杂度,增加数据的安全性。一般在 AES_256 中会使用到 偏移量 ,而在 AES_128 加密中不会使用到。

字符集


在 AES 加密中,特别也要注意到字符集的问题。一般用到的字符集是 utf-8 和 gbk 。

实际工作中的加密流程


在实际的工作中,客户端跟服务器交互一般都是字符串格式,所以一个比较好的加密流程是:

加密流程 :明文通过 密钥 (有时也需要 偏移量 ),利用 AES 加密算法,然后通过 Base64 转码,最后生成加密后的字符串。
解密流程 :加密后的字符串通过 密钥 (有时也需要 偏移量 ),利用 AES 解密算法,然后通过 Base64 转码,最后生成解密后的字符串。


AES 加密/解密 注意的问题


AES 加密/解密的时候,通常是用在服务端和客户端通讯的过程中,一端加密传输,另一端解密使用。虽然 AES 加密看似简单,但在使用过程过程中,仍然会出现在一端加密ok,但是另一端解密失败的情况。一旦出现 AES 解密失败,我们可以通过以下几个方面进行排查:

1. AES 加密/解密 使用相同的密钥
2. 若涉及到偏移量,则AES 加密/解密 使用的偏移量要一样
3. AES 加密/解密 要使用相同加密数位,如都使用`AES_256`
4. AES 加密/解密 使用相同的字符集
5. AES 加密/解密 使用相同的加密,填充模式,如都使用`AES/CBC/PKCS5Padding`模式
6. 由于不同开发语言(如C 和 Java)及不同开发环境(如 Java 和 Android)的影响,可能相同的加解密算法在实现上出现差异,若你们注意到这个差异,就可能导致加解密出现问题

最后,当我们需要验证自己的 AES 解密算法是否与别人的加密方法为一套的时候。可以让加密方发你一份加密后的密文和加密前的明文,然后你用密文解密,看解密结果和加密方发你的是否一致。需要注意的是,加密方给你的明文要尽量简洁,如就 中国 二字,这样既能看出加密方和解密方的字符集是否一致,而且能避免复制粘贴等环节出现空格,回车等转义字符对验证结果的干扰。

6、线上加密网站

线上加密网站:在线AES加密解密、AES在线加密解密、AES encryption and decryption--查错网

对比网站与代码的加密结果。

7、下载Cryptopp


Crypto++下载地址
官网地址:Crypto++ Library 8.9 | Free C++ Class Library of Cryptographic Schemes
github地址(8.7.0版本):https://github.com/weidai11/cryptopp/releases/tag/CRYPTOPP_8_7_0
有四个选项,请下载源码(如图所示)

8、在VS2022中编译Cryptopp库

1、打开下载并解压好的crypto++文件

2、在VS2022中可以看到四个子工程(如图所示):

  • cryptdll - 生成cryptopp.dll动态库
  • dlltest - 用来测试cryptopp.dll,依赖cryptdll工程
  • cryptlib - 生成cryptlib.lib静态库
  • cryptest - 用来测试cryptopp,依赖cryptlib工程

3、build子工程cryptlib,生成lib文件


分别在Release和debug两个情况下,调整设备型号(本机是x64),右击子工程cryptlib点击“生成”。输出显示成功后即为完成。

此时,crypto++源文件夹下会多出一个名称为x64的文件夹,该文件夹下有三个子文件夹,其中一个名为Output,Output文件夹下有两个子文件夹,如图所示。Debug和Release文件夹下都有lib文件,这两个lib文件就是我们后面要使用的lib库。

4、处理crypto++文件:


新建文件夹,命名为cryptopp(或其他名称),文件夹下创建两个文件夹,分别命名为include,lib。

将crypto++源文件中所有头文件(.h结尾的文件)复制到新文件夹下的include文件夹下。

将上述Output文件夹复制粘贴到新建文件夹下的lib文件中。

至此,我们需要用到crypto++库整理完成。简而言之,从官网上下载下来的整个crypto++源码,我们只需要用到include和lib两个库文件。

5、在VS2022项目中使用crypto++库

1、打开VS2022,文件->新建->项目->控制台应用

2、将新创建的项目中原本的hello world代码换成以下代码(该代码为crypto++官网给出的AES加密代码)作为测试代码:

#include "cryptlib.h"
#include "rijndael.h"
#include "modes.h"
#include "files.h"
#include "osrng.h"
#include "hex.h"

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
    using namespace CryptoPP;

    AutoSeededRandomPool prng;
    HexEncoder encoder(new FileSink(std::cout));

    SecByteBlock key(AES::DEFAULT_KEYLENGTH);
    SecByteBlock iv(AES::BLOCKSIZE);

    prng.GenerateBlock(key, key.size());
    prng.GenerateBlock(iv, iv.size());

    std::string plain = "CBC Mode Test:Hello!";
    std::string cipher, recovered;

    std::cout << "plain text: " << plain << std::endl;

    /*********************************\
    \*********************************/

    try
    {
        CBC_Mode< AES >::Encryption e;
        e.SetKeyWithIV(key, key.size(), iv);

        StringSource s(plain, true,
            new StreamTransformationFilter(e,
                new StringSink(cipher)
            ) // StreamTransformationFilter
        ); // StringSource
    }
    catch (const Exception& e)
    {
        std::cerr << e.what() << std::endl;
        exit(1);
    }

    /*********************************\
    \*********************************/

    std::cout << "key: ";
    encoder.Put(key, key.size());
    encoder.MessageEnd();
    std::cout << std::endl;

    std::cout << "iv: ";
    encoder.Put(iv, iv.size());
    encoder.MessageEnd();
    std::cout << std::endl;

    std::cout << "cipher text: ";
    encoder.Put((const byte*)&cipher[0], cipher.size());
    encoder.MessageEnd();
    std::cout << std::endl;

    /*********************************\
    \*********************************/

    try
    {
        CBC_Mode< AES >::Decryption d;
        d.SetKeyWithIV(key, key.size(), iv);

        StringSource s(cipher, true,
            new StreamTransformationFilter(d,
                new StringSink(recovered)
            ) // StreamTransformationFilter
        ); // StringSource

        std::cout << "recovered text: " << recovered << std::endl;
    }
    catch (const Exception& e)
    {
        std::cerr << e.what() << std::endl;
        exit(1);
    }

    return 0;
}

3、项目->属性


跟着图片进行修改选项:
a. 此处修改为我们新建的文件夹下的include文件路径。

b.留心你现在的模式是Release还是Debug。Release对应的运行库是/MT,Debug对应的是/MTd

c 此处修改为我们新建的文件夹下的lib文件下路径。查看自己的配置,Debug对应的是Debug文件下的路径;Release对应的是Release文件下的路径。

d. 选择“链接器”中的“输入”,在“附加依赖项”添加“cryptlib.lib”

运行测试代码,出现结果图则为crypto++配置成功。

9、Qt 5.15集成Crypto++ 8.8.0(MSVC 2019)

一、背景


  笔者已介绍过在Qt 5.15.x中使用MinGW(8.10版本)编译并集成Crypto++ 8.8.0。
但是该编译出来的库(.a和.dll)不适用MSVC(2019版本)构建环境,需要重新编译(.lib或和.dll)。

二、思路 & 尝试


  首先想到的是,在Qt MSVC环境下(VS Community 2019 版本 16.11.24)编译Crypto++ 8.8.0源代码,以得到相关库。

结果是大碰壁:

gcm.obj : error LNK2019: 无法解析的外部符号 GCM_AuthenticateBlocks_2K_SSE2,函数 "protected: virtual unsigned __int64 __cdecl CryptoPP::GCM_Base::AuthenticateBlocks(unsigned char const *,unsigned __int64)" (?AuthenticateBlocks@GCM_Base@CryptoPP@@MEAA_KPEBE_K@Z) 中引用了该符号
gcm.obj : error LNK2019: 无法解析的外部符号 GCM_AuthenticateBlocks_64K_SSE2,函数 "protected: virtual unsigned __int64 __cdecl CryptoPP::GCM_Base::AuthenticateBlocks(unsigned char const *,unsigned __int64)" (?AuthenticateBlocks@GCM_Base@CryptoPP@@MEAA_KPEBE_K@Z) 中引用了该符号
integer.obj : error LNK2019: 无法解析的外部符号 Baseline_Add,函数 "class CryptoPP::Integer __cdecl CryptoPP::StringToInteger<char>(char const *,enum CryptoPP::ByteOrder)" (??$StringToInteger@D@CryptoPP@@YA?AVInteger@0@PEBDW4ByteOrder@0@@Z) 中引用了该符号
integer.obj : error LNK2019: 无法解析的外部符号 Baseline_Sub,函数 "public: virtual class CryptoPP::Integer & __cdecl CryptoPP::ModularArithmetic::Accumulate(class CryptoPP::Integer &,class CryptoPP::Integer const &)const " (?Accumulate@ModularArithmetic@CryptoPP@@UEBAAEAVInteger@2@AEAV32@AEBV32@@Z) 中引用了该符号
...

提示找不到目标函数,搜索代码发现,这些函数都存在于汇编代码块中(.asm),在C++代码中声明。
  接着捣鼓,即便放开了定义,允许进入汇编代码,但编译还是无法pass:MSVC的工具链无法编译汇编代码。

xx\gcm.cpp:579: error: C4235: 使用了非标准扩展: 不支持在此结构上使用“__asm”关键字
xx\gcm.cpp:579: error: C2065: “mov”: 未声明的标识符
xx\gcm.cpp:579: error: C2146: 语法错误: 缺少“;”(在标识符“rcx”的前面)
xx\gcm.cpp:579: error: C2065: “rcx”: 未声明的标识符
xx\gcm.cpp:579: error: C2143: 语法错误: 缺少“;”(在“}”的前面)
xx\gcm.cpp:580: error: C4235: 使用了非标准扩展: 不支持在此结构上使用“__asm”关键字

看来,直接使用Qt MSVC构建的路径行不通了(至少不容易)。那就只能走用VS构建路了。
  出乎意料,使用Visual Studio 2019非常顺利地编译出静态和动态库。不过很快发现一个问题:动态库文件太小(1,646 kB),调查发现工程(cyrdll)中所包含的源文件也不完整。。(郁闷了),倒是静态库(cryptlib)看似比较靠谱。
  那就在Qt中使用静态库吧。。引用方式和动态库一样。demo工程文件(.pro)引用cryptlib库部分如下:
 

INCLUDEPATH += $$PWD/cryptlib

LIBS += -L$$PWD -lcryptlib

结果编译依然报错,但是错得没有那么离谱了:

cryptlib.lib(cryptlib.obj):-1: error: LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MT_StaticRelease”不匹配值“MD_DynamicRelease”(fscryptoutil.obj 中)
cryptlib.lib(filters.obj):-1: error: LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MT_StaticRelease”不匹配值“MD_DynamicRelease”(fscryptoutil.obj 中)

经搜索,定位该问题为运行时库的设置问题:Visual Studio默认的是MT(多线程),而Qt MSVC要的是MD(多线程DLL)。
  以下是VS IDE中,cryptlib工程有关运行库的设置页面:

在VS IDE中按照调整后的运行库选项,重新编译。集成OK。

三、集成

  Qt Demo工程环境:qmake + MSVC 2019 + Release + x64。如图:

其中cryptlib.lib即为Crypto++ 8.8.0静态库。fslib.dll则是动态库(同样使用MSVC编译)。
  相关的引用和集成在工程文件(.pro)中体现如下:

INCLUDEPATH += $$PWD/cryptlib
INCLUDEPATH += $$PWD/fslib

LIBS += -L$$PWD -lcryptlib
LIBS += -lfslib

四、结论


(1)Crypto++源代码编译ok的两种环境:

Qt qmake (MinGW)可以编译出动态库,参见:Qt 5.15编译(MinGW)及集成Crypto++ 8.8.0笔记_qt crypto-CSDN博客


Visual Studio 2019 可以编译出静态库(解决方案中的cryptlib工程),但需要调整运行库类型从MT→MD。


(2)Qt的集成,库使用什么环境构建,则可执行模块亦使用对应的构建环境:

Qt qmake (MinGW Release x64) → qmake (MinGW Release x64)
VS 2019 Release x64 → qmake (MSVC 2019 Release x64)

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

例子二:

// g++ -g3 -ggdb -O0 -DDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread
// g++ -g -O2 -DNDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread

#include "osrng.h"
using CryptoPP::AutoSeededRandomPool;

#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

#include <string>
using std::string;

#include <cstdlib>
using std::exit;

#include "cryptlib.h"
using CryptoPP::Exception;

#include "hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;

#include "filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::StreamTransformationFilter;

#include "aes.h"
using CryptoPP::AES;

#include "modes.h"
using CryptoPP::ECB_Mode;

int main(int argc, char* argv[])
{
	AutoSeededRandomPool prng;

	byte key[AES::DEFAULT_KEYLENGTH];
	prng.GenerateBlock(key, sizeof(key));

	string plain = "ECB Mode Test";
	string cipher, encoded, recovered;

	/*********************************\
	\*********************************/

	// Pretty print key
	encoded.clear();
	StringSource(key, sizeof(key), true,
		new HexEncoder(
			new StringSink(encoded)
		) // HexEncoder
	); // StringSource
	cout << "key: " << encoded << endl;

	/*********************************\
	\*********************************/

	try
	{
		cout << "plain text: " << plain << endl;

		ECB_Mode< AES >::Encryption e;
		e.SetKey(key, sizeof(key));

		// The StreamTransformationFilter adds padding
		//  as required. ECB and CBC Mode must be padded
		//  to the block size of the cipher.
		StringSource(plain, true, 
			new StreamTransformationFilter(e,
				new StringSink(cipher)
			) // StreamTransformationFilter      
		); // StringSource
	}
	catch(const CryptoPP::Exception& e)
	{
		cerr << e.what() << endl;
		exit(1);
	}

	/*********************************\
	\*********************************/

	// Pretty print
	encoded.clear();
	StringSource(cipher, true,
		new HexEncoder(
			new StringSink(encoded)
		) // HexEncoder
	); // StringSource
	cout << "cipher text: " << encoded << endl;

	/*********************************\
	\*********************************/

	try
	{
		ECB_Mode< AES >::Decryption d;
		d.SetKey(key, sizeof(key));

		// The StreamTransformationFilter removes
		//  padding as required.
		StringSource s(cipher, true, 
			new StreamTransformationFilter(d,
				new StringSink(recovered)
			) // StreamTransformationFilter
		); // StringSource

		cout << "recovered text: " << recovered << endl;
	}
	catch(const CryptoPP::Exception& e)
	{
		cerr << e.what() << endl;
		exit(1);
	}

	/*********************************\
	\*********************************/

	return 0;
}

  • 16
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个使用Crypto++库实现AES加密和解密的示例代码: ```c++ #include <iostream> #include <iomanip> #include <string> #include <cryptlib.h> #include <aes.h> #include <modes.h> #include <base64.h> using namespace CryptoPP; std::string encryptAES(const std::string& plainText, const std::string& key) { std::string cipherText; // 将密钥转换为byte数组 byte aesKey[AES::DEFAULT_KEYLENGTH]; ::memcpy(aesKey, key.c_str(), key.size()); // 选择ECB加密模式 ECB_Mode<AES>::Encryption encryption(aesKey, sizeof(aesKey)); // 加密 StringSource(plainText, true, new StreamTransformationFilter(encryption, new StringSink(cipherText) ) ); // 返回Base64编码后的密文 return base64_encode(reinterpret_cast<const unsigned char*>(cipherText.data()), cipherText.size()); } std::string decryptAES(const std::string& cipherText, const std::string& key) { std::string plainText; // 将密钥转换为byte数组 byte aesKey[AES::DEFAULT_KEYLENGTH]; ::memcpy(aesKey, key.c_str(), key.size()); // 选择ECB加密模式 ECB_Mode<AES>::Decryption decryption(aesKey, sizeof(aesKey)); // 解密 StringSource(base64_decode(cipherText), true, new StreamTransformationFilter(decryption, new StringSink(plainText) ) ); // 返回明文 return plainText; } int main() { // 待加密的明文和密钥 std::string plainText = "Hello, world!"; std::string key = "1234567890123456"; // 加密 std::string cipherText = encryptAES(plainText, key); std::cout << "Cipher text: " << cipherText << std::endl; // 解密 std::string decryptedText = decryptAES(cipherText, key); std::cout << "Decrypted text: " << decryptedText << std::endl; return 0; } ``` 这个示例代码使用了Crypto++库提供的AES加密和解密函数,使用ECB加密模式进行加密和解密,返回的是Base64编码后的密文。你可以根据自己的需求修改加密模式和返回结果的格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高亚奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值