📄 des.cpp
字号:
#include<iostream.h>
#include<math.h>
#include<stdio.h>
/***************************转换*************************************/
//////////////////IP置换-----------明文的第一次置换///////////////////
int ip[65]={0,
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};
/////////////////ip置换2----------L16R16合并后的置换生成最终二进制加密//////
int ip1[65]={0,
40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25
};
//密码
/////////////////pc置换-----------密码的第一次置换56位01/////////////
int pc1[57]={0,
57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4
};
///////////////pc置换2-----------cidi循环合并后的置换///////////////////
int pc2[49]={0,
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
};
//////////////移位位数---------cidi的循环位数////////////////////
int Ls[17]={0,
1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1
};
//f变换
//////////////f变换----e膨胀---------32变为48 ///////////////////
int ex[49]={0,
32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 31
};
/////////////f变换----si盒的输出////////////////////////////////
int s[9][4][16]={
{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,
0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8,
4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0,
15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13
},
{
15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10,
3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5,
0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15,
13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9,
},
{
10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8,
13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1,
13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7,
1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
},
{
7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15,
13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9,
10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4,
3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14
},
{
2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9,
14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6,
4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14,
11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3
},
{
12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11,
10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8,
9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6,
4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13
},
{
4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1,
13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6,
1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2,
6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12
},
{
13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7,
1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2,
7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8,
2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11
}
};
/////////////////////p变换-------s盒输出后的置换,为f变换最终输出////////
int p[33]={0,
16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25
};
/**********************************************子函数**************************************************/
/********************************生成16个子密钥ki*************************************/
void Create_key(char key[],int k[][49]){
//产生二进制密钥
int k1[65]={0}; //k1为二进制密钥
int i,i1,i2,i3; //循环计数器
int temp; //临时数据
i2=1;
for(i3=0;i3<=7;i3++){
temp=key[i3];
for(i1=7;i1>=0;i1--){
k1[i2]=temp/int(pow(2,i1));
temp=temp%int(pow(2,i1));
// cout<<k1[i2];
i2++;
}
//cout<<endl;
}
//pc1置换生成二进制密钥
int k2[57]; //k2位IP置换后的二进制密钥
i2=1;
// cout<<"密钥:"<<endl;
for(i3=0;i3<7;i3++){
for(i1=0;i1<8;i1++){
k2[i2]=k1[pc1[i2]]; //k2为pc1置换后的二进制密钥
// cout<<k2[i2];
i2++;
}
// cout<<endl;
}
//生成c0,d0
// cout<<"c0:"<<endl;
int c[16][29],d[16][29]; //c0,d0为k2分解成的28位01
for(i1=1;i1<=28;i1++){
c[0][i1]=k2[i1];
// cout<<c[0][i1];
}
// cout<<endl;
// cout<<"d0:"<<endl;
for(i1=29;i1<=56;i1++){
d[0][i1-28]=k2[i1];
// cout<<d[0][i1-28];
}
// cout<<endl;
//生成ci,di
for(i=1;i<=16;i++){
// cout<<"c"<<i<<":"<<endl;
for(i1=1;i1<=28;i1++){
c[i][i1]=c[i-1][i1];
}
for(i2=1;i2<=Ls[i];i2++){
for(i1=1;i1<=28;i1++){
c[i][i1-1]=c[i][i1];
// cout<<c[i][i1-1];
}
c[i][28]=c[i][0];
}
// cout<<c[i][28]<<endl;
}
for(i=1;i<=16;i++){
// cout<<"d"<<i<<":"<<endl;
for(i1=1;i1<=28;i1++){
d[i][i1]=d[i-1][i1];
}
for(i2=1;i2<=Ls[i];i2++){
for(i1=1;i1<=28;i1++){
d[i][i1-1]=d[i][i1];
// cout<<d[i][i1-1];
}
d[i][28]=d[i][0];
}
// cout<<d[i][28]<<endl;
}
//生成cidi
int cd[17][57]; //cd为ci,di循环后合并
for(i=1;i<=16;i++){
// cout<<"cd"<<i<<":"<<endl;
for(i1=1;i1<=28;i1++){
cd[i][i1]=c[i][i1];
// cout<<cd[i][i1];
}
// cout<<endl;
for(i1=1;i1<=28;i1++){
cd[i][i1+28]=d[i][i1];
// cout<<cd[i][i1+28];
}
// cout<<endl;
}
//生成ki
for(i=1;i<=16;i++){ //k为16个子密钥,cd经pc2置换
// cout<<"k"<<i<<":"<<endl;
for(i1=1;i1<=48;i1++){
k[i][i1]=cd[i][pc2[i1]];
// cout<<k[i][i1];
}
// cout<<endl;
}
}
/**********************************f变换***********************/
void f(int r[],int k[],int fch[]){
int e[49],i1,i2,i3=1,b[7],x,y,temp,j1,j2=1,sch[33];
//e膨胀
for(i1=1;i1<=48;i1++){ //e为膨胀后的48位
e[i1]=r[ex[i1]];
if(e[i1]==k[i1])
e[i1]=0;
else e[i1]=1;
}
//计算s盒输出
for(i2=0;i2<=7;i2++){
for(i1=1;i1<=6;i1++){
b[i1]=e[i3++];
}
x=b[1]*2+b[6];
y=b[5]+b[4]*2+b[3]*4+b[2]*8;
temp=s[i2][x][y]; //s查找
for(j1=3;j1>=0;j1--){
sch[j2]=temp/int(pow(2,j1)); //sch为s盒的输出
temp=temp%int(pow(2,j1));
// cout<<sch[j2];
j2++;
}
//cout<<endl;
}
//f变换的结果
// cout<<"f1:";
for(i1=1;i1<=32;i1++){
fch[i1]=sch[p[i1]]; //f变换的输出经过p变换
// cout<<fch[i1];
}
// cout<<endl;
}
/*******************************************加密******************************************/
void encryption(char m[],char key[],char sc[]){//m为明文,sc为输出
int m1[65]={0};
int i,i1,i2=1,i3,temp;
for(i3=0;i3<=7;i3++){
temp=m[i3];
if(temp<0)
temp+=256;
// cout<<temp<<",";
for(i1=7;i1>=0;i1--){
m1[i2]=temp/int(pow(2,i1)); //m1为二进制明文
temp=temp%int(pow(2,i1));
// cout<<m1[i2];
i2++;
}
//cout<<endl;
}
//IP置换
int m2[65];
i2=1;
// cout<<"明文:"<<endl;
for(i3=0;i3<8;i3++){
for(i1=0;i1<8;i1++){
m2[i2]=m1[ip[i2]]; //m2为ip转换后的二进制明文
// cout<<m2[i2];
i2++;
}
// cout<<endl;
}
//生成L0,R0
// cout<<"l0:"<<endl;
int l[17][33],r[17][33];
for(i1=1;i1<=32;i1++){
l[0][i1]=m2[i1];
// cout<<l[0][i1];
}
// cout<<endl;
// cout<<"r0:"<<endl;
for(i1=33;i1<=64;i1++){
r[0][i1-32]=m2[i1];
// cout<<r[0][i1-32];
}
// cout<<endl;
//生成Li,Ri
int k[17][49];
Create_key(key,k);
int fch[33];
for(i=1;i<=16;i++){
// cout<<"l"<<i<<":"<<endl;
for(i1=1;i1<=32;i1++){
l[i][i1]=r[i-1][i1];
// cout<<l[i][i1];
}
// cout<<endl;
f(r[i-1],k[i],fch);
// cout<<"r"<<i<<":"<<endl;
for(i1=1;i1<=32;i1++){
if(l[i-1][i1]==fch[i1])
r[i][i1]=0;
if(l[i-1][i1]!=fch[i1])
r[i][i1]=1;
// cout<<r[i][i1];
}
}
//生成二进制密文
int mw[65],mw1[65];
// cout<<"密文:"<<endl;
for(i=1;i<=32;i++){
mw[i]=r[16][i];
// cout<<mw[i];
}
for(i=1;i<=32;i++){
mw[i+32]=l[16][i];
// cout<<mw[i+32];
}
// cout<<endl;
// cout<<"最终二进制密文:"<<endl;
for(i=1;i<=64;i++){
mw1[i]=mw[ip1[i]];
// cout<<mw1[i];
}
// cout<<endl;
i3=1;
// char sc[8];
for(i1=0;i1<8;i1++){
temp=0;
for(i2=7;i2>=0;i2--){
temp+=(mw1[i3++]*pow(2,i2));
}
sc[i1]=temp;
// cout<<sc[i1];
}
}
/*****************************************解密********************************************/
void descryption(char sc[],char key[],char jmw[]){
int i,i1,i2=1,i3,temp;
int mw[65]={0},mw1[65];
for(i3=0;i3<=7;i3++){
temp=sc[i3];
if(temp<0)
temp+=256; //1problem
for(i1=7;i1>=0;i1--){
mw1[i2]=temp/int(pow(2,i1)); //mw1最终二进制,mw为r16l16
temp=temp%int(pow(2,i1));
// cout<<mw1[i2];
i2++;
}
// cout<<endl;
}
for(i1=1;i1<=64;i1++){
mw[ip1[i1]]=mw1[i1];
}
//L16,R16;
int L[17][33],R[17][33];
// cout<<"r16:"<<endl;
for(i1=1;i1<=32;i1++){
R[16][i1]=mw[i1];
// cout<<R[16][i1];
}
// cout<<"r16:"<<endl;
for(i1=1;i1<=32;i1++){
L[16][i1]=mw[i1+32];
// cout<<L[16][i1];
}
// cout<<endl;
//生成Li,Ri
int k[17][49];
Create_key(key,k);
for(i=15;i>=0;i--){
// cout<<"R"<<i<<":"<<endl;
for(i1=1;i1<=32;i1++){
R[i][i1]=L[i+1][i1];
// cout<<R[i][i1];
}
// cout<<endl;
int fch[33];
f(R[i],k[i+1],fch);
// cout<<"L"<<i<<":"<<endl;
for(i1=1;i1<=32;i1++){
if(R[i+1][i1]==fch[i1])
L[i][i1]=0;
if(R[i+1][i1]!=fch[i1])
L[i][i1]=1;
// cout<<L[i][i1];
}
// cout<<endl;
}
//////////////////////////////ip置换前的解密二进制/////////////////////
int jm1[65],jm[65];
for(i1=1;i1<=32;i1++){
jm1[i1]=L[0][i1];
}
for(i1=1;i1<=32;i1++){
jm1[i1+32]=R[0][i1];
}
/////////////////////jm为二进制明文////////////////////
for(i1=1;i1<=64;i1++){
jm[ip[i1]]=jm1[i1];
}
i3=1;
// char jmw[8];
for(i1=0;i1<8;i1++){
temp=0;
for(i2=7;i2>=0;i2--){
temp+=(jm[i3++]*pow(2,i2));
}
jmw[i1]=temp;
// cout<<jmw[i1];
}
// cout<<endl;
}
int main(){
char sc[8];
FILE *fp1,*fp2;
char ch[8],temp;
int i;
int j;char pathin[100],pathout[100],password[9];
cout<<"1.Encrypt a file."<<endl;
cout<<"2.Decrypt a file."<<endl;
cout<<"Please choose 1 to encrypt / 2 to decrypt:";
cin>>j;
if (j==1){
cout<<"Please input the path of the input file:";
cin>>pathin;
cout<<"Please input the path of the output file:";
cin>>pathout;
cout<<"please write down your password:";
cin>>password;
fp1=fopen(pathin,"rb");
if(fp1==NULL){
cout<<"can't open the file"<<endl;
return -1;
}
fp2=fopen(pathout,"wb");
for(i=0;i<8;i++)
ch[i]=' ';
temp=fgetc(fp1);
while(feof(fp1)==0){
for(i=0;i<8;i++){
if(feof(fp1)==0){
ch[i]=temp;
temp=fgetc(fp1);
}
else break;
}
encryption(ch,password,sc);
for(i=0;i<8;i++){
fputc(sc[i],fp2);
}
for(i=0;i<8;i++)
ch[i]=' ';
}
fclose(fp1);
fclose(fp2);
return 0;
}
if (j==2){
cout<<"Please input the path of the input file:";
cin>>pathin;
cout<<"Please input the path of the output file:";
cin>>pathout;
cout<<"please write down your password:";
cin>>password;
fp1=fopen(pathin,"rb");
if(fp1==NULL){
cout<<"can't open the file"<<endl;
return -1;
}
fp2=fopen(pathout,"wb");
temp=fgetc(fp1);
while(feof(fp1)==0){
for(i=0;i<8;i++){
if(feof(fp1)==0){
ch[i]=temp;
temp=fgetc(fp1);
}
else break;
}
descryption(ch,password,sc);
for(i=0;i<8;i++){
fputc(sc[i],fp2);
}
}
fclose(fp1);
fclose(fp2);
return 0;
}
if((j!=1)&(j!=2))
cout<<"you have't chosen the right number,please try again.";
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -