Cryptopp在VS上使用以及DES弱密钥测试
Cryptopp在VS上使用
-
克隆仓库
git clone https://github.com/weidai11/cryptopp.git
-
运行文件中
cryptest.sln
-
解决方案资源管理器中找到
cryptlib
中源文件cryptlib.cpp
,在Debug和Release和x64(64位电脑)模式下运行。 -
在克隆的文件目录下的
\x64\Output
可以看见生成的Debug和Release文件夹,然后自己找个地方创建个文件夹这里我创建的文件夹为:F:\\Cryptopp_VS
。 -
创建
include
文件夹和lib
文件夹。include
放克隆的cryptlib项目的头文件。lib
文件夹放刚刚生成的Debug和Release文件夹。 -
VS新建项目,然后
- 项目属性->C/C+±>常规->附加包含目录,加上刚刚的include文件夹路径
F:\Cryptopp_VS\include;
- 项目属性->链接器->常规->附加库目录,加上Debug和Release的目录,你要使用哪一个哪一个就排在前面。
- 项目属性->链接器->输入->附加依赖项,加上
cryptlib.lib
- 项目属性->C/C+±>常规->附加包含目录,加上刚刚的include文件夹路径
-
在项目属性->C/C+±>代码生成->运行库保持和克隆的cryptlib项目一致。
反正报错error LNK2038: 检测到“RuntimeLibrary”的不匹配项就是这个原因
DES弱密钥测试
这里测试了几个弱密钥
#include<iostream>
#include<Cryptopp/des.h>
#include<Cryptopp/aes.h>
#include<Cryptopp/modes.h>
#include<string>
#include <iomanip>
using namespace std;
using namespace CryptoPP;
void show(unsigned char key[DES::DEFAULT_KEYLENGTH]) {
for (int i = 0; i < DES::DEFAULT_KEYLENGTH; i++) {
cout << hex<<setw(2)<<setfill('0')<< (int)key[i] << " ";
}
}
int main() {
unsigned char key[DES::DEFAULT_KEYLENGTH] = {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01};
unsigned char input[DES::BLOCKSIZE] = "12345";//DES::BLOCKSIZE为8字节
unsigned char output[DES::BLOCKSIZE];
unsigned char output1[DES::BLOCKSIZE];
//弱密钥
unsigned char weakKetSets[4][DES::DEFAULT_KEYLENGTH] = {
{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
{0x1F,0x1F,0x1F,0x1F,0x0F,0x0F,0x0E,0x0E},
{0xE0,0xE0,0xE0,0xE0,0xF1,0xF1,0xF1,0xF1},
{0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE}
};
//半弱密钥
unsigned char halfWeakKey[4][DES::DEFAULT_KEYLENGTH]{
{0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
{0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
{0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
{0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01}
};
cout << "input is: " << input << endl;
DESEncryption encryption_DES;
cout << "----弱密钥测试----" << endl;
//密码测试
for (int i = 0; i < 4; i++) {
encryption_DES.SetKey(weakKetSets[i], DES::KEYLENGTH);//设置加密器密钥
encryption_DES.ProcessBlock(input, output);//进行加密
encryption_DES.ProcessBlock(output, output1);
if (memcmp(output1, "12345", 5)==0) {
show(weakKetSets[i]);
cout << "-->" << "YES" << endl;
}
else {
show(weakKetSets[i]);
cout << "-->" << "NO" << endl;
}
}
cout << "----半弱密钥测试----" << endl;
for (int i = 0; i < 4; i+=2) {
encryption_DES.SetKey(halfWeakKey[i], DES::KEYLENGTH);
encryption_DES.ProcessBlock(input, output);
encryption_DES.SetKey(halfWeakKey[i+1], DES::KEYLENGTH);
encryption_DES.ProcessBlock(output, output1);
if (memcmp(output1, "12345", 5) == 0) {
show(weakKetSets[i]);
cout << endl;
show(weakKetSets[i+1]);
cout << "-->" << "YES" << endl;
}
else {
show(weakKetSets[i]);
cout << endl;
show(weakKetSets[i+1]);
cout << "-->" << "NO" << endl;
}
}
system("pause");
return 0;
}
参考:
- https://blog.csdn.net/diablof/article/details/72779265[error LNK2038报错]
- https://blog.csdn.net/IT_Guo_/article/details/103701403[VS配置cryptopp,如果按照上面配置失败,建议看看这个]
- https://www.cryptopp.com/wiki/Main_Page[cryptopp wiki]