Cryptopp在VS上使用以及DES弱密钥测试

Cryptopp在VS上使用以及DES弱密钥测试

Cryptopp在VS上使用
  1. 克隆仓库

    git clone https://github.com/weidai11/cryptopp.git

  2. 运行文件中cryptest.sln

  3. 解决方案资源管理器中找到cryptlib中源文件cryptlib.cpp,在Debug和Release和x64(64位电脑)模式下运行。

  4. 在克隆的文件目录下的\x64\Output可以看见生成的Debug和Release文件夹,然后自己找个地方创建个文件夹这里我创建的文件夹为:F:\\Cryptopp_VS

  5. 创建include文件夹和lib文件夹。include放克隆的cryptlib项目的头文件。lib文件夹放刚刚生成的Debug和Release文件夹。

  6. VS新建项目,然后

    • 项目属性->C/C+±>常规->附加包含目录,加上刚刚的include文件夹路径F:\Cryptopp_VS\include;
    • 项目属性->链接器->常规->附加库目录,加上Debug和Release的目录,你要使用哪一个哪一个就排在前面。
    • 项目属性->链接器->输入->附加依赖项,加上cryptlib.lib
  7. 在项目属性->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;
}

参考:

  1. https://blog.csdn.net/diablof/article/details/72779265[error LNK2038报错]
  2. https://blog.csdn.net/IT_Guo_/article/details/103701403[VS配置cryptopp,如果按照上面配置失败,建议看看这个]
  3. https://www.cryptopp.com/wiki/Main_Page[cryptopp wiki]
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值