1、准备如下几项内容:
a、WIN10环境下的QT5.15.2安装包,QTCreator对应版本安装。(自行安装)
b、Crypto++8.6安装包下载,官网:https://www.cryptopp.com/,这里没选择最新的8.7是因为mingw-32编译后的库文件使用有问题,有错误,但是8.6用同样的方式编译就可以正常使用。
2、使用QT环境编译CryptoPP 8.6,解压源码包,命令行模式下:qmake -project生成 .Pro文件,修改内容:TEMPLATE = lib,增加:DEFINES += CRYPTOPPLIB_LIBRARY,QMAKE_CXXFLAGS += "-msse4.2"
QMAKE_CXXFLAGS += "-mavx"
QMAKE_CXXFLAGS += "-mavx2"
QMAKE_CXXFLAGS += "-mpclmul"
QMAKE_CXXFLAGS += "-maes"
QMAKE_CXXFLAGS += "-msha"
LIBS += -lws2_32
3、删除无用信息:
TestPrograms/test_32bit.cpp \
TestPrograms/test_64bit.cpp \
TestPrograms/test_arm_acle_header.cpp \
TestPrograms/test_arm_aes.cpp \
TestPrograms/test_arm_asimd.cpp \
TestPrograms/test_arm_crc.cpp \
TestPrograms/test_arm_neon.cpp \
TestPrograms/test_arm_neon_header.cpp \
TestPrograms/test_arm_pmull.cpp \
TestPrograms/test_arm_sha1.cpp \
TestPrograms/test_arm_sha256.cpp \
TestPrograms/test_arm_sha3.cpp \
TestPrograms/test_arm_sha512.cpp \
TestPrograms/test_arm_sm3.cpp \
TestPrograms/test_arm_sm4.cpp \
TestPrograms/test_asm_mixed.cpp \
TestPrograms/test_cxx.cpp \
TestPrograms/test_cxx11.cpp \
TestPrograms/test_cxx11_alignas.cpp \
TestPrograms/test_cxx11_alignof.cpp \
TestPrograms/test_cxx11_assert.cpp \
TestPrograms/test_cxx11_atomic.cpp \
TestPrograms/test_cxx11_auto.cpp \
TestPrograms/test_cxx11_constexpr.cpp \
TestPrograms/test_cxx11_deletefn.cpp \
TestPrograms/test_cxx11_enumtype.cpp \
TestPrograms/test_cxx11_initializer.cpp \
TestPrograms/test_cxx11_lambda.cpp \
TestPrograms/test_cxx11_noexcept.cpp \
TestPrograms/test_cxx11_nullptr.cpp \
TestPrograms/test_cxx11_staticinit.cpp \
TestPrograms/test_cxx11_sync.cpp \
TestPrograms/test_cxx11_vartemplates.cpp \
TestPrograms/test_cxx14.cpp \
TestPrograms/test_cxx17.cpp \
TestPrograms/test_cxx17_assert.cpp \
TestPrograms/test_cxx17_exceptions.cpp \
TestPrograms/test_cxx98_exception.cpp \
TestPrograms/test_glibc.cpp \
TestPrograms/test_newlib.cpp \
TestPrograms/test_ppc_aes.cpp \
TestPrograms/test_ppc_altivec.cpp \
TestPrograms/test_ppc_power7.cpp \
TestPrograms/test_ppc_power8.cpp \
TestPrograms/test_ppc_power9.cpp \
TestPrograms/test_ppc_sha.cpp \
TestPrograms/test_ppc_vmull.cpp \
TestPrograms/test_pthreads.cpp \
TestPrograms/test_x86_aes.cpp \
TestPrograms/test_x86_avx.cpp \
TestPrograms/test_x86_avx2.cpp \
TestPrograms/test_x86_avx512.cpp \
TestPrograms/test_x86_clmul.cpp \
TestPrograms/test_x86_cpuid.cpp \
TestPrograms/test_x86_rdrand.cpp \
TestPrograms/test_x86_rdseed.cpp \
TestPrograms/test_x86_sha.cpp \
TestPrograms/test_x86_sse2.cpp \
TestPrograms/test_x86_sse3.cpp \
TestPrograms/test_x86_sse41.cpp \
TestPrograms/test_x86_sse42.cpp \
TestPrograms/test_x86_ssse3.cpp \
TestPrograms/test_x86_via_aes.cpp \
TestPrograms/test_x86_via_rng.cpp \
TestPrograms/test_x86_via_sha.cpp
4、命令模式执行:
qmake
mingw32-make all
注意:此处的 mingw32-make.exe文件要跟qtcreator开发环境下的项目编译环境一直,这样才能保证编译后的dll和a文件能正确调用。
编译无错误生成:libcryptopp860.a 和 cryptopp860.dll文件。
5、新建Qt开发项目,可以是widgets或者console程序,目录下新建目录(根据个人喜好):libs\cryptopp\include 和libs\cryptopp\lib。
6、拷贝crypto8.6源码目录下的所有h文件到上面的include目录。拷贝编译后的libcryptopp860.a 和 cryptopp860.dll文件到lib目录。
7、编辑程序项目pro文件,
增加:HEADERS += \
libs/cryptopp/include/3way.h \
libs/cryptopp/include/adler32.h \
libs/cryptopp/include/adv_simd.h \
libs/cryptopp/include/aes.h \
libs/cryptopp/include/aes_armv4.h \
libs/cryptopp/include/algebra.h \
libs/cryptopp/include/algparam.h \
libs/cryptopp/include/allocate.h \
libs/cryptopp/include/arc4.h \
libs/cryptopp/include/argnames.h \
libs/cryptopp/include/aria.h \
libs/cryptopp/include/arm_simd.h \
libs/cryptopp/include/asn.h \
libs/cryptopp/include/authenc.h \
libs/cryptopp/include/base32.h \
libs/cryptopp/include/base64.h \
libs/cryptopp/include/basecode.h \
libs/cryptopp/include/bench.h \
libs/cryptopp/include/blake2.h \
libs/cryptopp/include/blowfish.h \
libs/cryptopp/include/blumshub.h \
libs/cryptopp/include/camellia.h \
libs/cryptopp/include/cast.h \
libs/cryptopp/include/cbcmac.h \
libs/cryptopp/include/ccm.h \
libs/cryptopp/include/chacha.h \
libs/cryptopp/include/chachapoly.h \
libs/cryptopp/include/cham.h \
libs/cryptopp/include/channels.h \
libs/cryptopp/include/cmac.h \
libs/cryptopp/include/config.h \
libs/cryptopp/include/config_align.h \
libs/cryptopp/include/config_asm.h \
libs/cryptopp/include/config_cpu.h \
libs/cryptopp/include/config_cxx.h \
libs/cryptopp/include/config_dll.h \
libs/cryptopp/include/config_int.h \
libs/cryptopp/include/config_misc.h \
libs/cryptopp/include/config_ns.h \
libs/cryptopp/include/config_os.h \
libs/cryptopp/include/config_ver.h \
libs/cryptopp/include/cpu.h \
libs/cryptopp/include/crc.h \
libs/cryptopp/include/cryptlib.h \
libs/cryptopp/include/darn.h \
libs/cryptopp/include/default.h \
libs/cryptopp/include/des.h \
libs/cryptopp/include/dh.h \
libs/cryptopp/include/dh2.h \
libs/cryptopp/include/dll.h \
libs/cryptopp/include/dmac.h \
libs/cryptopp/include/donna.h \
libs/cryptopp/include/donna_32.h \
libs/cryptopp/include/donna_64.h \
libs/cryptopp/include/donna_sse.h \
libs/cryptopp/include/drbg.h \
libs/cryptopp/include/dsa.h \
libs/cryptopp/include/eax.h \
libs/cryptopp/include/ec2n.h \
libs/cryptopp/include/eccrypto.h \
libs/cryptopp/include/ecp.h \
libs/cryptopp/include/ecpoint.h \
libs/cryptopp/include/elgamal.h \
libs/cryptopp/include/emsa2.h \
libs/cryptopp/include/eprecomp.h \
libs/cryptopp/include/esign.h \
libs/cryptopp/include/factory.h \
libs/cryptopp/include/fhmqv.h \
libs/cryptopp/include/files.h \
libs/cryptopp/include/filters.h \
libs/cryptopp/include/fips140.h \
libs/cryptopp/include/fltrimpl.h \
libs/cryptopp/include/gcm.h \
libs/cryptopp/include/gf256.h \
libs/cryptopp/include/gf2_32.h \
libs/cryptopp/include/gf2n.h \
libs/cryptopp/include/gfpcrypt.h \
libs/cryptopp/include/gost.h \
libs/cryptopp/include/gzip.h \
libs/cryptopp/include/hashfwd.h \
libs/cryptopp/include/hc128.h \
libs/cryptopp/include/hc256.h \
libs/cryptopp/include/hex.h \
libs/cryptopp/include/hight.h \
libs/cryptopp/include/hkdf.h \
libs/cryptopp/include/hmac.h \
libs/cryptopp/include/hmqv.h \
libs/cryptopp/include/hrtimer.h \
libs/cryptopp/include/ida.h \
libs/cryptopp/include/idea.h \
libs/cryptopp/include/integer.h \
libs/cryptopp/include/iterhash.h \
libs/cryptopp/include/kalyna.h \
libs/cryptopp/include/keccak.h \
libs/cryptopp/include/lea.h \
libs/cryptopp/include/lsh.h \
libs/cryptopp/include/lubyrack.h \
libs/cryptopp/include/luc.h \
libs/cryptopp/include/mars.h \
libs/cryptopp/include/md2.h \
libs/cryptopp/include/md4.h \
libs/cryptopp/include/md5.h \
libs/cryptopp/include/mdc.h \
libs/cryptopp/include/mersenne.h \
libs/cryptopp/include/misc.h \
libs/cryptopp/include/modarith.h \
libs/cryptopp/include/modes.h \
libs/cryptopp/include/modexppc.h \
libs/cryptopp/include/mqueue.h \
libs/cryptopp/include/mqv.h \
libs/cryptopp/include/naclite.h \
libs/cryptopp/include/nbtheory.h \
libs/cryptopp/include/nr.h \
libs/cryptopp/include/oaep.h \
libs/cryptopp/include/oids.h \
libs/cryptopp/include/osrng.h \
libs/cryptopp/include/ossig.h \
libs/cryptopp/include/padlkrng.h \
libs/cryptopp/include/panama.h \
libs/cryptopp/include/pch.h \
libs/cryptopp/include/pkcspad.h \
libs/cryptopp/include/poly1305.h \
libs/cryptopp/include/polynomi.h \
libs/cryptopp/include/ppc_simd.h \
libs/cryptopp/include/pssr.h \
libs/cryptopp/include/pubkey.h \
libs/cryptopp/include/pwdbased.h \
libs/cryptopp/include/queue.h \
libs/cryptopp/include/rabbit.h \
libs/cryptopp/include/rabin.h \
libs/cryptopp/include/randpool.h \
libs/cryptopp/include/rc2.h \
libs/cryptopp/include/rc5.h \
libs/cryptopp/include/rc6.h \
libs/cryptopp/include/rdrand.h \
libs/cryptopp/include/resource.h \
libs/cryptopp/include/rijndael.h \
libs/cryptopp/include/ripemd.h \
libs/cryptopp/include/rng.h \
libs/cryptopp/include/rsa.h \
libs/cryptopp/include/rw.h \
libs/cryptopp/include/safer.h \
libs/cryptopp/include/salsa.h \
libs/cryptopp/include/scrypt.h \
libs/cryptopp/include/seal.h \
libs/cryptopp/include/secblock.h \
libs/cryptopp/include/secblockfwd.h \
libs/cryptopp/include/seckey.h \
libs/cryptopp/include/seed.h \
libs/cryptopp/include/serpent.h \
libs/cryptopp/include/serpentp.h \
libs/cryptopp/include/sha.h \
libs/cryptopp/include/sha1_armv4.h \
libs/cryptopp/include/sha256_armv4.h \
libs/cryptopp/include/sha3.h \
libs/cryptopp/include/sha512_armv4.h \
libs/cryptopp/include/shacal2.h \
libs/cryptopp/include/shake.h \
libs/cryptopp/include/shark.h \
libs/cryptopp/include/simeck.h \
libs/cryptopp/include/simon.h \
libs/cryptopp/include/simple.h \
libs/cryptopp/include/siphash.h \
libs/cryptopp/include/skipjack.h \
libs/cryptopp/include/sm3.h \
libs/cryptopp/include/sm4.h \
libs/cryptopp/include/smartptr.h \
libs/cryptopp/include/sosemanuk.h \
libs/cryptopp/include/speck.h \
libs/cryptopp/include/square.h \
libs/cryptopp/include/stdcpp.h \
libs/cryptopp/include/strciphr.h \
libs/cryptopp/include/tea.h \
libs/cryptopp/include/threefish.h \
libs/cryptopp/include/tiger.h \
libs/cryptopp/include/trap.h \
libs/cryptopp/include/trunhash.h \
libs/cryptopp/include/ttmac.h \
libs/cryptopp/include/tweetnacl.h \
libs/cryptopp/include/twofish.h \
libs/cryptopp/include/validate.h \
libs/cryptopp/include/vmac.h \
libs/cryptopp/include/wake.h \
libs/cryptopp/include/whrlpool.h \
libs/cryptopp/include/words.h \
libs/cryptopp/include/xed25519.h \
libs/cryptopp/include/xtr.h \
libs/cryptopp/include/xtrcrypt.h \
libs/cryptopp/include/xts.h \
libs/cryptopp/include/zdeflate.h \
libs/cryptopp/include/zinflate.h \
libs/cryptopp/include/zlib.h
增加:
INCLUDEPATH += $$PWD/libs/cryptopp/include
DEPENDPATH += $$PWD/libs/cryptopp/include
LIBS += $$PWD/libs/cryptopp/lib/cryptopp860.dll
8、至此crypto的引用已经完成,下面代码例子:
头文件引用:
#include
#include "libs/cryptopp/include/cryptlib.h"
#include "libs/cryptopp/include/des.h"
#include "libs/cryptopp/include/filters.h"
#include "libs/cryptopp/include/modes.h"
#include "libs/cryptopp/include/hex.h"
#include "libs/cryptopp/include/secblock.h"
名称空间:
using namespace std;
using namespace CryptoPP;
以DES-CBC-pkcs7-编解码为例(参照网上的例子):
string DES_CBC_Encryption(string sKey, std::string sIV, const char *plainText) {
std::string strCipherText;
try {
// 填key
CryptoPP::SecByteBlock key(CryptoPP::DES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= CryptoPP::DES::MAX_KEYLENGTH
? memcpy(key, sKey.c_str(), sKey.size())
: memcpy(key, sKey.c_str(), CryptoPP::DES::MAX_KEYLENGTH);
// 填iv
CryptoPP::byte iv[CryptoPP::DES::BLOCKSIZE];
memset(iv, 0x30, CryptoPP::DES::BLOCKSIZE);
sIV.size() <= CryptoPP::DES::BLOCKSIZE
? memcpy(iv, sIV.c_str(), sIV.size())
: memcpy(iv, sIV.c_str(), CryptoPP::DES::BLOCKSIZE);
CryptoPP::DES::Encryption desEncryption((CryptoPP::byte *)key,
CryptoPP::DES::MAX_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(desEncryption,
iv);
CryptoPP::StreamTransformationFilter cbcEncryptor(
cbcEncryption,
new CryptoPP::HexEncoder(new CryptoPP::StringSink(strCipherText)),
CryptoPP::BlockPaddingSchemeDef::BlockPaddingScheme::PKCS_PADDING);
cbcEncryptor.Put((CryptoPP::byte *)plainText, strlen(plainText));
cbcEncryptor.MessageEnd();
} catch (const CryptoPP::Exception &e) {
std::cout << e.what() << std::endl;
}
return strCipherText;
}
string DES_CBC_Decryption(std::string sKey, std::string sIV,
const char *cipherText) {
std::string strPlainText;
try {
// 填key
CryptoPP::SecByteBlock key(CryptoPP::DES::MAX_KEYLENGTH);
memset(key, 0x30, key.size());
sKey.size() <= CryptoPP::DES::MAX_KEYLENGTH
? memcpy(key, sKey.c_str(), sKey.size())
: memcpy(key, sKey.c_str(), CryptoPP::DES::MAX_KEYLENGTH);
// 填iv
CryptoPP::byte iv[CryptoPP::DES::BLOCKSIZE];
memset(iv, 0x30, CryptoPP::DES::BLOCKSIZE);
sIV.size() <= CryptoPP::DES::BLOCKSIZE
? memcpy(iv, sIV.c_str(), sIV.size())
: memcpy(iv, sIV.c_str(), CryptoPP::DES::BLOCKSIZE);
CryptoPP::CBC_Mode
(CryptoPP::byte *)key, CryptoPP::DES::MAX_KEYLENGTH, iv);
CryptoPP::HexDecoder decryptor(new CryptoPP::StreamTransformationFilter(
cbcDecryption, new CryptoPP::StringSink(strPlainText),
CryptoPP::BlockPaddingSchemeDef::BlockPaddingScheme::PKCS_PADDING));
decryptor.Put((CryptoPP::byte *)cipherText, strlen(cipherText));
decryptor.MessageEnd();
} catch (const CryptoPP::Exception &e) {
std::cout << e.what() << std::endl;
}
return strPlainText;
}
正常使用:
编码:
string strCBCEncryp = DES_CBC_Encryption(strKey, strIV, period_of_validity.c_str());
解码:
string strCBCDecryp = DES_CBC_Decryption(strKey, strIV, strCBCEncryp);
正常调用无问题。
9、程序打包,生成的exe文件在debug或者relase目录下,需要将cryptopp860.dll拷贝到目录下,a文件貌似不需要,然后执行:windeployqt.exe 程序执行文件名,补充相关的库文件和相关资料,此目录下就是完整的执行环境可以直接打包成一个执行文件,这里可以使用Enigma Virtual Box,挺方便,可以压缩大小并打包到一个exe文件中,具体百度,有很多资料说明。
9、总结几个问题:
9.1、之前在编译crypto问题上花了很多时间,官方默认的源码支持vsC++编译生成库文件,但是在QT环境下不能用(可能也有办法但是没深研究),QT下编译需要使用mingw,如果装了多个qt环境或者mingw环境就会很麻烦,需要调整系统环境变量达到编译一致性。
9.2、项目软件编译后执行目录下可以只保留dll文件,a文件不需要(容量也很大),另外pro文件里配置LIBS += $$PWD/libs/cryptopp/lib/cryptopp860.dll,不要用
LIBS += $$PWD/libs/cryptopp/lib/libcryptopp860.a,原因是使用a文件会出现windows环境下输出命令行信息,必须配对使用config += console才能正常使用,但是我们使用widgets模式时不需要显示命令窗口,每次启动软件会跟随打开一个命令窗口,很影响感受,因此在windows下我们引用dll库即可。
9.3、windeployqt.exe打包程序必须跟qt开发环境中使用的编译环境版本一致,否则会导致补充dll等库文件的程序无法定位程序输入点,因此建议系统里只装一个版本的qt,并使用对应的mingw编译环境。
10、最后,以上内容有自己总结也有借鉴网上查询资料,难免有纰漏,如有错误欢迎执教,关于crypto8.7的编译为什么不能用需要再研究一下,按理说不应该跟8.6差别这么大,哪位知道的同学欢迎指教。
下一步涉及Linux和Android环境下的编译使用,也研究了一下基本差不多,不会太难,大家自行查找即可。