加密过程:
#include<iostream>
#include<fstream>
void main(void)
{
char strch,ch;
int i,x;
ifstream readfile;
ofstream writefile;
readfile.open("1.txt",ios::in | ios::nocreate);
if(!readfile)
{
cerr<<"cannot open thie file for input"<<endl;
exit(0); }
writefile.open("2.txt",ios::in | ios::nocreate);
if(!writefile)
{
cerr<<"cannot open the file for output"<<endl;
exit(0); }
while(!readfile.eof())
{
ch=readfile.get();
x=static_cast<int>(ch);
x=(x+3)%128;
strch=static_cast<char>(x);
writefile.put(strch);
}
}
相应的解密过程:
#include<iostream>
#include<string>
#include<fstream>
void main(void){
char strch,ch;
int i,x;
ifstream readfile;
ofstream writefile;
readfile.open("2.txt",ios::in | ios::nocreate);
if(!readfile)
{
cerr<<"cannot open thie file for input"<<endl;
exit(0);
}
writefile.open("3.txt",ios::in | ios::nocreate);
if(!writefile)
{
cerr<<"cannot open the file for output"<<endl;
exit(0);
}
while(!readfile.eof())
{
ch=readfile.get();
x=static_cast<int>(ch);
x=(x-3)%128;
strch=static_cast<char>(x);
writefile.put(strch);
}
}
博客给出了文件输入输出的加密和解密过程代码。加密时从1.txt读取内容,经处理后写入2.txt;解密时从2.txt读取内容,处理后写入3.txt。若文件打开失败会输出错误信息并退出程序。
511

被折叠的 条评论
为什么被折叠?



