cryptopp使用汇集

头文件等

#include <string>
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1 
#include "cryptopp/modes.h"
#include "cryptopp/aes.h"
#include "cryptopp/des.h"
#include "cryptopp/base64.h"
#include "cryptopp/md5.h"
#include "cryptopp/hex.h"
#include "cryptopp/files.h"
using namespace CryptoPP;
using namespace std;
#ifdef _DEBUG
#pragma comment(lib, "./3rd/cryptopp/debug/cryptlib.lib")
#else
#pragma comment(lib, "./3rd/cryptopp/release/cryptlib.lib")
#endif

字符串MD5算法

string Md5(string strData)
{
	string digest;
	Weak1::MD5 md5;
	StringSource(strData, true, new HashFilter(md5, new HexEncoder(new StringSink(digest))));
	return digest;
}

文件MD5算法

string FileMd5(string strFileName)
{
	string digest;
	if (!CDirectoryOP::IsExist(strFileName))
	{
	    return digest;
	}
	Weak1::MD5 md5;
	TRACE("\nFileMd5 file:%s\n", strFileName.c_str());
	CryptoPP::FileSource(strFileName.c_str(), true, new HashFilter(md5, new HexEncoder(new StringSink(digest))));
	TRACE("\nFileMd5 md5:%s\n", digest.c_str());
	return digest;
}

字符串Base64编码

string Base64Enc(string strSrc)
{
	string strOut;
	CryptoPP::StringSink* sink = new CryptoPP::StringSink(strOut);
	Base64Encoder *base64Enc = new Base64Encoder(sink);
	int iLen = strSrc.size();
	StringSource source(strSrc, true, base64Enc);

	int iPos=-1;
	iPos = strOut.find("\n", 0);
	while(iPos>=0)
	{
		strOut = strOut.erase(iPos, 1);
		iPos = strOut.find("\n", 0);
	}

	return strOut;
}

字符中Base64解码

string Base64Dec(string strSrc)
{
	string strOut;
	StringSink *sink = new StringSink(strOut);
	Base64Decoder *baseDec = new Base64Decoder(sink);
	StringSource dst(strSrc, true, baseDec);
	return strOut;
}

文件Base64编码

string FileBase64Enc(string strFileName)
{
	string strBase64;

	if (!CDirectoryOP::IsExist(strFileName))
	{
		LOG4CPLUS_DEBUG(g_logRoot, "FileBase64Enc 文件不存在<< " << strFileName);
		return strBase64;
	}

	string encode;
	CryptoPP::FileSource(strFileName.c_str(), true, new Base64Encoder(new StringSink(encode)));
	strBase64 = encode;

	int iPos=-1;
	iPos = strBase64.find("\n", 0);
	while(iPos>=0)
	{
		strBase64 = strBase64.erase(iPos, 1);
		iPos = strBase64.find("\n", 0);
	}

	return strBase64;
}

文件Base64解码

void FileBase64Dec(string strBase64, string strFileName)
{
	CryptoPP::FileSink *sink = new CryptoPP::FileSink(strFileName.c_str());
	Base64Decoder *base64Dec = new Base64Decoder(sink);
	StringSource dst(strBase64, true, base64Dec);
}

DES,CBC方式加解密

BYTE g_rgbKey[8] = { 23, 14, 119, 120, 117, 122, 13, 46 };//key 
BYTE g_rgbIV[8] = { 45, 27, 111, 125, 126, 32, 122, 104 };//向量

加密:

void Des_CBC_Enc(string strContent2Enc, string &strEncContent)
{
			DESEncryption desE(g_rgbKey);
			CBC_Mode_ExternalCipher::Encryption modeE(desE, g_rgbIV);
			string ciphertext;
			string plaintext = strContent2Enc;
			
			//CryptoPP::StringSink *pStringSink = new CryptoPP::StringSink( ciphertext );
			CryptoPP::StreamTransformationFilter stfEncryptor(modeE, new CryptoPP::StringSink( ciphertext ));
			stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() );
			stfEncryptor.MessageEnd();
			strEncContent = ciphertext;
}

解密:

void Des_CBC_Dec(string ciphertext, string &strDecContent)
{
	string strOut;
	DESDecryption desD(g_rgbKey);
	CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( desD, g_rgbIV );
	CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( strOut ) );
	stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
	//stfDecryptor.Put( reinterpret_cast<const unsigned char*>( pszCipherBuf ), iLen );
	stfDecryptor.MessageEnd();
	strDecContent = strOut;
}

编译静态库后,可以直接使用DES,base64等类,但是如果使用动态库,则无法使用DES和BASE64等的加密和编码功能,原因如下:(注:使用的版本是cryptopp562)

When using the DLL or FIPS DLL, the newsgroup occasional receives reports of LNK2001 and LINK2019 such as below. This is because the DLL and FIPS DLL only includes certified algorithms. So the DLL exports AES, but it does not export DES (DES has been deprecated in various standards). Also missing from the FIPS DLL are supporting classes such as Base64Encoder.

在vs 下编译发生LNK2019或LNK2001错误

解决方案如下:(摘自:http://www.codeproject.com/Articles/16388/Compiling-and-Integrating-Crypto-into-the-Microsof)

To resolve this, first read the "MSVC-Specific Information" in readme.txt. Wei offers two solutions to the issue:

There are two ways you can deal with this, either change Crypto++ to export those classes, by using the  CRYPTOPP_DLL macro, or link with both the DLL export library and a static library that contains the non-DLL classes and functions. The latter can be built by using the "DLL-Import" configuration of the cryptlib project.

需要注意的是:编译"DLL-Import"配置的static library cryptlib项目时要将配置中的Code Gerneration中的相关选项配置成MDD或/MD

注1:使用动态库时需要添加头文件【dll.h】

你可能感兴趣的:(cryptopp使用汇集)