📄 file_encrypt.cpp
字号:
#include <fstream.h>
#include <stdlib.h>
void main(void)
{
ifstream in_file;
ofstream out_file;
ifstream key_file;
char *read_filename = "text.txt";
char *write_filename = "result.txt";
char *key_filename = "key.txt";
char ch;
char ch_key[80];
int i,key_long,state;
key_file.open(key_filename,ios::in);
if(!key_file)
{
cout<<"Error: The file---key.txt open is fail.Check the disk and try again..."<<endl;
exit(1);
}
else
{
cout<<"Reading key.txt......"<<endl;
cout<<"File---key.txt read successful."<<endl<<endl;
cout<<"key.txt = ";
}
key_file.get(ch);
for(i=0; ch>='0'&&ch<='9'&&i<80; i++)
{
ch_key[i]=ch;
key_file.get(ch);
}
if(i==0)
{
cout<<"File---key.txt read error: file's key is empty!"<<endl;
exit(1);
}
else
{
key_long=i;
for(i=0;i<key_long;i++)
cout<<ch_key[i];
}
cout<<endl<<endl;
key_file.close();
in_file.open(read_filename,ios::in);
out_file.open(write_filename,ios::out);
if(!in_file)
{
cout<<"Error: The file---text.txt open is fail.Check the disk and try again..."<<endl;
exit(1);
}
else
{
cout<<"Reading text.txt......"<<endl;
cout<<"File---text.txt read successful."<<endl<<endl;
cout<<"Writing result.txt......"<<endl;
cout<<"File---result.txt write successful."<<endl<<endl;
}
for(i=0; !in_file.eof(); )
{
in_file.get(ch);
if(ch>='0' && ch<='9')
{
if(i==key_long)
i=0;
ch = (char)( ( (int)ch - 48 + ((int)ch_key[i] - 48) ) % 10 + 48 );
i++;
}
else
{
if(ch>='A' && ch<='Z')
{
if(i==key_long)
i=0;
ch = (char)( ( (int)ch - 65 + ((int)ch_key[i] - 48) ) % 26 + 65 );
i++;
}
else
{
if(ch>='a' && ch<='z')
{
if(i==key_long)
i=0;
ch = (char)( ( (int)ch - 97 + ((int)ch_key[i] - 48) ) % 26 + 97 );
i++;
}
}
}
out_file<<ch;
if(out_file.fail())
{
cerr<<"File---result.txt write error!"<<endl;
exit(1);
}
}
in_file.close();
out_file.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -