MFC - 使用 Base64 对图片进行加密解密

1 ++Base64.h

cpp 复制代码
//++Base64.h

#pragma once
#include <string>
using namespace std;

class CBase64
{
public:
public:
	CBase64();
	~CBase64();

	/*编码
	DataByte
	[in]输入的数据长度,以字节为单位
	*/
	std::string Encode(const char* Data, int DataByte);

	/*解码
	DataByte
	[in]输入的数据长度,以字节为单位
	OutByte
	[out]输出的数据长度,以字节为单位,请不要通过返回值计算
	输出数据的长度
	*/
	std::string Decode(const char* Data, int DataByte, int& OutByte);

};

2 //++Base64.cpp

cpp 复制代码
//++Base64.cpp
#include "stdafx.h"
#include "++Base64.h"

CBase64::CBase64()
{

}

CBase64::~CBase64()
{

}

std::string CBase64::Encode(const char* Data, int DataByte)
{
	//编码表
	const char EncodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	//返回值
	string strEncode;
	unsigned char Tmp[4] = { 0 };
	int LineLength = 0;
	for (int i = 0; i<(int)(DataByte / 3); i++)
	{
		Tmp[1] = *Data++;
		Tmp[2] = *Data++;
		Tmp[3] = *Data++;
		strEncode += EncodeTable[Tmp[1] >> 2];
		strEncode += EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];
		strEncode += EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];
		strEncode += EncodeTable[Tmp[3] & 0x3F];
		if (LineLength += 4, LineLength == 76) { strEncode += "\r\n"; LineLength = 0; }
	}
	//对剩余数据进行编码
	int Mod = DataByte % 3;
	if (Mod == 1)
	{
		Tmp[1] = *Data++;
		strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2];
		strEncode += EncodeTable[((Tmp[1] & 0x03) << 4)];
		strEncode += "==";
	}
	else if (Mod == 2)
	{
		Tmp[1] = *Data++;
		Tmp[2] = *Data++;
		strEncode += EncodeTable[(Tmp[1] & 0xFC) >> 2];
		strEncode += EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];
		strEncode += EncodeTable[((Tmp[2] & 0x0F) << 2)];
		strEncode += "=";
	}

	return strEncode;
}

std::string CBase64::Decode(const char* Data, int DataByte, int& OutByte)
{
	//解码表
	const char DecodeTable[] =
	{
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		62, // '+'
		0, 0, 0,
		63, // '/'
		52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
		0, 0, 0, 0, 0, 0, 0,
		0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
		13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
		0, 0, 0, 0, 0, 0,
		26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
		39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
	};
	//返回值
	string strDecode;
	int nValue;
	int i = 0;
	while (i < DataByte)
	{
		if (*Data != '\r' && *Data != '\n')
		{
			nValue = DecodeTable[*Data++] << 18;
			nValue += DecodeTable[*Data++] << 12;
			strDecode += (nValue & 0x00FF0000) >> 16;
			OutByte++;
			if (*Data != '=')
			{
				nValue += DecodeTable[*Data++] << 6;
				strDecode += (nValue & 0x0000FF00) >> 8;
				OutByte++;
				if (*Data != '=')
				{
					nValue += DecodeTable[*Data++];
					strDecode += nValue & 0x000000FF;
					OutByte++;
				}
			}
			i += 4;
		}
		else// 回车换行,跳过
		{
			Data++;
			i++;
		}
	}
	return strDecode;
}

3 Base64DemoDlg.cpp 中加入调用代码,Base64DemoDlg.h 中引入 ++Base64.h 头文件。

cpp 复制代码
//以下是读写图片的调用代码:
bool ReadPhotoFile(std::basic_string<TCHAR> strFileName, std::string &strData)
{
	HANDLE hFile;
	hFile = CreateFile(strFileName.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	if (hFile == INVALID_HANDLE_VALUE)
	{
		return false;
	}

	DWORD dFileSize = GetFileSize(hFile, NULL);
	char * pBuffer = new char[dFileSize + 1];

	if (pBuffer == NULL)
		return false;

	memset(pBuffer, 0, dFileSize);

	DWORD dReadSize(0);
	if (!ReadFile(hFile, pBuffer, dFileSize, &dReadSize, NULL))
	{
		delete[]pBuffer;
		CloseHandle(hFile);
		return false;
	}

	CBase64 base64;
	strData = "";
	strData = base64.Encode((const char*)pBuffer, dReadSize);

	delete[]pBuffer;
	CloseHandle(hFile);
	return true;
}

bool WritePhotoFile(std::basic_string<TCHAR> strFileName, std::string &strData)
{
	HANDLE hFile;
	hFile = CreateFile(strFileName.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	if (hFile == INVALID_HANDLE_VALUE)
	{
		return false;
	}

	CBase64 base64;
	int datalen(0);
	DWORD dwritelen(0);
	std::string strdcode = base64.Decode(strData.data(), strData.size(), datalen);
	if (!WriteFile(hFile, strdcode.data(), datalen, &dwritelen, NULL))
	{
		CloseHandle(hFile);
		return false;
	}
	CloseHandle(hFile);
	return true;
}

4 测试,获取图片路径,读取图片加密得到 imageData,然后将 imageData 解密写入新文件。

cpp 复制代码
//测试
void CBase64DemoDlg::OnBnClickedButtonTest()
{
	//1 获取文本框文件路径
	CString strFilePath;
	GetDlgItemText(IDC_EDIT_FILEPATH, strFilePath);
	//2 图片文件进行 Base64编码,存储到 imageData
	string imageData;
	ReadPhotoFile(strFilePath.GetString(), imageData);

	//3 将 imageData 解码后,生成 新图片
	CString strNewFilePath;
	strNewFilePath = _T("D:\\文件\\456.jpg");
	WritePhotoFile(strNewFilePath.GetString(), imageData);

	//其他
	/*
	CString 转 std::wstring
	std::wstring str = filename.GetString();

	std::wstring 转 CString
	CString str( filename.c_str() );
	*/
}

5 效果

参考:

C++读写图片数据转成Base64格式的一种方法

https://www.cnblogs.com/jeray/p/8746976.html

C++读写图片数据转成Base64格式

https://www.cnblogs.com/chechen/p/10417424.html

CString和wstring互转

https://blog.csdn.net/tg2003/article/details/4333130

相关推荐
yudiandian20142 小时前
MFC - Picture Control 控件显示图片
c++·mfc
我是李武涯7 小时前
从`std::mutex`到`std::lock_guard`与`std::unique_lock`的演进之路
开发语言·c++
卡提西亚7 小时前
C++笔记-10-循环语句
c++·笔记·算法
亮剑20188 小时前
第1节:C语言初体验——环境、结构与基本数据类型
c++
William_wL_8 小时前
【C++】类和对象(下)
c++
William_wL_9 小时前
【C++】内存管理
c++
星星火柴9369 小时前
笔记 | C++面向对象高级开发
开发语言·c++·笔记·学习
悲伤小伞10 小时前
Linux_Socket_UDP
linux·服务器·网络·c++·网络协议·udp
八个程序员11 小时前
自定义函数(C++)
开发语言·c++·算法