📄 des2.cpp
字号:
// DES2.cpp : implementation file
//
#include "stdafx.h"
#include "secure.h"
#include "DES2.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <dos.h>
#include<math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// DES2
DES2::DES2()
{ keytext1[0]=0;
keytext2[0]=0;
plaintext[0]=0;
keytext[0]=0;
encryption[0]=0;
decryption[0]=0;
}
DES2::~DES2()
{
}
BEGIN_MESSAGE_MAP(DES2, CAnimateCtrl)
//{{AFX_MSG_MAP(DES2)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// DES2 message handlers
//Initialize the permutation IP
unsigned int DES2::IP[]={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};
unsigned int DES2::IP_1[]={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};
unsigned int DES2::E[49]={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,1};
unsigned int DES2::P[33]={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};
unsigned int DES2::s1[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};
unsigned int DES2::s2[4][16]={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};
unsigned int DES2::s3[4][16]={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};
unsigned int DES2::s4[4][16]={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};
unsigned int DES2::s5[4][16]={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};
unsigned int DES2::s6[4][16]={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};
unsigned int DES2::s7[4][16]={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};
unsigned int DES2::s8[4][16]={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};
void DES2::plaintextTobin()
{
unsigned char ch;
int i,j,k,str[8];
k=1;
for(i=0;i<8;i++)
{
ch=plaintext[i];
for(j=0;j<8;j++)
{
str[j]=ch%2;
ch=ch/2;
}
for(j=7;j>=0;j--)
{
m[k++]=str[j];
}
}
}
void DES2::keytextTobin()
{
unsigned char ch;
int i,j,l,str[8];
l=1;
for(i=0;i<8;i++)
{
ch=keytext[i];
for(j=0;j<8;j++)
{
str[j]=ch%2;
ch=ch/2;
}
for(j=7;j>=0;j--)
{
k[l]=str[j];
if(l%8==7)l++;
l++;
}
}
}
void DES2::initial()//生成密钥的16个子密钥
{
int i,j,k0[57],C0[57];
int C[17][49],D[17][49];
int PC_1[57]={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};
int PC_2[49]={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};
keytextTobin();
//生成子密钥
for(i=1;i<57;i++)
k0[i]=k[PC_1[i-1]];
for(i=1;i<29;i++)
C[0][i]=k0[i];
for(i=29;i<=56;i++)
D[0][i-28]=k0[i];
for(j=1;j<=16;j++)
{
for(i=1;i<28;i++)//循环左移一位
{
C[j][i]=C[j-1][i+1];
D[j][i]=D[j-1][i+1];
}
C[j][28]=C[j-1][1];
D[j][28]=D[j-1][1];
for(i=1;i<=28;i++)
{
C0[i]=C[j][i];
C0[i+28]=D[j][i];
}
//生成子密钥k
for(i=1;i<=48;i++)
K[j][i]=C0[PC_2[i-1]];
}
}
void DES2::round(int *L1,int *R1,int *L2,int *R2,int Key[][49],int m )
{
int RE1[49];
int i,s11[7],s21[7],s31[7],s41[7],s51[7],s61[7],s71[7],s81[7];
int f[33],j;
int s[9],frk[33],temp[5];
for(i=1;i<=48;i++)//经过E变换扩充,由32位变为48位
RE1[i]=R1[E[i-1]];
for(i=1;i<=48;i++)//异或
{
RE1[i]=RE1[i]+Key[m][i];
if(RE1[i]==2) RE1[i]=0;
}
for(i=1;i<7;i++)//48位分成8组
{
s11[i]=RE1[i];
s21[i]=RE1[i+6];
s31[i]=RE1[i+12];
s41[i]=RE1[i+18];
s51[i]=RE1[i+24];
s61[i]=RE1[i+30];
s71[i]=RE1[i+36];
s81[i]=RE1[i+42];
}//下面经过S盒,得到8个数
s[1]=s1[s11[6]+s11[1]*2][s11[5]+s11[4]*2+s11[3]*4+s11[2]*8];
s[2]=s2[s21[6]+s21[1]*2][s21[5]+s21[4]*2+s21[3]*4+s21[2]*8];
s[3]=s3[s31[6]+s31[1]*2][s31[5]+s31[4]*2+s31[3]*4+s31[2]*8];
s[4]=s4[s41[6]+s41[1]*2][s41[5]+s41[4]*2+s41[3]*4+s41[2]*8];
s[5]=s5[s51[6]+s51[1]*2][s51[5]+s51[4]*2+s51[3]*4+s51[2]*8];
s[6]=s6[s61[6]+s61[1]*2][s61[5]+s61[4]*2+s61[3]*4+s61[2]*8];
s[7]=s7[s71[6]+s71[1]*2][s71[5]+s71[4]*2+s71[3]*4+s71[2]*8];
s[8]=s8[s81[6]+s81[1]*2][s81[5]+s81[4]*2+s81[3]*4+s81[2]*8];
for(i=0;i<8;i++)//8个数变换输出二进制
{
for(j=1;j<5;j++)
{
temp[j]=s[i+1]%2;
s[i+1]=s[i+1]/2;
}
for(j=1;j<5;j++)
f[4*i+j]=temp[5-j];
}
for(i=1;i<33;i++)//经过P变换
frk[i]=f[P[i-1]];
for(i=1;i<33;i++)
L2[i]=R1[i];
//与f(Ri-1,k)按位作不进位加法运算
for(i=1;i<33;i++)
{
R2[i]=L1[i]+frk[i];
if(R2[i]==2)R2[i]=0;
}
}
void DES2::endes()
{
int m1[65];
int i,n=1, t[65];
plaintextTobin();
for(i=1;i<=64;i++)
m1[i]=m[IP[i-1]];
for(i=1;i<33;i++)
L0[i]=m1[i];//明文左侧的初始化
for(i=33;i<=64;i++)
R0[i-32]=m1[i];//明文右侧的初始化
for(i=1;i<=8;i++)//16 time
{
round(L0,R0,L1,R1,K,n++);
round(L1,R1,L0,R0,K,n++);
}
for(i=1;i<=32;i++)
{
t[i]=R0[i];
t[i+32]=L0[i];// L16[i]=R15[i];
}
for(i=1;i<=64;i++)
t1[i]=t[IP_1[i-1]];
// printf("\n\nPlain text:\t%s",plaintext);
encryption[0]=0;
for(i=1;i<=64;i++)
{
sprintf(buf,"%c",(char)(48+m[i]));
strcat(encryption,buf);
if(i%8==0)strcat(encryption," ");
}
// printf("\nPlain text:\n%s\n",encryption);
// printf("\n\nKey text:\t%s",keytext);
encryption[0]=0;
for(i=1;i<=64;i++)
{
sprintf(buf,"%c",(char)(48+k[i]));
strcat(encryption,buf);
if(i%8==0)strcat(encryption," ");
}
// printf("\nKey text:\n%s\n",encryption);
encryption[0]=0;
for(i=1;i<=64;i++)
{
sprintf(buf,"%c",(char)(48+t1[i]));
strcat(encryption,buf);
if(i%8==0)strcat(encryption," ");
}
int j=0,ch=0;
encryption[0]=0;
for(i=0;i<=7;i++)
{
for(j=8;j>=1;j--)
{
ch=ch+t1[i*8+j]*(int)pow(2,8-j);
}
sprintf(buf,"%c",(char)ch);
strcat(encryption,buf);
ch=0;
}
// printf("\n\nEncryption result:\t%s\n",encryption);
}
void DES2::dedes()
{
int i,j,n=16, t[65];
int ch=0;
for(i=1;i<=64;i++)
{
t[i]=t1[IP[i-1]];
}
for(i=1;i<33;i++)
{
L0[i]=t[i];
R0[i]=t[i+32];
}
for(i=1;i<=8;i++)//16 time
{
round(L0,R0,L1,R1,K,n--);
round(L1,R1,L0,R0,K,n--);
}
for(i=1;i<=32;i++)
{
t[i]=R0[i];
t[i+32]=L0[i];
}
for(i=1;i<=64;i++)
t1[i]=t[IP_1[i-1]];
for(i=0;i<=7;i++)
{
for(j=8;j>=1;j--)
{
ch=ch+t1[i*8+j]*(int)pow(2,8-j);
}
sprintf(buf,"%c",(char)ch);
strcat(decryption,buf);
ch=0;
}
}
char *DES2::endes2()
{
int i;
encryption[0]=0;
/* for(i=0;i<=9;i++)
keytext[i]=keytext1[i];
initial();
endes();
for(i=0;i<=9;i++)
plaintext[i]=encryption[i];*/
encryption[0]='\0';
for(i=0;i<=9;i++)
keytext[i]=keytext1[i];
initial();
endes();
return encryption;
}
char *DES2::dedes2()
{int i;
decryption[0]=0;
for(i=0;i<=9;i++)
keytext[i]=keytext1[i];
initial();
dedes();
/* for(i=0;i<=9;i++)
encryption[i]=decryption[i];
decryption[0]=0;
for(i=0;i<=9;i++)
keytext[i]=keytext1[i];
initial();
dedes();*/
return decryption;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -