使用Crypto++实现AES加解密

3 篇文章 0 订阅

注意: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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值