首页 > 其他分享 >第四周课设任务

第四周课设任务

时间:2022-12-04 19:12:07浏览次数:63  
标签:AES 四周 lib 课设 CryptoPP cryptopp 任务 include string

第四周课设任务

1.CRYPTO++库

参考教程:https://blog.csdn.net/qq_32261191/article/details/78855651
https://blog.csdn.net/Yonggie/article/details/100592532

1.下载crypto++文件包,并且生成lib库文件(我生成的是debug的)
https://cryptopp.com/

运行sln文件后,用debug模式运行就好
运行结束后在D:\cryptopp\x64\Output\Debug里有cryptlib.lib即可

之后运行代码,代码如下

#include "pch.h"
#include <string>
#include <cstring>
#include <vector>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include "cryptopp/aes.h"
#include "cryptopp/filters.h"
#include "cryptopp/modes.h"
using namespace std;
using namespace CryptoPP;

byte key[CryptoPP::AES::DEFAULT_KEYLENGTH], iv[CryptoPP::AES::BLOCKSIZE];

void initKV() {
	memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH);
	memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);
}

string encrypt(string plainText) {
	string cipherText;

	CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
	CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);
	CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(cipherText));
	stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plainText.c_str()), plainText.length() + 1);
	stfEncryptor.MessageEnd();

	string cipherTextHex;
	for (int i = 0; i < cipherText.size(); i++)
	{
		char ch[3] = { 0 };
		sprintf_s(ch, "%02x", static_cast<byte>(cipherText[i]));
		cipherTextHex += ch;
	}

	return cipherTextHex;
}


int main() {
	string text = "20201303zyb";
	cout << "text : " << text << endl;

	initKV();
	string cipherHex = encrypt(text);
	cout << "cipher : " << cipherHex << endl;
}

下面是关键一步,就是导入项目和lib库

导入项目

将cryptopp文件放入D:\vs\VC\Tools\MSVC\14.34.31933\include地址里

导入lib库

将debug模式生成的lib库放入D:\vs\VC\Tools\MSVC\14.34.31933\lib\x64里,注意一定要对应debug/release模式,不要整差了。

导入后发现报错_ITERATOR_DEBUG_LEVEL不匹配,错误代码LNK2038
调整C/C++模块的代码生成内容,改成多线程调试(/MTD)即可

发现debug运行成功

标签:AES,四周,lib,课设,CryptoPP,cryptopp,任务,include,string
From: https://www.cnblogs.com/sanfeng-ooo/p/16950432.html

相关文章