利用openssl的AES加密解密数据明文(ECB模式)

本文展示了如何利用openssl库进行AES 256位ECB模式的加密和解密操作,同时结合RSA算法对AES密钥进行加解密。通过读取文件、加密、解密过程,演示了完整的加密流程,并验证了解密的正确性。

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

#include<windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/aes.h>
#include<iostream>
using namespace std;
#pragma comment(lib,"libssl.lib")
#pragma comment(lib,"libcrypto.lib")


#define code1024 117
#define code512  53
char g_pPubFile[30] = { 0 };
char g_pPriFile[30] = { 0 };

//maxCodeByte = g_nBits/8-11这里设置了2048,意味着最多可以 编、解码  2048/8-11=117个字节
const int g_nBits = 1024;
using namespace std;

bool ReadTxt(unsigned char(&buf)[3200])
{
	FILE *fp;
	if ((fp = fopen("1.txt", "rb")) == NULL)
	{
		return false;
	}
	fseek(fp, 0, SEEK_END);
	int fileLen = ftell(fp);
	char *tmp = new char[fileLen];
	fseek(fp, 0, SEEK_SET);
	fread(tmp, fileLen, sizeof(char), fp);
	fclose(fp);

	memcpy(buf, tmp, fileLen);
	delete tmp;
	return true;
}

bool WriteTxt(unsigned char(&Decbuf)[3200])
{
	FILE *fp;
	if ((fp = fopen("2.txt", "wb")) == NULL)
	{
		return false;
	}
	rewind(fp);
	int  fileLen = strlen((char*)Decbuf);
	fwrite(Decbuf, fileLen, sizeof(char), fp);
	fclose(fp);
}

RSA功能函数部分///

//************************************
// function:  产生公私钥对,并保存为pem格式的文件。用来后续的加密解密
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值