skdlafkdl 2020-05-24 22:34 采纳率: 0%
浏览 152

DES加密解密代码要怎样才能更换除S盒之外的盒子之后成功运行?

我在网上找到一个DES代码,但是在运行之后发现加解密效果都有,但加密结果和网上的不一样,仔细一看代码除了S盒之外都和我所知的盒子不一样,但更改盒子之后,代码加解密出问题,求求大神帮我看看,我该怎么解决?
/*--------------D-E-S-------------------------*/
#include"des.h"
//第一次换位选择
void PosChg1(unsigned char * Key)
{
unsigned char j,TakeBit;
unsigned int BitPos,BytePos;
int i;

for(i=55;i>=0;i--)
{
BitPos=PC1[i]%8; //在字节中的位置
BitPos=7-BitPos;
BytePos=PC1[i]; //所选择的位所在的字节
BytePos/=8;
TakeBit=Key[BytePos]&Position[BitPos]; //获取这一位
for(j=0;j //要放置到的字节
if(i>27)
{
BytePos=i-28;
BytePos/=8;
BData[BytePos]*=2;
BData[BytePos]|=TakeBit;
}
else
{
BytePos=i;
BytePos/=8;
AData[BytePos]*=2;
AData[BytePos]|=TakeBit;
}
}
}

//第二次换位选择
void PosChg2(unsigned char * Cdata,unsigned char * Ddata,unsigned char Num)
{
unsigned char j,TakeBit;
unsigned int BitPos,BytePos;
int i;
for(i=47;i>=0;i--)
{
BitPos=PC2[i]%8;
BytePos=PC2[i];
BytePos/=8;
if(i>27)
{
BytePos-=4;
TakeBit=Ddata[BytePos]&Position[BitPos];
}
else
TakeBit=Cdata[BytePos]&Position[BitPos];
for(j=0;j<BitPos;j++) TakeBit/=2;
BytePos=i;
BytePos/=8;
SubKey[Num][BytePos]*=2;
SubKey[Num][BytePos]|=TakeBit;
//Subkey[BytePos]*=2;
//Subkey[BytePos]|=TakeBit;
}
}

//左移1位
//1 2 3 4 5 6 7 8 <--
void LeftShift(unsigned char * Sdata)
{
unsigned char midchar1,midchar2;
unsigned char i;
midchar1=Sdata[0]&1; //最低位保存
for(i=0;i<3;i++)
{
midchar2=Sdata[i+1]&1;
Sdata[i]/=2;
midchar2*=128;
Sdata[i]|=midchar2;
}
Sdata[3]/=2;
Sdata[i]|=midchar1*8; //最低位放到第28位
}

//生成16个子密钥
void SubKeyG(unsigned char * KeyCode) //生成16个子密钥
{
unsigned char i,j;
PosChg1((unsigned char *)KeyCode);
for(i=0;i<16;i++)
{
for(j=0;j<LShift[i];j++)
{
LeftShift((unsigned char *)AData);
LeftShift((unsigned char *)BData);
}
PosChg2((unsigned char *)AData,(unsigned char *)BData,i);
//WriteI2c((6*i),(unsigned char *)SubKey,6);
}
}

//位置变换
void IPChange(unsigned char * PlText, unsigned char * IPs)
{
unsigned char j,TakeBit;
unsigned int BitPos,BytePos;
int i;
unsigned char IPText[8]={0};

for(i=63;i>=0;i--)
{
BitPos=IPs[i]%8;
BitPos=7-BitPos;
BytePos=IPs[i];
BytePos/=8;
TakeBit=PlText[BytePos]&Position[BitPos];
for(j=0;j<BitPos;j++) TakeBit/=2;
BytePos=i;
BytePos/=8;
IPText[BytePos]*=2;
IPText[BytePos]|=TakeBit;
}

for(i=0;i<8;i++)
PlText[i]=IPText[i];
}

//32位到48位的扩展
void Ex32to48(unsigned char * In32,unsigned char * Out48)
{
unsigned char j,TakeBit;
unsigned int BitPos,BytePos;
int i;

for(i=47;i>=0;i--)
{
BitPos=Exp[i]%8;
BytePos=Exp[i];
BytePos/=8;
TakeBit=In32[BytePos]&Position[BitPos];
for(j=0;j<BitPos;j++) TakeBit/=2;
BytePos=i;
BytePos/=8;
Out48[BytePos]*=2;
Out48[BytePos]|=TakeBit;
}
}

//两个48位异或存入8字节
void Xor2Byte(unsigned char *in1,unsigned char *in2,unsigned char *out)
{
for(i=0;i<6;i++)
out[i]=in1[i]^in2[i];
}

void Ex6to8Byte(unsigned char *out)
{
unsigned int BitPos,BytePos;
unsigned char TakeBit;
unsigned char Cbyte=0;
int i;

for(i=47;i>=0;i--)
{
BitPos=i%8;
BytePos=i/8;
TakeBit=out[BytePos]&Position[BitPos];
TakeBit/=Position[BitPos];
Cbyte*=2;
Cbyte|=TakeBit;
if((i%6)==0)
{
BytePos=i/6;
out[BytePos]=Cbyte;
Cbyte=0;
}
}

}
//S盒运算函数
void SBox(unsigned char * In,unsigned char *Out)
{

unsigned int PosL,PosH;
unsigned char Scode[2];

for(i=0;i<8;i++)
{
if(In[i]&0x20) PosL=1;
else PosL=0;
if(In[i]&0x1) PosL+=2; //S盒的行值

if(In[i]&0x10) PosH=1;
else PosH=0;
if(In[i]&0x8) PosH+=2;
if(In[i]&0x4) PosH+=4;
if(In[i]&0x2) PosH+=8; //S盒的列值

PosL=PosL*16+PosH;
Scode[i%2]=S[i][PosL];
if((i%2)==1)
{
Scode[1]*=16;
Out[i/2]=Scode[0]|Scode[1];
}
}
}

void S_PC(unsigned char * InByte)
{
unsigned int BitPos,BytePos;
unsigned char MidCode;
unsigned char spc[4];
int i;

for(i=0;i {
spc[i]=InByte[i];
InByte[i]=0;
}
for(i=31;i>=0;i--)
{
BitPos=SPc[i]%8; //所选择的位所在的字节
BytePos=SPc[i];
BytePos/=8; //在字节中的位置
MidCode=spc[BytePos]&Position[BitPos]; //获取这一位
MidCode/=Position[BitPos];
BytePos=i; //在字节中的位置
BytePos/=8;
InByte[BytePos]*=2;
InByte[BytePos]|=MidCode;
}
}

void Des(unsigned char * Plant,unsigned char * Chipe)
{
IPChange((unsigned char *)Plant,(unsigned char *)IP); //IP change

for(i=0;i<4;i++) //to L0,R0
{
AData[i]=Plant[i];
BData[i]=Plant[i+4];
}

for(DeiDaiLoop=0;DeiDaiLoop<16;DeiDaiLoop++) //16 times subkey encrypt
{
//ReadI2c((6*i),(unsigned char *)SubKey,6);
Ex32to48((unsigned char *)BData,(unsigned char *)EX48bit); //Rhalf 32bit to 48bit
Xor2Byte((unsigned char *)EX48bit,(unsigned char *)SubKey[DeiDaiLoop],(unsigned char *)SBox48); //XOR 48bit with 48bit subkey
Ex6to8Byte((unsigned char *)SBox48);
SBox((unsigned char *)SBox48,(unsigned char *)SBox32); //S-Box function
S_PC((unsigned char *)SBox32);
for(j=0;j<4;j++)
SBox32[j]^=AData[j];
for(j=0;j<4;j++) //L(i+1)=R(i)
{
AData[j]=BData[j];
BData[j]=SBox32[j];
}
}
for(i=0;i<4;i++)
{
Chipe[i]=AData[i];
Chipe[i+4]=BData[i];
}
IPChange((unsigned char *)Chipe,(unsigned char *)ReIP); //IP(-1) change
}

void DeDes(unsigned char * Plant,unsigned char * Chipe)
{
IPChange((unsigned char *)Plant,(unsigned char *)IP); //IP(-1) change

for(i=0;i<4;i++) //to L0,R0
{
BData[i]=Plant[7-i];
AData[i]=Plant[3-i];
}

for(DeiDaiLoop=15;DeiDaiLoop>=0;DeiDaiLoop--) //16 times subkey encrypt
{
//ReadI2c((6*i),(unsigned char *)SubKey,6);
Ex32to48((unsigned char *)BData,(unsigned char *)EX48bit); //Rhalf 32bit to 48bit
Xor2Byte((unsigned char *)EX48bit,(unsigned char *)SubKey[DeiDaiLoop],(unsigned char *)SBox48); //XOR 48bit with 48bit subkey
Ex6to8Byte((unsigned char *)SBox48);
SBox((unsigned char *)SBox48,(unsigned char *)SBox32); //S-Box function
S_PC((unsigned char *)SBox32);
for(j=0;j<4;j++)
SBox32[j]^=AData[j];
for(j=0;j<4;j++) //L(i+1)=R(i)
{
AData[j]=BData[j];
BData[j]=SBox32[j];
}
}
for(i=0;i<4;i++)
{
Chipe[7-i]=BData[i];
Chipe[3-i]=AData[i];
}
IPChange((unsigned char *)Chipe,(unsigned char *)ReIP); //IP change
}

/*----------D-E-S---------------------*/

void main(void)
{
long int_sys();
SubKeyG(KeyText);
Des(PlantText,Chiper);
DeDes(Chiper,PlantText);

}
des.h
```#define uchar unsigned char
#define uint unsigned int
#define lint long int
#define nop_NOP()

int DeiDaiLoop; //2
//uchar Subkey[6]; //6
unsigned char SubKey[16][6]; //96
unsigned char AData[4]={0}; //4
unsigned char BData[4]={0}; //4
unsigned char EX48bit[6]={0}; //6
unsigned char SBox48[8]={0}; //8
unsigned char SBox32[4]={0}; //4
unsigned char PlantText[8]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}; //8
unsigned char i,j; //2
unsigned char KeyText[8]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}; //8
unsigned char Chiper[8]; //8
//total use:150(SubKey in RAM)
// 60(SubKey store in Exterior)

/*
//生成的16个子密钥,可以作子密钥的计算验证,为的节省RAM,建议将子密钥存到EEPROM中,运算中一个一个的提取。
const uchar SubKey[16][6]={
{ 15, 125, 119, 11, 224, 25},
{ 7, 125, 111, 169, 45, 33},
{ 47, 127, 110, 20, 96, 167},
{103, 239, 78, 88, 23, 225},
{119, 235, 238, 100, 162, 137},
{247, 203, 218, 209, 132, 194},
{244, 203, 223, 99, 195, 0},
{253, 154, 219, 10, 224, 114},
{248, 218, 219, 34, 169, 42},
{252, 158, 187, 144, 37, 55},
{248, 182, 179, 22, 59, 129},
{218, 182, 189, 80, 34, 252},
{187, 181, 181, 241, 154, 1},
{203, 117, 245, 1, 194, 142},
{159, 125, 101, 203, 81, 32},
{143, 125, 116, 128, 65, 122}
};
*/
//初始置换IP
const unsigned char IP[64]={
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,
56, 48, 40, 32, 24, 16, 8, 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
};
//初始置换的逆置换IP-1
const unsigned char ReIP[64]={
32, 0, 40, 8, 48, 16, 56, 24,
33, 1, 41, 9, 49, 17, 57, 25,
34, 2, 42, 10, 50, 18, 58, 26,
35, 3, 43, 11, 51, 19, 59, 27,
36, 4, 44, 12, 52, 20, 60, 28,
37, 5, 45, 13, 53, 21, 61, 29,
38, 6, 46, 14, 54, 22, 62, 30,
39, 7, 47, 15, 55, 23, 63, 31
};
//密钥的第一次选位
unsigned char PC1[56]={
56, 48, 40, 32, 24, 16, 8,
0, 57, 49, 41, 33, 25, 17,
9, 1, 58, 50, 42, 34, 26,
18, 10, 2, 59, 51, 43, 35,
62, 54, 46, 38, 30, 22, 14,
6, 61, 53, 45, 37, 29, 21,
13, 5, 60, 52, 44, 36, 28,
20, 12, 4, 27, 19, 11, 3
};
//密钥的第二次选位
const unsigned char PC2[48]={
13, 16, 10, 23, 0, 4,
2, 27, 14, 5, 20, 9,
22, 18, 11, 3, 25, 7,
15, 6, 26, 19, 12, 1,
44, 55, 34, 40, 50, 58,
33, 43, 54, 48, 36, 51,
47, 52, 42, 59, 37, 56,
49, 45, 53, 39, 32, 35
};

const unsigned char Exp[48]={
31, 0, 1, 2, 3, 4,
3, 4, 5, 6, 7, 8,
7, 8, 9, 10, 11, 12,
11, 12, 13, 14, 15, 16,
15, 16, 17, 18, 19, 20,
19, 20, 21, 22, 23, 24,
23, 24, 25, 26, 27, 28,
27, 28, 29, 30, 31, 0 //0?????
};
//8个S-Box数组
const unsigned char S[8][64]={
{//S1
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
},
{//S2
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
},
{//S3
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
},
{//S4
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
},
{//S5
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
},
{//S6
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
},
{//S7
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
},
{//S8
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
}
};
//S-box结果压缩换位表
const unsigned char SPc[32]={
12, 5, 16, 23, 31, 8, 24, 19,
3, 13, 21, 26, 7, 18, 29, 10,
2, 4, 20, 14, 28, 25, 1, 11,
17, 15, 30, 6, 22, 9, 0, 27
};
//16个字密钥的移位次数
const unsigned char LShift[16]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
//字节中的位位置
const unsigned char Position[8]={1,2,4,8,16,32,64,128};

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2020-09-18 09:08
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug