Guide
- windows 10 64-bit
- crypto++ 7.0
- vs 2015
compile
download crypto++ 7.0
open cryptest.sln
with Visual Studio 2015
change Windows SDK Version
from 8.1
to 10.0
compile cryptodll
with Release x64
and we get cryptopp.dll
and cryptopp.lib
sdk
copy headers to include
, copy libs to lib
and dlls to dll
like this:
cryptopp-config.cmake
set(CRYPTOPP_FOUND TRUE) # auto
set(CRYPTOPP_VERSION 7.0.0)
set(CRYPTOPP_ROOT_DIR "C:/car_libs/cryptopp-7.0")
find_path(CRYPTOPP_INCLUDE_DIR NAMES cryptopp/aes.h PATHS "${CRYPTOPP_ROOT_DIR}/include")
mark_as_advanced(CRYPTOPP_INCLUDE_DIR) # show entry in cmake-gui
find_library(CRYPTOPP_LIBRARY NAMES cryptopp.lib PATHS "${CRYPTOPP_ROOT_DIR}/lib")
mark_as_advanced(CRYPTOPP_LIBRARY) # show entry in cmake-gui
# use xxx_INCLUDE_DIRS and xxx_LIBRARIES in CMakeLists.txt
set(CRYPTOPP_INCLUDE_DIRS ${CRYPTOPP_INCLUDE_DIR} )
set(CRYPTOPP_LIBRARIES ${CRYPTOPP_LIBRARY} )
message( "cryptopp-config.cmake " ${CRYPTOPP_ROOT_DIR})
Example
see cryptopp-7.0/dlltest.cpp
Code
cipher.h
#pragma once
#pragma warning( disable: 4275 )
#pragma warning( disable: 4819 )
#include <iostream>
#include <cryptopp/dll.h>
#include <cryptopp/cryptlib.h>
#include <cryptopp/aes.h>
#include <cryptopp/filters.h>
#include <cryptopp/modes.h>
using namespace std;
USING_NAMESPACE(CryptoPP)
class CipherApiImpl
{
public:
enum MODE
{
AES,
DES
};
public:
CipherApiImpl(MODE mode = MODE::AES);
int get_filesize(const char *filename);
int save(const std::string& filepath, const std::string& str);
std::string load(const std::string& filepath);
std::string encrypt_string(const std::string& plain_text);
std::string decrypt_string(const std::string& cipher_text);
std::string encrypt_file(const std::string& infile);
std::string decrypt_file(const std::string& infile);
int encrypt_file(const std::string& infile, const std::string& outfile);
int decrypt_file(const std::string& infile, const std::string& outfile);
protected:
std::string tohex(const char& c);
std::string tohexs(const std::string& str);
char tochar(const char& hex1, const char& hex2);
std::string tochars(const std::string& hex);
private:
// 32 + 16
const byte aes_key[CryptoPP::AES::MAX_KEYLENGTH] = {
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
};
const byte aes_iv[CryptoPP::AES::BLOCKSIZE] = {
0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
};
// 24+8
const byte des_key[CryptoPP::DES_EDE3::MAX_KEYLENGTH] = {
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
};
const byte des_iv[CryptoPP::DES_EDE3::BLOCKSIZE] = {
0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
};
CryptoPP::CFB_FIPS_Mode<CryptoPP::AES>::Encryption aes_encryption;
CryptoPP::CFB_FIPS_Mode<CryptoPP::AES>::Decryption aes_decryption;
CryptoPP::CFB_FIPS_Mode<CryptoPP::DES_EDE3>::Encryption des_encryption;
CryptoPP::CFB_FIPS_Mode<CryptoPP::DES_EDE3>::Decryption des_decryption;
MODE m_mode;
};
cipher.cpp
#include "cipher_api_impl.h"
#include <sys/stat.h>
CipherApiImpl::CipherApiImpl(MODE mode)
{
m_mode = mode;
// for des
des_encryption.SetKeyWithIV(des_key, sizeof(des_key), des_iv);
des_decryption.SetKeyWithIV(des_key, sizeof(des_key), des_iv);
// for aes
aes_encryption.SetKeyWithIV(aes_key, sizeof(aes_key), aes_iv);
aes_decryption.SetKeyWithIV(aes_key, sizeof(aes_key), aes_iv);
}
#pragma region hex/char
std::string CipherApiImpl::tohex(const char& c)
{
/*
00,01,02,...0F [0,1,2,...15]
10,11,12,...1F [16,17,...31]
...
F0,F1,F2,...FF [240,241,...255]
*/
/*
char ch[2] = { 0 };
sprintf(ch, "%02x", static_cast<byte>(c)); // char ---> 16 hex
return std::string(ch);
*/
const char HEX[] = "0123456789ABCDEF";
const int HEX_BASE = 16;
std::string c_hex;
int uc = (unsigned char)(c);
int a = uc / HEX_BASE;
int b = uc % HEX_BASE;
c_hex.push_back(HEX[a]);
c_hex.push_back(HEX[b]);
return c_hex;
}
std::string CipherApiImpl::tohexs(const std::string& str)
{
const char HEX[] = "0123456789ABCDEF";
const int HEX_BASE = 16;
std::string hex_result;
for (size_t i = 0; i < str.size(); i++)
{
std::string c_hex;
int uc = (unsigned char)(str[i]);
int a = uc / HEX_BASE;
int b = uc % HEX_BASE;
c_hex.push_back(HEX[a]);
c_hex.push_back(HEX[b]);
hex_result += c_hex;
}
return hex_result;
}
char CipherApiImpl::tochar(const char& hex1, const char& hex2)
{
const int HEX_BASE = 16;
int h1 = (int)hex1;
int h2 = (int)hex2;
// F1 ===> 16,1===>15*16+1 = 241 ===> char
char uc = (char)(h1*HEX_BASE + h2);
return uc;
}
std::string CipherApiImpl::tochars(const std::string& hex)
{
if (hex.size() % 2 != 0)
{
return "";
};
std::map<char, int> hex_int = {
{ '0' , 0 },
{ '1', 1 },
{ '2', 2 },
{ '3', 3 },
{ '4', 4 },
{ '5', 5 },
{ '6', 6 },
{ '7', 7 },
{ '8', 8 },
{ '9', 9 },
{ 'A', 10 },
{ 'B', 11 },
{ 'C', 12 },
{ 'D', 13 },
{ 'E', 14 },
{ 'F', 15 },
};
std::string char_result;
const int HEX_BASE = 16;
for (size_t i = 0; i < hex.size(); i += 2)
{
int h1 = hex_int.at(hex[i]); // 'F' ===>15
int h2 = hex_int.at(hex[i + 1]); // '1'===>1
// F1 ===> 15,1===>15*16+1 = 241 ===> char
//std::cout << "===============\n";
char uc = (char)(h1*HEX_BASE + h2);
//std::cout << h1 <<","<<h2 << std::endl;
//std::cout << uc << std::endl;
char_result += uc;
}
return char_result;
}
#pragma endregion
#pragma region encrypt_string and decrypt_string
int CipherApiImpl::get_filesize(const char *filename)
{
struct stat f_stat;
if (stat(filename, &f_stat) == -1)
{
return -1;
}
return f_stat.st_size;
}
/*
std::string load_text_not_use(const std::string& filepath)
{
std::string str;
ifstream in(filepath, ios::in | ios::binary);
std::string line;
std::string content;
while (getline(in, line))
{
//std::cout << "line.size()=" << line.size() << std::endl;
if (line.size() > 0) {
content += line + "\n";
}
line.clear();
}
in.close();
return content;
}
*/
int CipherApiImpl::save(const std::string& filepath, const std::string& str)
{
ofstream out(filepath, ios::out | ios::binary); // binary mode (default text mode)
out.write(str.c_str(), str.length());
out.close();
return 1;
}
std::string CipherApiImpl::load(const std::string& filepath)
{
int filesize = get_filesize(filepath.c_str());
char *buffer = new char[filesize];
ifstream in(filepath, ios::in | ios::binary); // binary mode (default text mode)
in.read(buffer, filesize);
in.close();
// std::string content(buffer); // ERROR for binary file
std::string content;
for (size_t i = 0; i < filesize; i++)
{
content.push_back(buffer[i]);
}
delete[] buffer;
return content;
}
/*
//==========================================================================
std::string cipher_text;
CryptoPP::AES::CipherApiImpl aesEncryption(key, CryptoPP::AES::MAX_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::CipherApiImpl cbcEncryption(aesEncryption, iv);
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(cipher_text));
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plain_text.c_str()), plain_text.length());
stfEncryptor.MessageEnd();
std::string cipher_text_hex = tohexs(cipher_text);
return cipher_text_hex;
//==========================================================================
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::MAX_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedText));
stfDecryptor.Put(reinterpret_cast<const unsigned char*>(cipher_text.c_str()), cipher_text.size());
stfDecryptor.MessageEnd();
return decrypted_text;
//==========================================================================
byte ciphertext[24];
byte decrypted[24];
CFB_FIPS_Mode<DES_EDE3>::Encryption encryption_DES_EDE3_CFB;
encryption_DES_EDE3_CFB.SetKeyWithIV(key, sizeof(key), iv);
encryption_DES_EDE3_CFB.ProcessString(ciphertext, plaintext, 24);
CFB_FIPS_Mode<DES_EDE3>::Decryption decryption_DES_EDE3_CFB;
decryption_DES_EDE3_CFB.SetKeyWithIV(key, sizeof(key), iv);
decryption_DES_EDE3_CFB.ProcessString(decrypted, ciphertext, 24);
*/
std::string CipherApiImpl::encrypt_string(const std::string& plain_text)
{
size_t size = plain_text.size();
const byte* in_byte = (const unsigned char*)(plain_text.c_str());
byte* out_byte = new byte[size];
des_encryption.ProcessString(out_byte, in_byte, size);
std::string cipher_text;
for (size_t i = 0; i < size; i++)
{
cipher_text.push_back(out_byte[i]);
}
delete[] out_byte;
return cipher_text;
}
std::string CipherApiImpl::decrypt_string(const std::string& cipher_text)
{
size_t size = cipher_text.size();
const byte* in_byte = (const unsigned char*)(cipher_text.c_str());
byte* out_byte = new byte[size];
des_decryption.ProcessString(out_byte, in_byte, size);
std::string plain_text;
for (size_t i = 0; i < size; i++)
{
plain_text.push_back(out_byte[i]);
}
delete[] out_byte;
return plain_text;
}
std::string CipherApiImpl::encrypt_file(const std::string& infile)
{
std::string plain_text = load(infile);
std::string cipher_text = encrypt_string(plain_text);
return cipher_text;
}
std::string CipherApiImpl::decrypt_file(const std::string& infile)
{
std::string cipher_text = load(infile);
std::string plain_text = decrypt_string(cipher_text);
return plain_text;
}
int CipherApiImpl::encrypt_file(const std::string& infile, const std::string& outfile)
{
std::string cipher_text_hex = encrypt_file(infile);
return save(outfile, cipher_text_hex);
}
int CipherApiImpl::decrypt_file(const std::string& infile, const std::string& outfile)
{
std::string plain_text = decrypt_file(infile);
return save(outfile, plain_text);
}
#pragma endregion
test.cpp
#include <iostream>
#include <string>
#include <gtest/gtest.h>
#include <glog/logging.h>
#pragma warning( disable: 4819 )
#pragma warning( disable: 4244 )
#pragma warning( disable: 4267 )
#include "cipher/cipher.h"
using namespace std;
int test_cipher_api()
{
CipherApi cipher;
std::string text = "[hello zhuzhu dashen !]\n[1234567]\n[123]\n1\n";
cout << "text : " << text << endl;
cout << "text size: " << text.size() << endl;
cipher.save("./tmp/text.txt", text);
std::cout << "==============encrypt/decrypt string======================\n";
std::string cipherHex = cipher.encrypt_string(text);
//cout << "cipher : " << cipherHex << endl;
cipher.save("./tmp/cipherHex.txt", cipherHex);
std::string text2 = cipher.decrypt_string(cipherHex);
cout << "text2 : " << text2 << endl;
cout << "text2 size: " << text2.size() << endl;
cipher.save("./tmp/text2.txt", text2);
// load_text
std::string text_load = cipher.load("./tmp/text2.txt");
cout << "text_load : " << text_load << endl;
cout << "text_load size: " << text_load.size() << endl;
// encrypt/decrypt text file
std::cout << "==============encrypt/decrypt text file======================\n";
cipher.encrypt_file("./tmp/text.txt", "./tmp/text_encryption.txt");
cipher.decrypt_file("./tmp/cipherHex.txt", "./tmp/cipherHex_decryption.txt");
// encrypt/decrypt prototxt
std::cout << "==============encrypt/decrypt prototxt======================\n";
cipher.encrypt_file("./tmp/small/deploy.prototxt", "./tmp/small/deploy_encryption.prototxt");
cipher.decrypt_file("./tmp/small/deploy_encryption.prototxt", "./tmp/small/deploy_new.prototxt");
std::cout << "==============encrypt/decrypt caffemodel======================\n";
cipher.encrypt_file("./tmp/small/recon__iter_140000.caffemodel", "./tmp/small/recon__iter_140000_encryption.caffemodel");
cipher.decrypt_file("./tmp/small/recon__iter_140000_encryption.caffemodel", "./tmp/small/recon__iter_140000_new.caffemodel");
return 0;
}
TEST(cipher_test, test_cipher_api) {
test_cipher_api();
}
CMakeLists.txt
find_package(CRYPTOPP REQUIRED)
MESSAGE( [Main] " CRYPTOPP_INCLUDE_DIRS = ${CRYPTOPP_INCLUDE_DIRS}")
MESSAGE( [Main] " CRYPTOPP_LIBRARIES = ${CRYPTOPP_LIBRARIES}")
include_directories(${CRYPTOPP_INCLUDE_DIRS})
target_link_libraries (demo ${CRYPTOPP_LIBRARIES})
Reference
History
- 20181018: created.