文件加密之异或加密(C++实现)

#include <iostream>
#include <fstream>
#include <string.h> 
using namespace std;


//加密函数
void Encryption(char *in_fname,char *out_fname,char *pwd)
{
FILE *in_file,*out_file;
register char ch;
int x;
x=strlen(pwd);
in_file=fopen(in_fname,"rb");//以读的方式打开二进制文件
if(in_file==NULL)//如果打开失败

cout<<"Open file error"; 
exit(1);

out_file=fopen(out_fname,"wb");//以写的方式打开二进制文件
if(out_file==NULL)//如果创建失败
{
cout<<"Create file error";
exit(1);
}


//一下两断注释代码,要选其一进行使用
/*
这一断注释代码的功能是只对文件的前几位加密,可以提高读写速度,主要用于视频、音频文件,因为只要这类文件的前几位一经修改,是无法正常播放的。
ch=fgetc(in_file); 
for(int i=0;i<=x;i++)
{
fputc(ch^pwd[i],out_file); //fputc函数是向文件写入一位字符
ch=fgetc(in_file); //fgetc()函数是得到文件的一个字符,会自动使指针后移一位
}
while(!feof(in_file))
{
fputc(ch,out_file);
ch=fgetc(in_file); 
}
*/




/*
这一断注释代码的功能是对文件的所有位加密,主要用于文本文件。
  while(!feof(in_file))
{
fputc(ch^pwd[i>=x?i=0:i++],out_file);
ch=fgetc(in_file); 
}
*/
fclose(in_file);
fclose(out_file);
}


void main()

char in_fname[30],out_fname[30],pwd[30];
cout<<"Please input file name which you want to be encrypted:"<<endl;//输入想要加密文件的文件名
cin>>in_fname;
cout<<"Please input file name which which will be output:"<<endl;//输出加密后文件的文件名
cin>>out_fname;
cout<<"Please input password:"<<endl;//输入密码
cin>>pwd;
Encryption(in_fname,out_fname,pwd);//调用加密函数
cout<<"Success!"<<endl;

转载于:https://www.cnblogs.com/achao123/p/4518266.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值