对文本文档加密的c++程序

//
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
#include<stdio.h>

ifstream&operator>>(ifstream&fin,bool&aa)
{
 int a;
 fin>>a;
 if(a==0)
  aa=false;
 else
  aa=true;
 return fin;
}
class  BUFFER
{
protected:
 char Buffer[50];
 BUFFER *next;
 BUFFER *front;
public:
 BUFFER();
 ~BUFFER();
 bool linkto(BUFFER&);
 friend istream&operator>>(istream&,BUFFER&);
 friend ostream&operator<<(ostream&,BUFFER&);
 friend ifstream&operator>>(ifstream&,BUFFER&);
 friend ofstream&operator<<(ofstream&,BUFFER&);
 friend class  makesecorytext;
};
BUFFER::BUFFER()
{
 next=NULL;
 front=NULL;
}
BUFFER::~BUFFER()
{
 BUFFER *This;
 This=this;
 if(This->front==NULL)//只能让第一个成员去释放空间
 {
  while(This->next!=NULL)
  {
   This=This->next;
  }
  while(This->front!=NULL)
  {
   if(This->next!=NULL)
   {
    delete This->next;
   }
   This=This->front;//传到上一个
  }
  if(This->next!=NULL)//删除最后一个
   {
    delete This->next;
   }
 }
}
bool BUFFER::linkto(BUFFER&aa)
{
 next=&aa;
 aa.front=this;
 return true;
}
istream&operator>>(istream&stin,BUFFER&aa)
{
 cout<<"请输入字符串(50个字符以内)"<<endl;
 stin>>setw(49)>>aa.Buffer;
 return stin;
}
ostream&operator<<(ostream&stout,BUFFER&aa)
{
 stout<<aa.Buffer;
 return stout;
}
ifstream&operator>>(ifstream&fin,BUFFER&aa)
{
 fin>>setw(49)>>aa.Buffer;
 return fin;
}
ofstream&operator<<(ofstream&fout,BUFFER&aa)
{
 fout<<aa.Buffer;
 return fout;
}


class  makesecorytext
{
 char filename[20];//加密或解密的文件名
 int scory;//密码,初始化为0
 bool whether;//判断文件是否加密
 BUFFER first;//作为缓冲内存
public:
 makesecorytext();
 ~makesecorytext();
 bool readtext();
 bool secoryorbreaktext(int);//加密或解密
 bool savetext();
 bool display();
};
makesecorytext::makesecorytext()
{
 whether=true;
 filename[0]=0;
 this->scory=0;
}
makesecorytext::~makesecorytext()
{
}
bool makesecorytext::readtext()
{
 BUFFER *This;
 This=&first;
 ifstream fin;
 int i=0;
 system("cls");
 cout<<"/n/n/n/t/t请输入要读取的文件/n/n/t/t";
 cin>>filename;
 fin.open(filename,ios::nocreate);
 if(fin.fail())
 {
  cout<<"/n/n/t/t打开文件失败!!!"<<endl;
  cout<<"/n/t/t";
  system("PAUSE");
  return false;
 }
 if(fin.eof())
 {
  cout<<"/n/n/t/t打开的是空文件!!!/n/n/t/t";
  system("PAUSE");
  fin.close();
  return false;
 }
 while(!fin.eof())
 {
  fin>>(*This);
  if(!fin.eof())
   This->next=new BUFFER();
  else
   break;
  if(This->next==NULL)
  {
   cout<<"/n/n/t/t计算机内存不够,读文件失败!!!"<<endl;
   system("PAUSE");
   fin.close();
   return false;
  }
  This->linkto(*This->next);
  This=This->next;
 }
 fin.close();
 return true;
}
bool makesecorytext::secoryorbreaktext(int se)
{
 char a=0;
 char FIlename[20];
 int i=0,k=0,SECORY;
 BUFFER *This;
 ifstream filein;
 filein.open("secory.txt",ios::nocreate);
 if(filein.fail())
 {
  cout<<"/n/n/t/t无法进行加密解密设置!!!"<<endl;
  system("PAUSE");
  return false;
 }
 This=&this->first;
 if(se==0)//表示加密
 {
  for(;a!='1'&&a!='2';)
  {
   system("cls");
   cout<<"/n/n/t/t请选择加密的强度:/n"
    <<"/n/t/t1:普通加密/t2:密码加密"<<endl;
   a=getch();
   switch(a)
   {
   case '1':scory=0;break;
   case '2':cout<<"/n/t/t请输入口令:"<<endl;//设置加密的密码,
       cin>>scory;break;
   default:cout<<"/n/n/t/t输入有误,重新输入!!!"<<endl;break;
   }
   whether=true;//表示加密了
  }
 }
 if(se==1)//表示开始解密
 {
  cout<<"请输入口令:"<<endl;
  cin>>SECORY;
  for(;!filein.eof();)
  {
   filein>>FIlename>>this->scory;
   filein>>whether;
   cout<<FIlename;
   if(strcmp(FIlename,filename)==0&&SECORY==scory)
   {
    k=1;
    break;
   }
  }
  filein.close();
  if(k==0)
  {
   cout<<"输入的口令不正确或此文件没有加密!!!"<<endl;
   system("PAUSE");
   return false;
  }
  if(whether==false)
  {
   cout<<"文件没有加密!!!"<<endl;
   system("PAUSE");
   return false;
  }
  whether=false;//表示解密了
 }
 if(scory==0)
 {
  
  while((This->next)!=NULL)//开始加密
  {
   for(i=0;i<49&&(This->Buffer[i])!=0;i++)//因为读文件时确保了最后一个元素为字NULL符
   {
    This->Buffer[i]=~(This->Buffer[i]);
   }
   This=This->next;//把指针移到下一个
  }
  for(i=0;i<49&&(This->Buffer[i])!=0;i++)
  {
   This->Buffer[i]=~(This->Buffer[i]);
  }
 }
 else
 {
  a=(this->scory)%255;
  while((This->next)!=NULL)//开始加密
  {
   for(i=0;i<49&&(This->Buffer[i])!=0;i++)//因为读文件时确保了最后一个元素为字NULL符
   {
    This->Buffer[i]=(This->Buffer[i])^a;
   }
   This=This->next;//把指针移到下一个
  }
  for(i=0;i<49&&(This->Buffer[i])!=0;i++)
  {
   This->Buffer[i]=(This->Buffer[i])^a;
  }
  this->whether=true;
 }
 
 if(se==0)
  cout<<"/t/t完成文件加密"<<endl;
 else
  cout<<"/t/t完成文件解密"<<endl;
 system("PAUSE");
 return true;
}
bool makesecorytext::savetext()
{
 ofstream fout,fileout;
 BUFFER *This;
 This=&this->first;
 fileout.open("secory.txt",ios::nocreate,ios::app);
 if(fileout.fail())
 {
  cerr<<"存放密码的文件不存在或出问题,保存文件失败!!!";
  system("PAUSE");
  return false;
 }
 fileout<<endl<<filename<<"  "<<scory<<"   "<<whether;
 fout.open(filename,ios::nocreate);
 if(fout.fail())
 {
  cout<<"打开文件失败,加密失败并可能破坏原文件!!!"<<endl;
  system("PAUSE");
  return false;
 }
 while((This->next)!=NULL)
 {
  fout<<endl<<This->Buffer;
  This=This->next;
 }
 fout<<endl<<This->Buffer;
 cout<<"/t/t文件已保存"<<endl;
 system("PAUSE");
 fout.close();
 fileout.close();
  return true;
}
bool makesecorytext::display()
{
 BUFFER *This;
 This=&this->first;
 if(strlen(This->Buffer)==0)
 {
  cout<<"/t/t没有内容显示!!!"<<endl;
  system("PAUSE");
  return false;
 }
 while(This->next!=NULL)
 {
  cout<<This->Buffer<<endl;
  This=This->next;
 }
 cout<<This->next<<endl;
 cout<<"/n/n/t/t显示了全部内容"<<endl;
 system("PAUSE");
 return true;
}
void main()
{
 char a,cho;
 bool res;
 makesecorytext  filesecory;
 for(;;)
 {
  system("cls");
  cout<<"/n/n/n/t/t*------------------------------------------*/n"
   <<"/t/t*        欢迎进入文件加密解密处理系统      */n"
   <<"/t/t*------------------------------------------*/n"
   <<"/n/t/t*请选择加密还是解密:/n"
   <<"/n/t/t*1:加密   2:解密"
   <<"/n/n/t/t*退出请按回车键"
   <<endl;
  a=getch();
  if(a=='1'||a=='2')
  {
   res=filesecory.readtext();
   for(;;)
   {
    cout<<"/t/t是否显示文件内容(y/n)"<<endl;
    cho=getch();
    if(cho=='Y'||cho=='y')
    {
     filesecory.display();
     break;
    }
    else if(cho=='n'||cho=='N')
     break;
   }
  }
  if(a=='1')
  {
   try
   {
    res=filesecory.secoryorbreaktext(0);
    res=filesecory.savetext();
    if(res==false)
     throw false;
    for(;;)
    {
     cout<<"/t/t是否显示文件内容(y/n)"<<endl;
     cho=getch();
     if(cho=='Y'||cho=='y')
      break;
    else if(cho=='n'||cho=='N')
     break;
    }
   }
   catch(bool=false)
   {
    cout<<"操作出错,无法继续进行!!!"<<endl;
   }
  }
  else if(a=='2')
  {
   filesecory.secoryorbreaktext(1);
   res=filesecory.savetext();
   for(;;)
   {
    cout<<"/t/t是否显示文件内容(y/n)"<<endl;
    cho=getch();
    if(cho=='Y'||cho=='y'||cho=='n'||cho=='N')
     break;
   }
  }
  else if(a==13)
   break;
  else
   cout<<"输入有误!!!"<<endl;
  if(cho=='y'||cho=='Y')
   res=filesecory.display();
 }
 return;
}
 

你可能感兴趣的:(C++,加密,null,System,文档,buffer)