⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 加密大全.cpp

📁 这是我们学校课程设计中的文件加解密的课题
💻 CPP
字号:



 


 
 
  
 
 
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
int lb[25];								//全局变量存储加了32的字符位置
char str[40];							//全局变量存储要加密的字符串
int w;									//全局变量作为加密字符串或文件的选择
char filename[35];						 //全局变量存储源文件名 
void menu();							//声明菜单函数
void printtxt(char *filename0);			//声明打印函数
void encode(char *filename1);			//声明加密函数
void decode(char *filename2);			//声明解密函数
struct file *AppendNode(struct file *head,char *filename3); //声明将文件内容存入链表函数

struct password      
{
 char pa[16];							//加密字符串    
 int l;									//加密间隔字节数
}s;

/**********定义全局链表存文件内容*********/
struct file
{
 char data;
 struct file *next;
}*head=NULL,*p=NULL,*p1=NULL;

/*******主函数*********/
void main()
{
 cout<<"**欢迎进入文件夹解密系统**\n";
 int m;
 while(1)
 {
  menu();
  cout<<endl<<"请选择:";
  cin>>m;
  switch(m)
  {
  case 1:
   cout<<"请输入您的密码(1-16个字符):\n";
   cin>>s.pa;
   do{
   cout<<"请您输入大于零加密间隔字节数\n";
   cin>>s.l;
   }while(s.l<=0);
   cout<<endl;
   break;
  case 2:
   cout<<"1.加密字符串\n";
   cout<<"2加密文件\n";
   cout<<"请选择:\n";
   cin>>w;
   switch(w)     //嵌套开关语句
   {
   case 1:		//选择1加密字符串
    {
    int q=0;
    printf("请输入要加密的字符串\n");
    gets(str);
    ofstream fout("strfile.txt");
    while(str[q]!='\0')				//要加密的字符串存入文件
    {
     fout.put(str[q]);
        q++;
    }
    fout.close();
    encode("result1.txt");
   cout<<" 加密后的字符串为:\n";
   printtxt("result1.txt");
    }
   break;

   case 2:   //选择2加密文件内容
    {
   encode("result.txt");
   cout<<"加密前的文件内容为:\n";
   printtxt(filename);
   cout<<"加密后的文件内容为:\n";
   printtxt("result.txt");
   cout<<endl;
    }
     break;
    default:
     {
    cout<<"Input error!\n";
     }
    exit(0);
   }
   break;
  case 3: 
   if(w==2)
   {
   decode("recall.txt");
   cout<<"解密后的文件为:\n";  
   printtxt("recall.txt");
   }
   if(w==1)
   {
    decode("recall1.txt");
    cout<<"解密后的字符串为:\n";
    printtxt("recall1.txt");
   }
   cout<<endl;
   break;
  case 4:
   cout<<"谢谢您的使用\n";
   exit(0);
   break;
  default:
   cout<<"输入有误,请重输\n";
  }
 }
}

 
/*********菜单函数**********/
void menu()
{
 int i;
 for(i=0;i<15;i++)
  cout<<"*";
 cout<<endl;
 cout<<"1.设置加密方法\n";
 cout<<"2.加密\n";
 cout<<"3.解密\n";
 cout<<"4.退出\n";
 for(i=0;i<15;i++)
  cout<<"*";
 
}

/**********打印函数**********/
void printtxt(char *filename0)
{
 char ch;
 ifstream in(filename0);
 if(!in)
 {
  cout<<"Can not open file\n";
  exit(0);
 }
 while(in.get(ch))
 {
  cout<<ch;
 }
 cout<<endl;
 in.close();
}

/**********加密函数************/
void encode(char *filename1)
{
 int i=0,j=0,e=0,f=0;
 if(w==2)
 {
 cout<<"请输入要加密的文件名\n";
 cin>>filename;
 p1=AppendNode(head,filename);        //将文件内容读入链表
 }
 if(w==1)
 {
   p1=AppendNode(head,"strfile.txt");  //将字符串读进链表
 }
 ofstream out(filename1);
 if(!out)
 {
  cout<<"Can not open file\n";
  exit(0);
 }
while(p1!=NULL)
 {
  if((int)(p1->data+s.pa[j])>122)
  {
   p1->data=(int)(p1->data+s.pa[j])%122;      //将字符强制转换为整形数判断是否大于122
   if(p1->data<32)
   {
    p1->data=p1->data+32;
	lb[e]=f;
	e++;
  }
  }
  else
  {
   p1->data=p1->data+s.pa[j];
  }
  j++;
  if(j==(int)strlen(s.pa))
   j=0;
  for(i=0;i<s.l ;i++)
  {
   out.put(p1->data);
   p1=p1->next;
   f++;
  }
 }
 out.close();
}

/***********解密函数**********/
void decode(char *filename2)
{
 char de[16];
 int i=0,j=0,m=0,n=0,e=0,f=0;
 if(w==2)
 {
	p=AppendNode(head,"result.txt");
 }
 if(w==1)
 {
	 p=AppendNode(head,"result1.txt");
 }
 ofstream out(filename2);
 if(!out)
 {
  cout<<"can not open file!\n";
  exit(0);
 }
Lable:
 cout<<"请输入您的密码:\n";
 cin>>de;
 n++;
   if(n>3)
    exit(0);
 
  if(strcmp(s.pa,de)!=0)   //比较密码是否相同
  {
   cout<<"密码错误!\n";
   goto Lable;
  }
 
while(p!=NULL)
 {
  if(f==lb[e]&&(int)(p->data-s.pa[j])<32)  //将字符强制转换为整形数进行判断
  {
   p->data=(int)(p->data-s.pa[j])+122-32;
   e++;
  }
  else if((int)(p->data-s.pa[j])<32)
  {
  p->data=(int)(p->data-s.pa[j])+122;
  }
  else
  {
   p->data=p->data-s.pa[j];
  }
  j++;
  if(j==(int)strlen(s.pa))
   j=0;
  for(i=0;i<s.l ;i++)
  {
   out.put(p->data);
   p=p->next;
   f++;
  }
 }
 out.close();

}

/*****************将文件内容存入链表*****************/
struct file *AppendNode(struct file *head,char *filename3)
  {
   struct file *pp=NULL;
   struct file *pr=head;
   char ch;
   ifstream in(filename3);
   if(!in)
   {
    cout<<"Cannot open\n";
    exit(0);
   }
    while(in.get(ch))
   {
   pp=new struct file;     //新建节点,节点数与文件字符数相等
   if(pp==NULL)
   {
    cout<<"No enough memory to alloc";
    exit(0);
   }
  if(head==NULL)
  {
    head=pp;
  }
  else
  {
  while(pr->next!=NULL)
   {
    pr=pr->next;
   }
   pr->next=pp;
  }
    pr=pp;
   pr->data=ch;
   pr->next=NULL;

   }
   return head;
   in.close();
}

 

 

 

 

  
 
 
 
 

 
  
 

 
 

 

 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -