使用Crypto++实现AES加解密

时间:2021-05-10 18:32:10

注意:AES加密解密是区分模式和填充方式的,本列子是基于CBC/PKCS的AES加密和解密,不同模式下的算法不通用,即不是CBC模式的加密的数据,按CBC模式是不能解密的。

1 下载Crypto++,下载路径可在其官网找到,官网地址:http://www.cryptopp.com/,我这次实现是选择的5.6.2版本,开源软件版本之间差异比较大,同学们参考时,记得核对版本。

2 下载之后,unzip解压到合适路径下,我的解压路径为:/home/lclin/crypto562,在解压目录下执行 make操作,编译默认生成lib库文件和测试程序的可执行文件,库文件的名称为:libcryptopp.a,Crypto++实现了很多安全算法,默认的make是编译所有的算法,所以编译生成的lib文件很大,5.6.2编译生成的lib文件为75M,可自行通过修改GNUMakefile文件生成so动态库,关于如何修改Makefile生成动态so,可参考如下文章(未亲自验证可行否,请各位同学自行验证):linux下完整编译crypto++库 以及使用crypto++进行RSA加密、解密实例程序

3 参考下面的代码可实现AES(CBC模式),md5算法,其他的算法请参考官网。

#ifndef _CRYPTO_UTIL_H_
#define _CRYPTO_UTIL_H_

#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1

#include <iostream>
#include <sstream>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string>

#include "aes.h"
#include "md5.h"
#include "hex.h"
#include "files.h"
#include "default.h"
#include "filters.h"
#include "osrng.h"

using namespace CryptoPP;

enum AESKeyLength
{
AES_KEY_LENGTH_16 = 16, AES_KEY_LENGTH_24 = 24, AES_KEY_LENGTH_32 = 32
};

class CCryptoUtil
{
public:
static int encrypt4aes(const std::string &inData, const std::string &strKey,
std::string &outData, std::string &errMsg)
{
outData = "";
errMsg = "";

if (inData.empty() || strKey.empty())
{
errMsg = "indata or key is empty!!";
return -1;
}

unsigned int iKeyLen = strKey.length();

if (iKeyLen != AES_KEY_LENGTH_16 && iKeyLen != AES_KEY_LENGTH_24
&& iKeyLen != AES_KEY_LENGTH_32)
{
errMsg = "aes key invalid!!";
return -2;
}

byte iv[AES::BLOCKSIZE];
int iResult = 0;

try
{
CBC_Mode<AES>::Encryption e;
e.SetKeyWithIV((byte*) strKey.c_str(), iKeyLen, iv);
StringSource ss(inData, true,
new StreamTransformationFilter(e, new StringSink(outData)));
} catch (const CryptoPP::Exception& e)
{
errMsg = "Encryptor throw exception!!";
iResult = -3;
}

return iResult;
}

static int decrypt4aes(const std::string &inData, const std::string &strKey,
std::string &outData, std::string &errMsg)
{
outData = "";
errMsg = "";

if (inData.empty() || strKey.empty())
{
errMsg = "indata or key is empty!!";
return -1;
}

unsigned int iKeyLen = strKey.length();

if (iKeyLen != AES_KEY_LENGTH_16 && iKeyLen != AES_KEY_LENGTH_24
&& iKeyLen != AES_KEY_LENGTH_32)
{
errMsg = "aes key invalid!!";
return -2;
}

byte iv[AES::BLOCKSIZE];
int iResult = 0;

try
{
CBC_Mode<AES>::Decryption d;
d.SetKeyWithIV((byte*) strKey.c_str(), iKeyLen, iv);
StringSource ss(inData, true,
new StreamTransformationFilter(d, new StringSink(outData)));
}
catch (const CryptoPP::Exception& e)
{
errMsg = "Encryptor throw exception";
iResult = -3;
}

return iResult;
}

static std::string md5(const std::string& inData)
{
std::string digest;
Weak1::MD5 md5;
StringSource(inData, true,
new HashFilter(md5, new HexEncoder(new StringSink(digest))));

return digest;
}
};

int main(int argc, char**argv)
{
std::string strKeyReed = "123456789";
std::string strCipher = "123456789";
std::string strKey = CCryptoUtil::md5(strKeyReed);

std::cout << "Key:"<<strKey << std::endl;

std::string strResult;
std::string strErrMsg;
int iResult = CCryptoUtil::encrypt4aes(strCipher, strKey, strResult, strErrMsg);
if(iResult)
{
std::cout<<"CCryptoUtil::encrypt4aes failed,errMsg:"<<strErrMsg;
return -1;
}

std::string strPlainText;
iResult = CCryptoUtil::decrypt4aes(strResult,strKey,strPlainText,strErrMsg);
if(iResult)
{
std::cout<<"CCryptoUtil::decrypt4aes failed,errMsg:"<<strErrMsg;
return -2;
}

std::cout << "PlainText:"<<strPlainText << std::endl;
}

#endif//_CRYPTO_UTIL_H_

4 编译该Demo时,需要相关的头文件和库文件,如果我的头文件和库文件都在/home/lclin/crypto/目录下,则

g++ crypto_util.cpp -I /home/lclin/crypto562/ -L /home/licuilin/crypto562/ -lcryptopp