C++文件加密解密

#include "stdafx.h"
#include <fstream> //fstream类的头文件

//C++有名字空间
using std::fstream;//单独使用fstream这个名字
//using namespace std;//使用名字空间std里面的所有名字


int _tmain(int argc, _TCHAR* argv[])
{
	//1创建文件流对象
	fstream  srcFile;//待加密文件
	fstream  dstFile;//加密后文件

	//2打开文件
	//保存待加密文件名的数组
	char srcFileName[256] = { 0 };
	//待加密文件的指针
	char dstFileName[256] = { 0 };
	printf("请输入待加密文件名:");
	scanf("%s", srcFileName);
	printf("请输入加密后文件名:");
	scanf("%s", dstFileName);
	//打开待加密文件  文件名  读方式  二进制
	srcFile.open(srcFileName, std::ios::in | std::ios::binary);
	打开加密文件  文件名  读方式  二进制
	dstFile.open(dstFileName, std::ios::out | std::ios::binary);

	//3得到待加密文件大小
	//3.1设置文件内容指针到文件末尾
	srcFile.seekg(0, std::ios::end);
	//3.2获得文件指针到文件头都的字节数
	std::streamoff size = srcFile.tellg();
	//3.3设置文件内容指针到文件头
	srcFile.seekg(0, std::ios::beg);
	for (std::streamoff i = 0; i < size; i++)
		dstFile.put(srcFile.get()^0x66);

#if 0
	char temp;
	//4循环读取 加密 写入
	for (std::streamoff i = 0; i < size; i++){
		//读取
		temp = srcFile.get();
		//加密
		temp ^= 0x66;
		//写入
		dstFile.put(temp);
	}
#endif
	//5关闭文件
	srcFile.close();
	dstFile.close();



	while (1);
	return 0;
}


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值