OPENSSL对压缩包处理

本文分享了使用OpenSSL进行zip文件的AES加密、解密以及解压的C++代码实现,包括AES-CBC加密/解密函数和利用ZLibWrap.dll进行解压缩的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

openssl对压缩包加解密

由于公司对项目安全问题都有一定的要求,而我最近也在做一个项目中的一个小模块,就是对压缩包进行解密和加密以及解压数据包。我在这里分享这三个功能的实现,随便做个小笔记。
1.加密zip

//加密
int EncryptionAndDecryption::aesEncryption(const char * key,const char * inputFile_path,
const char * outFile_Path,const char * VectorQuantity)
{	
	ifstream in(inputFile_path, ios::binary);
	if (!in) 
	{ 
		std::cerr << "无法打开文件\n";
		return 0;
	}
	ofstream out(outFile_Path, ios::binary);
	if (!out) 
	{ 
		std::cerr << "无法打开文件\n";
		return 0;
	}
	AES_KEY aes_key;
	AES_set_encrypt_key((const unsigned char *)key, 256, &aes_key);
	char *in_data = new char[AES_BLOCK_SIZE + 1];
	char *out_data = new char[AES_BLOCK_SIZE + 1];
	while (in.good()) 
	{
		in.read(in_data,AES_BLOCK_SIZE);
		size_t bytes_read = in.gcount();
		if (bytes_read == 0)
		{
			break;
		}
		if (bytes_read < AES_BLOCK_SIZE)
		{
			out.write(in_data, in.gcount());
		}
		AES_cbc_encrypt((const unsigned char *)in_data, (unsigned char *)out_data,
		 bytes_read, &aes_key, (unsigned char *) VectorQuantity, AES_ENCRYPT);
		out.write(out_data, bytes_read);
	}
	in.close();
	out.close();
	delete []in_data;
	delete []out_data;
	return 1;
}

2.解密zip

//解密
int  EncryptionAndDecryption::aesDecryption( const char * key,
const char * inputFile_path,const char * outFile_Path,const char * VectorQuantity)
{
	ifstream in(inputFile_path, ios::binary);
	ofstream out(outFile_Path, ios::binary);
	AES_KEY aes_key;
	AES_set_decrypt_key((const unsigned char *)key, 256, &aes_key);
	char *in_data = new char[AES_BLOCK_SIZE + 1];
	char *out_data = new char[AES_BLOCK_SIZE + 1];
	while (in.good()) 
	{
		in.read(in_data, AES_BLOCK_SIZE);
		size_t bytes_read = in.gcount();
		if (bytes_read == 0) 
		{
			break;
		}
		if (bytes_read < AES_BLOCK_SIZE)
		{
			out.write(in_data, in.gcount());
		}
		AES_cbc_encrypt((const unsigned char *)in_data, (unsigned char *)out_data, 
		bytes_read, &aes_key,(unsigned char *) VectorQuantity, AES_DECRYPT);
		out.write(out_data, bytes_read);
	}

	in.close();
	out.close();
	delete []in_data;
	delete []out_data;
	return 0;
}

3.解压压缩包

bool EncryptionAndDecryption::DecompressingThePackage( const char * sourceFilePath,
const char * targetPath )
{
	const size_t cSize = strlen(sourceFilePath)+1;
	wchar_t* trSrc = new wchar_t[cSize];
	mbstowcs (trSrc, sourceFilePath, cSize);
	const size_t tar_Size = strlen(targetPath)+1;
	wchar_t* trDest = new wchar_t[tar_Size];
	mbstowcs (trDest, targetPath, tar_Size);
	BOOL result = FALSE;
	typedef	BOOL (*ZWZipExtract)(WCHAR*, WCHAR*);
	HMODULE hModule = LoadLibrary(L"ZLibWrap.dll"); //加载解压动态库文件
	if (NULL != hModule)
	{
		//获取解压函数地址
		ZWZipExtract pFunc = (ZWZipExtract)GetProcAddress(hModule, "ZWZipExtract");
		if (NULL != pFunc)
		{
			result = pFunc(trSrc, trDest);
		}															
	}
	FreeLibrary(hModule);
	delete [] trSrc;
	delete [] trDest;
	return result;
}

以上就是三个功能实现的demo,大家对一些知识不了解可以去查阅相关的资料,如果对openssl不清楚,可以查阅相关官方文档,毕竟现在网上什么都有。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值