- 论坛徽章:
- 0
|
我写的一个异或加密解密的小程序,但是为什么解密的时候多开头比源文件多了一个乱码呢?
#include<fstream>;
#include<iostream>;
#include<iomanip>;
#include<cstdlib>;
using namespace std;
void add_key(char *source_file,char* d_file,char* pwd)
{
char ch;int i=0,t=0;
int j=0;
ifstream fin1(source_file,ios::in|ios::binary);
ofstream fout2(d_file,ios::app|ios::binary);
if(fin1.is_open()&&fout2.is_open())
{
while(pwd[j++]){};
while(fin1.read((char*)&ch,sizeof ch))
// for(int t=0;t<3;t++)
{ //fin1.read((char*)&ch,sizeof ch);
// t++;
ch=(ch^pwd);
if(i==(j-2)) i=0;
// if(t>;1)
fout2.write((char*)&ch,sizeof ch);
}
if(fin1.eof())
fin1.clear();
fin1.close();fout2.close();
}
else
{
cerr<<"Open the file failed!";
exit(1);
}
}
void exact_key(char *source_file,char* d_file,char* pwd)
{
char ch;int i=0,t=0;
int j=0;
ifstream fin1(source_file,ios::in|ios::binary);
ofstream fout2(d_file,ios::app|ios::binary);
if(fin1.is_open()&&fout2.is_open())
{
while(pwd[j++]){};
while(fin1.read((char*)&ch,sizeof ch))
// for(int t=0;t<3;t++)
{ //fin1.read((char*)&ch,sizeof ch);
// t++;
ch=(ch^pwd);
if(i==(j-2)) i=0;
// if(t>;1)
fout2.write((char*)&ch,sizeof ch);
}
if(fin1.eof())
fin1.clear();
fin1.close();fout2.close();
}
else
{
cerr<<"Open the file failed!";
exit(1);
}
}
int main()
{
char source_file[40];
char target_file[20];
// char third_file[20]="g:\\13.txt";
char pwd[8];
// char ch;
cout<<" lease input source_file path you want to keep password:";
cin.get(source_file,40);
while(cin.get()!='\n')
continue;
cout<<" lease input target_file path you want to keep save:";
cin.get(target_file,40);
while(cin.get()!='\n')
continue;
cout<<" lease input the password:";
cin.get(pwd, ;
while(cin.get()!='\n')
continue;//最多八位,多了忽略!
add_key(source_file,target_file,pwd);
exact_key(target_file,third_file,pwd);
return 0;
} |
|