对文本文档加密的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;
}
 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
祖冲之密码是一种古老的加密算法,也称为“替换加密”或“移位加密”。它的基本原理是将明文中的每个字母替换为一个固定数量的字母,通常是将字母表中的字母向右或向左移动几个位置。以下是一个简单的C++程序,可以用来加密和解密文本文件,使用祖冲之密码。 ```c++ #include <iostream> #include <fstream> #include <string> using namespace std; // 加密函数 void encrypt(string &str, int shift) { int n = str.length(); for (int i = 0; i < n; i++) { if (isalpha(str[i])) { str[i] = toupper(str[i]); // 转换为大写字母 str[i] = ((str[i] - 'A') + shift) % 26 + 'A'; // 移位加密 } } } // 解密函数 void decrypt(string &str, int shift) { int n = str.length(); for (int i = 0; i < n; i++) { if (isalpha(str[i])) { str[i] = toupper(str[i]); // 转换为大写字母 str[i] = ((str[i] - 'A') - shift + 26) % 26 + 'A'; // 移位解密 } } } int main() { string input_file, output_file, line, mode; int shift; cout << "请输入要加密或解密的文本文件名:"; cin >> input_file; cout << "请输入加密或解密的位移量:"; cin >> shift; cout << "请输入要执行的操作,加密或解密:"; cin >> mode; if (mode == "加密") { output_file = "encrypted_" + input_file; } else if (mode == "解密") { output_file = "decrypted_" + input_file; } else { cout << "输入错误,请重新输入!" << endl; return 0; } ifstream fin(input_file); ofstream fout(output_file); while (getline(fin, line)) { if (mode == "加密") { encrypt(line, shift); } else if (mode == "解密") { decrypt(line, shift); } fout << line << endl; } fin.close(); fout.close(); if (mode == "加密") { cout << "加密完成,结果保存在 " << output_file << " 中。" << endl; } else if (mode == "解密") { cout << "解密完成,结果保存在 " << output_file << " 中。" << endl; } return 0; } ``` 这个程序可以接受用户输入要加密或解密的文本文件名、位移量和操作类型(加密或解密),并将结果保存在一个新的文件中。例如,如果要将名为“input.txt”的文件加密,并将结果保存在“encrypted_input.txt”中,可以在控制台中输入以下命令: ``` 请输入要加密或解密的文本文件名:input.txt 请输入加密或解密的位移量:3 请输入要执行的操作,加密或解密:加密 加密完成,结果保存在 encrypted_input.txt 中。 ``` 如果要解密该文件,可以将操作类型改为“解密”,并再次运行程序
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值