龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

S-DES

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
下面是我根据《密码编码学与网络安全:原理与实践》(第二版,电子工业出版社)一书中S-DES编的一个与书上对应的S-DES程序,能通过编译,且正确 #include main(){ int i,temp,temp1,a[8],k[10],k1

下面是我根据《密码编码学与网络安全:原理与实践》(第二版,电子工业出版社)一书中S-DES编的一个与书上对应的S-DES程序,能通过编译,且正确

#include

main(){

int i,temp,temp1,a[8],k[10],k1[8],k2[8],t[8],t1[8],t2[8],p4[4],m[8];

   int p10[10]={3,5,2,7,4,10,1,9,8,6};

   int p8[8]={6,3,7,4,8,5,10,9};

   int ip[8]={2,6,3,1,4,8,5,7};

   int ip1[8]={4,1,3,5,7,2,8,6};

   int ep[8]={4,1,2,3,2,3,4,1};

   int s0[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}};

   int s1[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};

   char c='c';

while(c=='c'c=='C'){

  printf("

********************************************

");

  printf("This is the S-DES program for our BOOK !");

  printf("

Please input a[8] 8bit MingWen:");

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

  scanf("%1d",&a[i]);

  /* for(i=0;i<8;i++)

   printf("%1d",a[i]); */

  printf("

Please input k[10] 10bit MiShi:");

  for(i=0;i<10;i++)

  scanf("%1d",&k[i]);

  /* for(i=0;i<10;i++)

   printf("%1d",k[i]); */

/*下面是算K1与K2*/

  for(i=0;i<10;i++)

  t[i]=k[p10[i]-1];

  temp=t[0];

  for(i=0;i<9;i++)

  t[i]=t[i+1];

  t[9]=t[4];

  t[4]=temp;

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

  k1[i]=t[p8[i]-1];

  temp=t[0];

  temp1=t[1];

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

  t[i]=t[i+2];

  t[8]=t[3];

  t[9]=t[4];

  t[3]=temp;

  t[4]=temp1;

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

  k2[i]=t[p8[i]-1];

/*下面是两次f函数运算与ip,ip`,SW得最后密文*/

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

  t[i]=a[ip[i]-1];

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

  t1[i]=t[ep[i]+4-1];

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

  t1[i]=t1[i]^k1[i];

  temp=s0[t1[0]*2+t1[3>[t1[1]*2+t1[2>;

  temp1=s1[t1[4]*2+t1[7>[t1[5]*2+t1[6>;

  p4[0]=temp/2;

  p4[1]=temp%2;

  p4[2]=temp1/2;

  p4[3]=temp1%2;

  for(i=0;i<4;i++)

  t2[i+4]=t1[i]^p4[i];

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

  t1[i]=t2[ep[i]+4-1];

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

  t1[i]=t1[i]^k2[i];

  temp=s0[t1[0]*2+t1[3>[t1[1]*2+t1[2>;

  temp1=s1[t1[4]*2+t1[7>[t1[5]*2+t1[6>;

  p4[0]=temp/2;

  p4[1]=temp%2;

  p4[2]=temp1/2;

  p4[3]=temp1%2;

  for(i=0;i<4;i++)

  t2[i]=t1[i]^p4[i];

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

  m[i]=t2[ip1[i>;

printf("

MiWen Shi : ");

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

  printf("%d",m[i]);

printf("

C For Continue !

Any Else Key To Exit !

");

  c=getch();

}/*END while*/

}

  

精彩图集

赞助商链接