C/C++ AES+base64加密

9 篇文章 0 订阅

头文件:

/*
 * CAesCbc.h
 *
 *  Created on: Oct 8, 2023
 *      Author: root
 */

#ifndef CAESCBC_H_
#define CAESCBC_H_
#include <string>
using namespace std;
class CAesCbc {
public:
	CAesCbc();
	virtual ~CAesCbc();
public:
	void Pkcs7Padding(std::string& strInPut);
	void Pkcs7UnPadding(std::string& strInPut);
	string SetEncryptKey(std::string strKey);
	string SetEncryptIv(std::string strIv);
	bool EncryptString(std::string strInPut, std::string& strOutPut);
	bool DecryptString(std::string strInPut, std::string& strOutPut);
private:
	std::string m_strKey;
	std::string m_strIv;
};

#endif /* CAESCBC_H_ */

源文件:

/*
 * CAesCbc.cpp
 *
 *  Created on: Oct 8, 2023
 *      Author: root
 */

#include <CAesCbc.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/md5.h>
#include "base64.h"

CAesCbc::CAesCbc()
{

}


CAesCbc::~CAesCbc()
{

}

void CAesCbc::Pkcs7Padding(std::string& strInPut)
{
	char cPadding = 16 - strInPut.length() % 16;
	int iPaddingNum = cPadding;
	for (int i = 0; i < iPaddingNum; i++)
	{
		strInPut += cPadding;
	}
}

void CAesCbc::Pkcs7UnPadding(std::string& strInPut)
{
	int iInputLen = strInPut.length();
	int iRemovedByte = strInPut[iInputLen - 1];

	strInPut = std::string(strInPut.c_str(), iInputLen - iRemovedByte);
}



string CAesCbc::SetEncryptKey(std::string strKey)
{
	int iKeyLen = strKey.length();
	char szBuf[100]="\0";
	if(iKeyLen < 32)
	{
		sprintf(szBuf,"%s",strKey.c_str());
		int i = 0;
		for(i=iKeyLen;i<32;i++)
		{
			szBuf[i]=' ';
		}
		m_strKey = szBuf;

	}

	m_strKey = szBuf;
	return m_strKey;
}

string CAesCbc::SetEncryptIv(std::string strIv)
{
	int iVLen = strIv.length();
	char szVBuf[100]="\0";
	if(iVLen < 16)
	{
		sprintf(szVBuf,"%s",strIv.c_str());
		int i = 0;
		for(i=iVLen;i<16;i++)
		{
			szVBuf[i]=' ';
		}
	}
	m_strIv = szVBuf;
	return m_strIv;
}

bool CAesCbc::EncryptString(std::string strInPut, std::string& strOutPut)
{
	Pkcs7Padding(strInPut);
	unsigned char iv[17] = { 0 };
	memcpy(iv, m_strIv.c_str(), 16);
	int iInputLen = strInPut.length();
	unsigned char *temp = new unsigned char[iInputLen + 1];
	memset(temp, 0, iInputLen + 1);

	AES_KEY aes;
	AES_set_encrypt_key((const unsigned char *)m_strKey.c_str(), 256, &aes);

	const unsigned char* pInput = (const unsigned char*)strInPut.c_str();
	iInputLen = strInPut.length();
	for (int i = 0; i < iInputLen / 16; i++)
	{
		AES_cbc_encrypt(pInput + i * 16, temp + i * 16, 16, &aes, iv, AES_ENCRYPT);
	}
	char *pOutput = new char[iInputLen * 4 / 3 + 3];

	strOutPut = base64_encode(temp,iInputLen);

	delete[] temp;
	delete[] pOutput;

	return true;
}

bool CAesCbc::DecryptString(std::string strInPut, std::string& strOutPut)
{
	unsigned char iv[17] = { 0 };
	memcpy(iv, m_strIv.c_str(), 16);

//	char *pOutStr = new char[strInPut.length()];
	std::string  strOutput = base64_decode(strInPut.c_str(), strInPut.length());

	AES_KEY aes;
	AES_set_decrypt_key((const unsigned char *)m_strKey.c_str(), 256, &aes);

	unsigned char *pDescrypt = new  unsigned char[strOutput.length() + 1];
	memset(pDescrypt, 0, strOutput.length() + 1);
	for (int i = 0; i < strOutput.length() / 16; i++)
	{
		AES_cbc_encrypt((const unsigned char *)strOutput.c_str() + i * 16, pDescrypt + i * 16, 16, &aes, iv, AES_DECRYPT);
	}
	strOutPut = std::string((const char *)pDescrypt);

	Pkcs7UnPadding(strOutPut);
	return true;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

设备系统软件集成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值