DES算法基于C语言加密解密代码

更新时间:2023-12-31 19:52:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

/* Note:Your choice is C IDE */ #include \#include \

#define uchar unsigned char

/**************************************************************************** ******************************明文转换声明部分*******************************/ //IP1置换表

int IP_1[64]={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};

//IP2逆置换表

int IP_2[64]={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};

//E扩展置换表

int E_case[48]={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};

//S盒压缩

int S1[4][16]={14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,

int 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, 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};

13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9};

int S3[4][16]={10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,

int S4[4][16]={ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,

int S5[4][16]={ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,

int 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}; 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}; 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}; 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};

int S7[4][16]={ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,

int 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}; 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};

//P盒置换

int Permute[32]={ 16, 7, 20, 21, 29, 12, 28, 17,

/****************************************************************************

**************************字节与二进制相互变换部分*************************** *****************************************************************************/ //字节转换成二进制

int ByteToBit(char ch,char bit[8]) {

1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25};

uchar x;

for(x=0;x<8;x++)

{ } return 0; }

//字符串转换成二进制位串

int Char8ToBit64(char ch[8],char bit[64]) { }

//二进制转换成字节

int BitToByte(char bit[8],char *ch) {

uchar x;

for(x=0;x<8;x++) uchar x;

for(x=0;x<8;x++) { } return 0;

ByteToBit(*(ch+x),bit+(x<<3)); *(bit+x)=((ch<>7;

}

{ } return 0;

*ch|=*(bit+x)<<(7-x);

//将二进制串转换成字符串

int Bit64ToChar8(char bit[64],char ch[8]) { }

/*************************************************************************************** ********************************密钥声明部分********************************************/ //密钥PC—1置换表

int PC_1[56]={57, 49, 41, 33, 25, 17, 9,

1, 58, 50, 42, 34, 26, 18, uchar x;

memset(ch,0,8); //把ch[8]全部清零。 for(x=0;x<8;x++) { } return 0;

BitToByte(bit+(x<<3),ch+x);

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置换表

int PC_2[48]={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};

//密钥每次轮回移位表

uchar Left_Move[16]={1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};

/*************************************************************************************

**********************************密钥的生成函数部分************************************** **************************************************************************************/ //PC-1置换函数

int PC1_permute(char pc1[64],char PC1_Buff[56]) { }

//PC密钥左移

int KeyLeftMove(char pcB[56],char Move) {

uchar x;

char Cn[28],Dn[28];

char a_buff,b_buff,c_buff,d_buff; uchar x;

for(x=0;x<56;x++) { } return 0;

PC1_Buff[x]=pc1[PC_1[x]-1];

//**********PC-1密钥56位分离成左右各28位************** memcpy(Cn,pcB,28); memcpy(Dn,pcB+28,28);

//*****************PC密钥左移部分*******************

a_buff=Cn[0]; b_buff=Dn[0];

if(2==Move)

{

c_buff=Cn[1]; d_buff=Dn[1];

}

for(x=0;x<28;x++) {

if(27==x) { } else

if(1==Move) { }

if(2==Move) { }

Cn[26]=a_buff; Cn[27]=c_buff; Dn[26]=b_buff; Dn[27]=d_buff; Cn[27]=a_buff; Dn[27]=b_buff;

}

{ }

Cn[x]=Cn[x+Move]; Dn[x]=Dn[x+Move];

//******************28bit合成56bit******************** memcpy(pcB,Cn,28); memcpy(pcB+28,Dn,28); }

return 0;

//PC-2逆置换函数

int PC2_permut(char pc2[56],char PC2_Buff[48]) { }

//产生16轮的子密钥函数

int Key16Itera(char key[64],char subKeys[16][48])

uchar x;

for(x=0;x<48;x++) { } return 0;

PC2_Buff[x]=pc2[PC_2[x]-1];

{ }

/************************************************************************************ *******************************明文加密函数部分**************************************/ //IP置换

int IP1_permute(char IP1[64]) {

uchar x; char IP1Buff[64]; for(x=0;x<64;x++) uchar x,move; char temp1[56];

PC1_permute(key,temp1);//PC1置换,返回值temp for(x=0;x<16;x++) { } return 0;

move=Left_Move[x];

KeyLeftMove(temp1,move);//移位,返回值temp,56bit PC2_permut(temp1,subKeys[x]);

}

{ }

memcpy(IP1,IP1Buff,64); return 0;

IP1Buff[x]=IP1[IP_1[x]-1];

//32bit经E盒扩展成48bit

int R_Expand48bit(char R[32],char RE[48]) { }

//IP64逆置换

int IP2_permute(char IP2[64]) {

uchar x; char temp2[64]; for(x=0;x<64;x++) uchar x;

for(x=0;x<48;x++) { } return 0;

RE[x]=R[E_case[x]-1];

}

{ }

memcpy(IP2,temp2,64); return 0;

temp2[x]=IP2[IP_2[x]-1];

//经P盒置换

int Pcase(char S8Byte[32]) { }

//经S盒压缩成32bit

int Reduceto32bit(char EKey[48],char S32bit[32]) {

uchar x,Rank,Line; uchar x; char temp3[32]; for(x=0;x<32;x++) { }

memcpy(S8Byte,temp3,32); return 0;

temp3[x]=S8Byte[Permute[x]-1];

int out;

for(x=0;x<8;x++) {

Rank=(EKey[1+6*x]<<3)|(EKey[2+6*x]<<2)|(EKey[3+6*x]<<1)|(EKey[4+6*x]); Line=(EKey[6*x]<<1)|(EKey[5+6*x]); //在S盒寻找需要的数 if(0==x) { out=S1[Line][Rank];} if(1==x) { out=S2[Line][Rank];} if(2==x) { out=S3[Line][Rank];} if(3==x) { out=S4[Line][Rank];} if(4==x) { out=S5[Line][Rank];} if(5==x) { out=S6[Line][Rank];} if(6==x) { out=S7[Line][Rank];} if(7==x) { out=S8[Line][Rank];} //把一位数拆成数四位数

S32bit[4*x+0]=(((char)(out))&0x08)>>3; S32bit[4*x+1]=(((char)(out))&0x04)>>2; S32bit[4*x+2]=(((char)(out))&0x02)>>1; S32bit[4*x+3]=(((char)(out))&0x01);

} return 0;

}

/*******************明文16次迭代,加密,**********************

**********************输入值:mw[64]\\子密钥:miyue[16][48]************************* **********************返回值:data[64]***********************/ int Encryp(char data[64],char Zkey16[16][48]) {

uchar x,y;

char Left[32],Right[32],EKbuff[48]; char SE32bit[32];

IP1_permute(data);//IP置换 memcpy(Left,data,32);//分离32bit memcpy(Right,data+32,32); for(x=0;x<16;x++) {

R_Expand48bit(Right,EKbuff);//E盒扩展 for(y=0;y<48;y++) //与密钥异或 { }

EKbuff[y]^=Zkey16[x][y];

Reduceto32bit(EKbuff,SE32bit);//经S盒压缩32bit,返回值SE32bit Pcase(SE32bit); //经P盒置换,返回值SE32bit for(y=0;y<32;y++) //与左半部分Left 32bit异或

}

}

{ }

memcpy(Left,Right,32); memcpy(Right,SE32bit,32);

SE32bit[y]^=Left[y];

memcpy(data,Right,32);//合成64bit memcpy(data+32,Left,32); IP2_permute(data); //IP逆置换 return 0;

/**********************密文输入,解密,16次迭代************************ **************************输入值:miwen[64]**************************** ***************************输出值:mw[64]******************************/ int Decryp(char miwen[64],char D_Zkey16[16][48]) {

uchar x,y;

char D_Left[32],D_Right[32]; char D_EKbuff[48],D_SE32bit[32]; IP1_permute(miwen); //IP1置换。 memcpy(D_Left,miwen,32);//分离32bit memcpy(D_Right,miwen+32,32);

for(x=16;x>0;x--) //16轮迭代 {

R_Expand48bit(D_Right,D_EKbuff);//E盒扩展 for(y=0;y<48;y++) //与密钥异或 { }

Reduceto32bit(D_EKbuff,D_SE32bit);//经S盒压缩32bit,返回值

D_EKbuff[y]^=D_Zkey16[x-1][y];

D_SE32bit。

for(y=0;y<32;y++) }

memcpy(miwen,D_Right,32); //合成64bit memcpy(miwen+32,D_Left,32); IP2_permute(miwen); //IP逆置换

{ }

memcpy(D_Left,D_Right,32); memcpy(D_Right,D_SE32bit,32);

D_SE32bit[y]^=D_Left[y];

Pcase(D_SE32bit); //经P盒置换,返回值SE32bit

}

return 0;

/*************************************主函数部分*********************************/ int main(void) {

uchar x;

char MingW64bit[64],MingW8Byte[8]; char Key64bit[64],Key8Byte[8]; char Key16[16][48],temp2[64]; char Mybuff[8];

printf(\请输入密钥:\\n\); scanf(\,Key8Byte);

Char8ToBit64(Key8Byte,Key64bit); //密钥字符串置换成二进制串 for(x=0;x<8;x++) //补偿足8位奇偶校验位 { }

memcpy(Key64bit,temp2,64);

Key16Itera(Key64bit,Key16);//生成子密钥

memcpy(temp2+8*x,Key64bit+7*x,7); temp2[x*8+7]=1;

printf(\请输入明文:\\n\); scanf(\,MingW8Byte);

Char8ToBit64(MingW8Byte,MingW64bit);//明文字符串置换成二进制串

/************************************************************************* **************************解密部分**************************************** **************************************************************************/

Encryp(MingW64bit,Key16); //加密函数 printf(\密文二进制串64bit:\); for(x=0;x<64;x++) //打印加密后的明文 {

if(x%8==0) printf(\); printf(\,MingW64bit[x]); }

Bit64ToChar8(MingW64bit,Mybuff); //把密文二进制串转换成字符串 printf(\显示密文\\n\);

printf(\,Mybuff); //把密文字符串打印出来 printf(\);

/************************************************************************

*************************解密部分**************************************** *************************************************************************/

Decryp(MingW64bit,Key16);//解密函数,输入密文MingW64bit,和密钥Key16。

printf(\明文二进制串的64bit:\); for(x=0;x<64;x++)//打印解密后的明文 {

if(x%8==0) printf(\); printf(\,MingW64bit[x]); }

Bit64ToChar8(MingW64bit,Mybuff); //把明文的二进制串转换成字符串 printf(\);

printf(\,Mybuff); //把明文的字符串显示出来 return 0; }

本文来源:https://www.bwwdw.com/article/nzix.html

Top