DES一轮加密(C语言)
DES一轮加密算法,假设8个S盒一样,都为S1盒。输出每一步
的结果。
已知R0=00000000 11111111 00000110 10000011,
子密钥K1=00111101 10001111 11001101 00110111 00111111 00000110
实现E盒扩展、与轮密钥异或加、S盒代换(8个一样的S盒,都为S1盒)和P盒置换。#include<stdio.h>
#include<string.h>
int R0[32]={0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,0,1,0,0,0,0,0,1,1};
int K1[48]={0,0,1,1,1,1,0,1,1,0,0,0,1,1,1,1,1,1,0,0,1,1,0,1,0,0,1,1,0,1,1,1,0,0,1,1,1,1,1,1,0,0,0,0,0,1,1,0};
int E[48];
int S[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}};
int P[32]={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};int i,j;
int R1[48],R2[32],R3[32];int kuozhan_E()
{int a=1,b=2,c=3,d=4;
int count0=1;
int count1=1;for(i=0;i<48;i=i+6)
{
E=R0[(4*count0+27)%32];
count0++;
}for(i=5;i<48;i=i+6)
{
E=R0[(4*count1+32)%32];
count1++;
}for(i=0;i<32;i++)
{
if(i%4==0)
{
E[a]=R0;
a=a+6;
}else if(i%4==1)
{
E=R0;
b=b+6;
}else if(i%4==2)
{
E[c]=R0;
c=c+6;
}else if(i%4==3)
{
E[d]=R0;
d=d+6;
}
}
for(i=0;i<48;i++)
{
printf("%d",E);
if(i%6==5)
printf("n");
}
}int yihuo()
{
for(j=0;j<48;j++)
{
R1[j]=E[j]^K1[j];
printf("%d",R1[j]);
if(j%6==5)
printf("n");
}}
int S_bianhuan()
{
int h,l,n=0; //h l表示行列
int p,q,k=0,S1[8];
printf("s盒代换十进制:n");
for(p=0;p<47;p+=6)
{
h=1*R1[p+5]+2*R1[p];
l=8*R1[p+1]+4*R1[p+2]+2*R1[p+3]+1*R1[p+4];
S1[k]=S[h][l];
printf("%d ",S1[k]);
int x;
x=n+3;
for(int i=0;i<4;i++)
{
R2[x]=S1[k]%2;
S1[k]/=2;
x--;
n++;
}
k++;
}
printf("n");
printf("s盒代换结果:n");
for(int j=0;j<32;j++)
{
printf("%d ",R2[j]);
if(j%4==3)
printf("n");
}
}
int P_bianhuan()
{
for (i=0;i<32;i++)
{
R3=R2[P-1];
printf("%d ",R3);
if(i%4==3)
printf("n");
}
}
int main()
{
printf("扩展En");
kuozhan_E();printf("扩展E变换后与K1异或结果:n");
yihuo();
printf("S盒变换:n");
S_bianhuan();
printf("P盒变换:n");
P_bianhuan();
}
补充:综合编程 , 安全编程 ,