文件加密之异或加密(C++实现)
2015-03-03 19:44
996 查看
#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;
}
#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;
}
相关文章推荐
- 文件加密之异或加密(C++实现)
- 文件加密之异或加密(C++实现)
- C++ 哈夫曼树对文件压缩、加密实现代码
- java实现简单的异或操作(用于文件简单加密)
- C++实现文件简单加密与解密(读取文件中的字符,对字符转换,然后写入,完成加密,解密反之)
- C++:文件加密(凯撒加密、栅栏加密、异或加密四方加密、Base64加密)
- ASP.NET 2.0 中一种新的方法实现对配置文件的加密
- Delphi 实现加密与解密文件
- 在C++中实现文件的读写的基本方法
- C++(STL)实现的修改游戏存档文件的2个相关类
- 用emacs代替dev-c++。几个define轻松实现单文件编译
- 用C/C++实现SMC动态代码加密技术
- 利用TEA算法实现加密密码文件生成与解密
- 用C++实现文件I/O操作
- 使用C++对文件加密的简单例子
- 用C++实现简单的文件I/O操作
- 在C++中实现文件的读写的基本方法
- 用C++实现遍历指定目录下的[指定文件后缀名]文件
- 【头文件】c++实现链表
- 用C++实现简单的文件I/O操作(ifstream,ofstream)