📄 加密解密(快).cpp
字号:
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
const int min=5,chsize=15;
void createcode(char *pass,int lens,int *code,int height,int types,int &sum)
{
int num=int(pow(2,height+1))-1,*temp=new int [num],pos=0;
sum=0;
for(int i=0;i<lens;i++)
{
temp[0]=int(pass[i])*(i+1);
sum+=int(pass[i]);
for(int j=0;j<int(pow(2,height))-1;j++)
{
temp[2*j+1]=int(fabs(cos(temp[j])*10000));
temp[2*j+2]=int(fabs(sin(temp[j])*10000));
if(types==1)
{
int tt=temp[2*j+1];
temp[2*j+1]=temp[2*j+2];
temp[2*j+2]=tt;
}
}
for(j=int(pow(2,height))-1;j<num;j++)
{
code[pos]=temp[j];
pos++;
}
}
}
void transform(int *outch,int *code,int size)
{
int ch[chsize][chsize];
for(int i=0;i<chsize;i++)
for(int j=0;j<chsize;j++)
ch[i][j]=i*chsize+j;
for(i=0;i<size;i++)
{
int high=(code[i]/100)%chsize,low=(code[i]%100)%chsize;
if (high==low)
low=(low+1)%chsize;
if(i%2==0)
{
for(int j=0;j<chsize;j++)
{
int t=ch[high][j];
ch[high][j]=ch[low][j];
ch[low][j]=t;
}
for(j=0;j<chsize;j++)
{
int t=ch[j][high];
ch[j][high]=ch[j][low];
ch[j][low]=t;
}
}
else
{
for(int j=0;j<chsize;j++)
{
int t=ch[high][j];
ch[high][j]=ch[j][low];
ch[j][low]=t;
}
for(j=0;j<chsize;j++)
{
int t=ch[j][high];
ch[j][high]=ch[low][j];
ch[low][j]=t;
}
}
}
for(i=0;i<chsize*chsize;i++)
{
outch[i]=ch[i/chsize][i%chsize];
}
}
int checkfiletype(char file[])
{
int len=int(strlen(file));
if(len>=3&&file[len-1]=='q'&&file[len-2]=='w'&&file[len-3]=='l')
return 1;
return 0;
}
void usage(int type)
{
cout<<" ╭╮ ╭╮"<<endl;
cout<<" ╭═║╮ ╭║═╮"<<endl;
cout<<"╭═║═║═════════════════════║═║═╮"<<endl;
cout<<"╰║═║╯ ╰║═║╯"<<endl;
cout<<" ╰║╯ 欢迎大家学习使用,在传播过程中 ╰║╯"<<endl;
cout<<" ║ 请保留本人版权:华北电力大学计 ║"<<endl;
cout<<" ║ 算机025 yixiu006 qq:327591387 ║"<<endl;
cout<<" ╭║╮ ╭║╮ "<<endl;
cout<<"╭║═║╮ ╭║═║╮"<<endl;
cout<<"╰═║═║═════════════════════║═║═╯"<<endl;
cout<<" ╰═║╯ ╰║═╯"<<endl;
cout<<" ╰╯ ╰╯ "<<endl;
if(type==1)
{
cout<<" 用法:"<<endl;
cout<<"加密:将要加密的文件拖动到这个exe文件上面再按照要求做!"<<endl;
cout<<"解密:也是将要解密的*.lwq文件拖动到这个exe文件上面!"<<endl;
}
}
int checkhead(char file[])
{
int ch[chsize*chsize];
for(int i=0;i<chsize*chsize;i++)
ch[i]=0;
ifstream fin(file,ios::binary);
char ach;
fin.get(ach);
for(int j=0;j<chsize*chsize;j++)
{
if(fin.eof())
{
fin.close();
return 0;
}
else
{
ch[(int(ach)+256)%256]=1;
fin.get(ach);
}
}
int num=0;
for(i=0;i<chsize*chsize;i++)
if(ch[i]==1)
num++;
fin.close();
if(num>=chsize*chsize)
return 1;
return 0;
}
int checkpass(char file[],char pass[])
{
ifstream fin(file,ios::binary);
char ch,filehead[chsize*chsize];
int lens=int(strlen(pass)),height=0,flag=1,size,sum,head[chsize*chsize];
while(flag<=lens)
{
height++;
flag*=2;
}
height=min-height+1;
size=lens*int(pow(2,height));
int *code=new int [size];
createcode(pass,lens,code,height,0,sum);
transform(head,code,size);
fin.get(ch);
for(int i=0;i<chsize*chsize;i++)
{
if(fin.eof())
{
fin.close();
delete []code;
return 0;
}
filehead[i]=ch;
fin.get(ch);
}
int match=1;
for(i=0;i<chsize*chsize&&match;i++)
if(filehead[i]!=char(head[i]))
match=0;
fin.close();
delete []code;
return match;
}
void change(char ch[])
{
int len=int(strlen(ch)),addlwq=1;
if(len<5)
addlwq=1;
else
if(ch[len-1]=='q'&&ch[len-2]=='w'&&ch[len-3]=='l'&&ch[len-4]=='.')
addlwq=0;
else
addlwq=1;
if(addlwq)
{
ch[len]='.';
ch[len+1]='l';
ch[len+2]='w';
ch[len+3]='q';
ch[len+4]='\0';
}
else
ch[len-4]='\0';
}
int checkblank(char path[])
{
for(int i=0;i<int(strlen(path));i++)
if(path[i]==' ')
return 1;
return 0;
}
void del(char path[])
{
char cmd[4]="del";
int length=int(strlen(path));
if(!checkblank(path))
{
char *command=new char[length+5];
for(int i=0;i<3;i++)
command[i]=cmd[i];
command[3]=' ';
for(i=4;i<length+4;i++)
command[i]=path[i-4];
command[i]='\0';
system(command);
delete []command;
}
else
{
char *command=new char[length+7];
for(int i=0;i<3;i++)
command[i]=cmd[i];
command[3]=' ';
command[4]='\"';
for(i=5;i<length+5;i++)
command[i]=path[i-5];
command[i]='\"';
command[i+1]='\0';
system(command);
delete []command;
}
}
void main(int argc,char *argv[])
{
if(argc!=2)
{
usage(1);
cout<<"参数错误!"<<endl;
system("pause");
return;
}
else
{
usage(0);
int len=int(strlen(argv[1])),operation,maxpass=int(pow(2,min+1)),sum=0;
char *newfile=new char[len+5],*pass=new char[maxpass],ch,chbak;
int head[chsize*chsize],tran[chsize*chsize];
strcpy(newfile,argv[1]);
change(newfile);
operation=checkfiletype(argv[1]);
if(operation)
{
if(checkhead(argv[1]))
{
cout<<"请输入解密密码!"<<endl;
cin>>pass;
if(int(strlen(pass))>maxpass||!checkpass(argv[1],pass))
{
cout<<"密码不正确!"<<endl;
delete []newfile;
delete []pass;
system("pause");
return;
}
else
{
int lens=int(strlen(pass)),height=0,flag=1,size;
while(flag<=lens)
{
height++;
flag*=2;
}
height=min-height+1;
size=lens*int(pow(2,height));
int *code=new int [size];
createcode(pass,lens,code,height,1,sum);
transform(tran,code,size);
ifstream fin(argv[1],ios::binary);
ofstream fout(newfile,ios::binary);
for(int i=0;i<chsize*chsize;i++)
fin.get(ch);
int pos=0;
fin.get(chbak);
fin.get(ch);
while(!fin.eof())
{
fout.put(char((int(chbak)-tran[(pos+sum)%(chsize*chsize)])%256));
chbak=ch;
fin.get(ch);
pos=(pos+1)%256;
}
int changed=0;
if(chbak!=char(pos))
{
cout<<"文件内容被手工改动!内容与加密前不一致!"<<endl;
changed=1;
}
else
cout<<"解密成功!"<<endl;
fin.close();
fout.close();
if(changed==0)
del(argv[1]);
delete []newfile;
delete []code;
delete []pass;
system("pause");
return;
}
}
else
{
cout<<"文件头已经被损坏!"<<endl;
delete []newfile;
delete []pass;
system("pause");
return;
}
}
else
{
cout<<"请输入加密密码!"<<maxpass<<"位内,建议6位以上!"<<endl;
cin>>pass;
while(int(strlen(pass))>maxpass)
{
cout<<"密码不符合要求,"<<maxpass<<"位以内!"<<endl;
cin>>pass;
}
ifstream fin(argv[1],ios::binary);
ofstream fout(newfile,ios::binary);
int lens=int(strlen(pass)),height=0,flag=1,size;
while(flag<=lens)
{
height++;
flag*=2;
}
height=min-height+1;
size=lens*int(pow(2,height));
int *code=new int [size];
createcode(pass,lens,code,height,1,sum);
transform(tran,code,size);
createcode(pass,lens,code,height,0,sum);
transform(head,code,size);
for(int i=0;i<chsize*chsize;i++)
fout.put(char(head[i]));
fin.get(ch);
if(fin.eof())
{
cout<<"空文件不需要加密!"<<endl;
fin.close();
fout.close();
del(newfile);
delete []newfile;
delete []pass;
delete []code;
system("pause");
return ;
}
int pos=0;
while(!fin.eof())
{
fout.put(char((int(ch)+tran[(pos+sum)%(chsize*chsize)])%256));
fin.get(ch);
pos=(pos+1)%256;
}
fout.put(char(pos));
fin.close();
fout.close();
del(argv[1]);
delete []newfile;
delete []pass;
delete []code;
cout<<"加密成功!"<<endl;
system("pause");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -