Qt下图片加密的两种方式

目录

Base64加密

Base64加密

Base64解密 

异或加密

测试

Base64加密

Base64解密 

异或加密 

异或解密 

最后


 

Base64加密

Base64加解密密主要用Qt的两个函数,即

QByteArray QByteArray::toBase64() const
QByteArray QByteArray::fromBase64(const QByteArray & base64) [static]

下面自己封装两个函数,一个加密,一个解密。

Base64加密

/***************************************************************
	FunctionName:	EncryptAndSaveJPG
	Purpose:		加载图片,Base64加密后保存,并返回加密后的图片数据
	Parameter:		
					1 btImage [QByteArray, OUT]
						加密的图片数据
					2 fullPath [QString, IN]
						图片的全路径
	Return:			return true if sucess,else return false
	Remark:      	NULL
****************************************************************/
bool EncryptAndSaveJPG(QByteArray& btImage, QString fullPath)
{
	QFile file(fullPath);
	if (!file.open(QFile::WriteOnly))
	{
		QMessageBox::warning(this, QString::fromLocal8Bit("提示"),
			QString::fromLocal8Bit("不能写文件 %1:\n%2.")
			.arg(fullPath)
			.arg(file.errorString()));
		return false;
	}

	//加密
	QByteArray encryImag = btImage.toBase64();
	file.write(encryImag);
	file.close();
	return true;
}

Base64解密 

/***************************************************************
	FunctionName:	LoadJPGAndDecrpt
	Purpose:		加载图片,Base64解密,并返回解密后的图片数据
	Parameter:		
					1 btImage [QByteArray, OUT]
						解密的图片数据
					2 filefullPath [QString, IN]
						图片的全路径
	Return:			return true if sucess,else return false
	Remark:      	NULL
****************************************************************/
bool LoadJPGAndDecrpt(QByteArray& btImage, QString filefullPath)
{
	QFile file(filefullPath);
	if (!file.open(QFile::ReadOnly))
	{
		QMessageBox::warning(this, QString::fromLocal8Bit("提示"),
			QString::fromLocal8Bit("不能读取文件 %1:\n%2.")
			.arg(filefullPath)
			.arg(file.errorString()));
		return false;
	}

	btImage = QByteArray::fromBase64(file.readAll());
	file.close();
	return true;
}

异或加密

异或加解密一个函数就可以搞定。即将待加密/解密的图片的每一个字节与0xff异或。

/***************************************************************
	FunctionName:	XorEncryAndDecry
	Purpose:		将图片数据与key值异或,key一般取0xff
	Parameter:		
					1 QByteArray [QByteArray, IN]
						待加/解密的图片数据
					2 btImageOut [QByteArray, OUT]
						加/解密后的图片数据
                    3 key [const char, INT]
                        键值
	Return:			NULL
	Remark:      	NULL
****************************************************************/
void XorEncryAndDecry(QByteArray& btImageIn, QByteArray& btImageOut, const char &key)
{
	for (int i = 0; i < btImageIn.size(); i++)
	{
		btImageOut[i] = btImageIn[i] ^ key;
	}
}

测试

下面调用测试一下,可以弹出对话框选取文件。

Base64加密

void encry_Slot()
{
	QString filePath = QFileDialog::getOpenFileName(this);
	if (filePath.isEmpty())
	{
		return;
	}


	QFile read_file(filePath);
	if (!read_file.open(QIODevice::ReadOnly))
	{
		QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("文件读取失败!"));
		return;
	}

	QByteArray btImage = read_file.readAll();
	bool bResult = EncryptAndSaveJPG(btImage, filePath);
	if (bResult)
	{
		QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("加密完成!"));
	}
}

Base64解密 

void decry_Slot()
{
	QString fileName = QFileDialog::getOpenFileName(this);
	QByteArray btImage;
	if (!fileName.isEmpty())
	{
		bool bResult = LoadJPGAndDecrpt(btImage, fileName);
		if (bResult)
		{
			QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("解密完成!"));
		}

		//保存解密后的图片
		QFile file(fileName);
		if (!file.open(QFile::WriteOnly))
		{
			QMessageBox::warning(this, QString::fromLocal8Bit("提示"),
				QString::fromLocal8Bit("不能写文件 %1:\n%2.")
				.arg(fileName)
				.arg(file.errorString()));
			return;
		}

		file.write(btImage);
		file.close();
	}
}

异或加密 

void encryXor_Slot()
{
	QString filePath = QFileDialog::getOpenFileName(this);
	if (filePath.isEmpty())
	{
		return;
	}


	QFile read_file(filePath);
	if (!read_file.open(QIODevice::ReadOnly))
	{
		QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("文件读取失败!"));
		return;
	}

	QByteArray btImage = read_file.readAll();
	QByteArray btImageXor;
	XorEncryAndDecry(btImage, btImageXor, 0xff);
	
	QFile write_file(filePath);
	if (!write_file.open(QFile::WriteOnly))
	{
		QMessageBox::warning(this, QString::fromLocal8Bit("提示"),
			QString::fromLocal8Bit("不能写文件 %1:\n%2.")
			.arg(filePath)
			.arg(write_file.errorString()));
		return;
	}

	//加密
	write_file.write(btImageXor);
	read_file.close();
	write_file.close();
	QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("加密完成!"));

}

异或解密 

void decryXor_Slot()
{
	QString fileName = QFileDialog::getOpenFileName(this);
	if (fileName.isEmpty())
	{
		return;
	}

	QFile file(fileName);
	if (!file.open(QFile::ReadOnly))
	{
		QMessageBox::warning(this, QString::fromLocal8Bit("提示"),
			QString::fromLocal8Bit("不能读取文件 %1:\n%2.")
			.arg(fileName)
			.arg(file.errorString()));
		return;
	}

	QByteArray btImageXor = file.readAll();
	QByteArray btImage;
	XorEncryAndDecry(btImageXor, btImage, 0xff);

	//保存解密后的图片
	QFile write_file(fileName);
	if (!write_file.open(QFile::WriteOnly))
	{
		QMessageBox::warning(this, QString::fromLocal8Bit("提示"),
			QString::fromLocal8Bit("不能写文件 %1:\n%2.")
			.arg(fileName)
			.arg(write_file.errorString()));
		return;
	}


	write_file.write(btImage);
	write_file.close();
	file.close();
	
	QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("解密完成!"));
}

最后

测试发现,解密后的图片与原图大小一致,且每一个字节数据都一致。

测试程序地址:https://download.csdn.net/download/qq_24282081/11295479

mfc下Base64加解密文件请看:

https://blog.csdn.net/qq_24282081/article/details/100527570

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值