Windows10 VS2017 C++使用crypto++库加密解密(AES)

参考文章:
https://blog.csdn.net/tangcaijun/article/details/42110319

首先下载库:
https://www.cryptopp.com/#download
使用vs2017打开cryptest.sln文件,解决方案选择“重订解决方案目标”,升级sdk。
编译库和dll文件
将生成的cryptopp.lib和cryptopp.dll放到项目文件夹,如果单独运行需要将dll文件拷贝到debug文件夹和生成的exe文件放在一起使用。
新建win32 c++控制台程序,工程->配置属性->vc++目录->包含目录,填写cryptopp的目录,需要使用其中的头文件.
编码:

#include "pch.h"
#include <iostream>
#include <fstream>
#include <aes.h>
#include <filters.h>
#include <modes.h>
#include <Windows.h>


#pragma comment(lib, "cryptopp.lib")



using namespace std;

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);

	// 或者也可以
	/*
	char tmpK[] = "1234567890123456";
	char tmpIV[] = "1234567890123456";
	for (int j = 0; j < CryptoPP::AES::DEFAULT_KEYLENGTH; ++j)
	{
		key[j] = tmpK[j];
	}
	for (int i = 0; i < CryptoPP::AES::BLOCKSIZE; ++i)
	{
		iv[i] = tmpIV[i];
	}
	*/
}


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;
}

void writeCipher(string output)
{
	ofstream out("cipher.data");
	out.write(output.c_str(), output.length());
	out.close();

	cout << "writeCipher finish " << endl << endl;
}



string decrypt(string cipherTextHex)
{
	string cipherText;
	string decryptedText;
	int i = 0;
	while (true)
	{
		char c;
		int x;
		stringstream ss;
		ss << hex << cipherTextHex.substr(i, 2).c_str();
		ss >> x;
		c = (char)x;
		cipherText += c;
		if (i >= cipherTextHex.length() - 2)break;
		i += 2;
	}

	//
	CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
	CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption(aesDecryption, iv);
	CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink(decryptedText));
	stfDecryptor.Put(reinterpret_cast<const unsigned char*>(cipherText.c_str()), cipherText.size());

	stfDecryptor.MessageEnd();

	return decryptedText;
}

string readCipher()
{
	ifstream in("cipher.data");

	string line;
	string decryptedText;
	while (getline(in, line))
	{
		if (line.length() > 1)
		{
			decryptedText += decrypt(line) + "\n";
		}
		line.clear();
	}

	cout << "readCipher finish " << endl;
	in.close();
	return decryptedText;
}





int main()
{
	string text = "What's up dude!";
	cout << "text : " << text << endl;

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

	string decrpt_text = readCipher();
	cout << "text : " << decrpt_text << endl;


	return 0;
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
Windows Crypto API是一个提供密码学功能的,可以用于加密和解密数据。下面是一个简单的示例,演示如何使用Windows Crypto API加密和解密数据: ```c++ #include <Windows.h> #include <Wincrypt.h> #include <iostream> #pragma comment(lib, "Crypt32.lib") int main() { // 要加密的数据 const char* data = "Hello, world!"; DWORD dataLen = strlen(data) + 1; // 创建一个随机的加密密钥 HCRYPTPROV hProv; if (!CryptAcquireContext(&hProv, nullptr, nullptr, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { std::cerr << "Error acquiring crypto context: " << GetLastError() << std::endl; return 1; } HCRYPTKEY hKey; if (!CryptGenKey(hProv, CALG_AES_256, CRYPT_EXPORTABLE, &hKey)) { std::cerr << "Error generating key: " << GetLastError() << std::endl; CryptReleaseContext(hProv, 0); return 1; } // 加密数据 DWORD encryptedDataLen = dataLen; if (!CryptEncrypt(hKey, nullptr, TRUE, 0, reinterpret_cast<BYTE*>(const_cast<char*>(data)), &encryptedDataLen, dataLen)) { std::cerr << "Error encrypting data: " << GetLastError() << std::endl; CryptDestroyKey(hKey); CryptReleaseContext(hProv, 0); return 1; } // 输出加密后的数据 std::cout << "Encrypted data: "; for (DWORD i = 0; i < encryptedDataLen; ++i) { std::cout << std::hex << static_cast<int>(reinterpret_cast<const BYTE*>(data)[i]) << " "; } std::cout << std::endl; // 解密数据 if (!CryptDecrypt(hKey, nullptr, TRUE, 0, reinterpret_cast<BYTE*>(const_cast<char*>(data)), &dataLen)) { std::cerr << "Error decrypting data: " << GetLastError() << std::endl; CryptDestroyKey(hKey); CryptReleaseContext(hProv, 0); return 1; } // 输出解密后的数据 std::cout << "Decrypted data: " << data << std::endl; CryptDestroyKey(hKey); CryptReleaseContext(hProv, 0); return 0; } ``` 在上面的示例中,我们使用CryptAcquireContext函数获取一个加密上下文句柄,然后使用CryptGenKey生成一个随机的AES-256加密密钥。接着,我们使用CryptEncrypt函数对数据进行加密,再使用CryptDecrypt函数对数据进行解密。最后,我们释放加密上下文句柄和密钥句柄。 需要注意的是,在实际使用中,我们需要将加密密钥保存在安全的地方,以便后续使用
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值