简述

Crypto++库是一个用c++ 编写的密码类库,是一个自由软件。有关它的信息可以访问以下两个网站:

下载

解压缩之后,我们会看到里面包含大量的头文件、源文件、以及工程文件,用VS2013打开cryptest.sln,然后进行编译(清理->重新生成)即可。

编译完成,会生成cryptest.exe、cryptlib.lib(E:\Crypto++5.6.3\Win32\Output\Debug)。

使用

新建一个Win32控制台应用程序TestCrypto++。

右键“属性”,选择“配置属性”-> C/C++ ->常规,附加包含目录E:\Crypto++5.6.3

右键“属性”,选择“配置属性”-> 链接器 ->常规,附加库目录E:\Crypto++5.6.3\Win32\Output\Debug

右键“属性”,选择“配置属性”-> C/C++ -> 代码生成,运行库选择“多线程调试(/MTd)”(Release模式下对应“多线程(/MT)”)。

此时,使用Crypto++的开发环境就搭建好了,在Crypto++ User Guide中的例子。

AES加解密

Ok,编写测试程序。。。

效果

a8fee9023868571a834f2e480dfc4fdf.png

源码

// TestCrypto++.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"

#include

#include // StreamTransformationFilter

#include // CFB_Mode

#include // std:cerr

#include // std::stringstream

#include

using namespace std;

using namespace CryptoPP;

#pragma comment(lib, "cryptlib.lib" )

std::string ECB_AESEncryptStr(std::string sKey, const char *plainText)

{

std::string outstr;

//填key

SecByteBlock key(AES::MAX_KEYLENGTH);

memset(key, 0x30, key.size());

sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

ECB_Mode_ExternalCipher::Encryption ecbEncryption(aesEncryption);

StreamTransformationFilter ecbEncryptor(ecbEncryption, new HexEncoder(new StringSink(outstr)));

ecbEncryptor.Put((byte *)plainText, strlen(plainText));

ecbEncryptor.MessageEnd();

return outstr;

}

std::string ECB_AESDecryptStr(std::string sKey, const char *cipherText)

{

std::string outstr;

//填key

SecByteBlock key(AES::MAX_KEYLENGTH);

memset(key, 0x30, key.size());

sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

ECB_Mode::Decryption ecbDecryption((byte *)key, AES::MAX_KEYLENGTH);

HexDecoder decryptor(new StreamTransformationFilter(ecbDecryption, new StringSink(outstr)));

decryptor.Put((byte *)cipherText, strlen(cipherText));

decryptor.MessageEnd();

return outstr;

}

std::string CBC_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)

{

std::string outstr;

//填key

SecByteBlock key(AES::MAX_KEYLENGTH);

memset(key, 0x30, key.size());

sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

//填iv

byte iv[AES::BLOCKSIZE];

memset(iv, 0x30, AES::BLOCKSIZE);

sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);

StreamTransformationFilter cbcEncryptor(cbcEncryption, new HexEncoder(new StringSink(outstr)));

cbcEncryptor.Put((byte *)plainText, strlen(plainText));

cbcEncryptor.MessageEnd();

return outstr;

}

std::string CBC_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)

{

std::string outstr;

//填key

SecByteBlock key(AES::MAX_KEYLENGTH);

memset(key, 0x30, key.size());

sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

//填iv

byte iv[AES::BLOCKSIZE];

memset(iv, 0x30, AES::BLOCKSIZE);

sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

CBC_Mode::Decryption cbcDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);

HexDecoder decryptor(new StreamTransformationFilter(cbcDecryption, new StringSink(outstr)));

decryptor.Put((byte *)cipherText, strlen(cipherText));

decryptor.MessageEnd();

return outstr;

}

std::string CBC_CTS_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)

{

std::string outstr;

//填key

SecByteBlock key(AES::MAX_KEYLENGTH);

memset(key, 0x30, key.size());

sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

//填iv

byte iv[AES::BLOCKSIZE];

memset(iv, 0x30, AES::BLOCKSIZE);

sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

CBC_CTS_Mode_ExternalCipher::Encryption cbcctsEncryption(aesEncryption, iv);

StreamTransformationFilter cbcctsEncryptor(cbcctsEncryption, new HexEncoder(new StringSink(outstr)));

cbcctsEncryptor.Put((byte *)plainText, strlen(plainText));

cbcctsEncryptor.MessageEnd();

return outstr;

}

std::string CBC_CTS_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)

{

std::string outstr;

//填key

SecByteBlock key(AES::MAX_KEYLENGTH);

memset(key, 0x30, key.size());

sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

//填iv

byte iv[AES::BLOCKSIZE];

memset(iv, 0x30, AES::BLOCKSIZE);

sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

CBC_CTS_Mode::Decryption cbcctsDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);

HexDecoder decryptor(new StreamTransformationFilter(cbcctsDecryption, new StringSink(outstr)));

decryptor.Put((byte *)cipherText, strlen(cipherText));

decryptor.MessageEnd();

return outstr;

}

std::string CFB_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)

{

std::string outstr;

//填key

SecByteBlock key(AES::MAX_KEYLENGTH);

memset(key, 0x30, key.size());

sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

//填iv

byte iv[AES::BLOCKSIZE];

memset(iv, 0x30, AES::BLOCKSIZE);

sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

CFB_Mode_ExternalCipher::Encryption cfbEncryption(aesEncryption, iv);

StreamTransformationFilter cfbEncryptor(cfbEncryption, new HexEncoder(new StringSink(outstr)));

cfbEncryptor.Put((byte *)plainText, strlen(plainText));

cfbEncryptor.MessageEnd();

return outstr;

}

std::string CFB_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)

{

std::string outstr;

//填key

SecByteBlock key(AES::MAX_KEYLENGTH);

memset(key, 0x30, key.size());

sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

//填iv

byte iv[AES::BLOCKSIZE];

memset(iv, 0x30, AES::BLOCKSIZE);

sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

CFB_Mode::Decryption cfbDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);

HexDecoder decryptor(new StreamTransformationFilter(cfbDecryption, new StringSink(outstr)));

decryptor.Put((byte *)cipherText, strlen(cipherText));

decryptor.MessageEnd();

return outstr;

}

std::string OFB_AESEncryptStr(std::string sKey, std::string sIV, const char *plainText)

{

std::string outstr;

//填key

SecByteBlock key(AES::MAX_KEYLENGTH);

memset(key, 0x30, key.size());

sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

//填iv

byte iv[AES::BLOCKSIZE];

memset(iv, 0x30, AES::BLOCKSIZE);

sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

AES::Encryption aesEncryption((byte *)key, AES::MAX_KEYLENGTH);

OFB_Mode_ExternalCipher::Encryption ofbEncryption(aesEncryption, iv);

StreamTransformationFilter ofbEncryptor(ofbEncryption, new HexEncoder(new StringSink(outstr)));

ofbEncryptor.Put((byte *)plainText, strlen(plainText));

ofbEncryptor.MessageEnd();

return outstr;

}

std::string OFB_AESDecryptStr(std::string sKey, std::string sIV, const char *cipherText)

{

std::string outstr;

//填key

SecByteBlock key(AES::MAX_KEYLENGTH);

memset(key, 0x30, key.size());

sKey.size() <= AES::MAX_KEYLENGTH ? memcpy(key, sKey.c_str(), sKey.size()) : memcpy(key, sKey.c_str(), AES::MAX_KEYLENGTH);

//填iv

byte iv[AES::BLOCKSIZE];

memset(iv, 0x30, AES::BLOCKSIZE);

sIV.size() <= AES::BLOCKSIZE ? memcpy(iv, sIV.c_str(), sIV.size()) : memcpy(iv, sIV.c_str(), AES::BLOCKSIZE);

OFB_Mode::Decryption ofbDecryption((byte *)key, AES::MAX_KEYLENGTH, iv);

HexDecoder decryptor(new StreamTransformationFilter(ofbDecryption, new StringSink(outstr)));

decryptor.Put((byte *)cipherText, strlen(cipherText));

decryptor.MessageEnd();

return outstr;

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐