发信人: sonicln() 
整理人: wenbobo(2002-12-27 15:50:04), 站内信件
 | 
 
 
下面的程序是我刚刚编的.仅把主要函数贴上.
 RSA的程序我正在编,过几天贴上.
 void descode(char *p)是加密程序*p指向一个8byte的字符数组
 为明文.函数调用后将产生8byte的密文.
 void desdecode(char *p)为解密函数.输入密文输出明文.
 
 unsigned char key48[16][6],key[8]={0,0,0,0,0,0,0,0};
 unsigned char sel_table1[]={
         0x38,0x30,0x28,0x20,0x18,0x10,0x08,
         0x00,0x39,0x31,0x29,0x21,0x19,0x11,
         0x09,0x01,0x3a,0x32,0x2a,0x22,0x1a,
         0x12,0x0a,0x02,0x3b,0x33,0x2b,0x23,
 
         0x3e,0x36,0x2f,0x26,0x1e,0x16,0x0e,
         0x06,0x3d,0x35,0x2d,0x25,0x1d,0x15,
         0x0d,0x05,0x3b,0x34,0x2c,0x24,0x1c,
         0x14,0x0c,0x04,0x1b,0x13,0x0b,0x03};
 
 unsigned char sel_table2[]={
         0x0d,0x10,0x0a,0x17,0x00,0x04,0x02,0x1b,
         0x0e,0x05,0x14,0x09,0x16,0x12,0x0b,0x03,
         0x19,0x07,0x0f,0x06,0x1a,0x13,0x0c,0x01,
         0x28,0x33,0x1e,0x24,0x2e,0x36,0x1d,0x27,
         0x32,0x2c,0x20,0x2f,0x2b,0x30,0x26,0x37,
         0x21,0x34,0x2d,0x29,0x31,0x23,0x1c,0x1f};
 
 unsigned char sel_table3[]={
          0x1f,0x00,0x01,0x02,0x03,0x04,0x03,0x04,
          0x05,0x06,0x07,0x08,0x07,0x08,0x09,0x0a,
          0x0b,0x0c,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,
          0x0f,0x10,0x0f,0x10,0x11,0x12,0x13,0x14,
          0x13,0x14,0x15,0x16,0x17,0x18,0x17,0x18,
          0x19,0x1a,0x1b,0x1c,0x1b,0x1c,0x1d,0x1e};
 
 unsigned char sel_table4[8][4][16]={
 	 {{0x0e,0x04,0x0d,0x01,0x02,0x0f,0x0b,0x08,0x03,0x0a,0x06,0x0c,0x05,0 
 x09,0x00,0x07},
 	  {0x00,0x0f,0x07,0x04,0x0e,0x02,0x0d,0x01,0x0a,0x06,0x0c,0x0b,0x09,0 
 x05,0x03,0x08},
 	  {0x04,0x01,0x0e,0x08,0x0d,0x06,0x02,0x0b,0x0f,0x0c,0x09,0x07,0x03,0 
 x0a,0x05,0x00},
 	  {0x0f,0x0c,0x08,0x02,0x04,0x09,0x01,0x07,0x05,0x0b,0x03,0x0e,0x0a,0 
 x00,0x06,0x0d}
 	 },/*   s1   */
 	 {{0x0f,0x01,0x08,0x0e,0x06,0x0b,0x03,0x04,0x09,0x07,0x02,0x0d,0x0c,0 
 x00,0x05,0x0a},
 	  {0x03,0x0d,0x04,0x07,0x0f,0x02,0x08,0x0e,0x0c,0x00,0x01,0x0a,0x06,0 
 x09,0x0b,0x05},
 	  {0x00,0x0e,0x07,0x0b,0x0a,0x04,0x0d,0x01,0x05,0x08,0x0c,0x06,0x09,0 
 x03,0x02,0x0f},
 	  {0x0d,0x08,0x0a,0x01,0x03,0x0f,0x04,0x02,0x0b,0x06,0x07,0x0c,0x00,0 
 x05,0x0e,0x09}
 	 },/*   s2   */
 	 {{0x0a,0x00,0x09,0x0e,0x06,0x03,0x0f,0x05,0x01,0x0d,0x0c,0x07,0x0b,0 
 x04,0x02,0x08},
 	  {0x0d,0x07,0x00,0x09,0x03,0x04,0x06,0x0a,0x02,0x08,0x05,0x0e,0x0c,0 
 x0b,0x0f,0x01},
 	  {0x0d,0x06,0x04,0x09,0x08,0x0f,0x03,0x00,0x0b,0x01,0x02,0x0c,0x05,0 
 x0a,0x0e,0x07},
 	  {0x01,0x0a,0x0d,0x00,0x06,0x09,0x08,0x07,0x04,0x0f,0x0e,0x03,0x0b,0 
 x05,0x02,0x0c}
 	 },/*   s3   */
 	 {{0x07,0x0d,0x0e,0x03,0x00,0x06,0x09,0x0a,0x01,0x02,0x08,0x05,0x0b,0 
 x0c,0x04,0x0f},
 	  {0x0d,0x08,0x0b,0x05,0x06,0x0f,0x00,0x03,0x04,0x07,0x02,0x0c,0x01,0 
 x0a,0x0e,0x09},
 	  {0x0a,0x06,0x09,0x00,0x0c,0x0b,0x07,0x0d,0x0f,0x01,0x03,0x0e,0x05,0 
 x02,0x08,0x04},
 	  {0x03,0x0f,0x00,0x06,0x0a,0x01,0x0d,0x08,0x09,0x04,0x05,0x0b,0x0c,0 
 x07,0x02,0x0e}
 	 },/*   s4   */
 	 {{0x02,0x0c,0x04,0x01,0x07,0x0a,0x0b,0x06,0x08,0x05,0x03,0x0f,0x0d,0 
 x00,0x0e,0x09},
 	  {0x0e,0x0b,0x02,0x0c,0x04,0x07,0x0d,0x01,0x05,0x00,0x0f,0x0a,0x03,0 
 x09,0x08,0x06},
 	  {0x04,0x02,0x01,0x0b,0x0a,0x0d,0x07,0x08,0x0f,0x09,0x0c,0x05,0x06,0 
 x03,0x00,0x0e},
 	  {0x0b,0x08,0x0c,0x07,0x01,0x0e,0x02,0x0d,0x06,0x0f,0x00,0x09,0x0a,0 
 x04,0x05,0x03}
 	 },/*   s5   */
 	 {{0x0c,0x01,0x0a,0x0f,0x09,0x02,0x06,0x08,0x00,0x0d,0x03,0x04,0x0e,0 
 x07,0x05,0x0b},
 	  {0x0a,0x0f,0x04,0x02,0x07,0x0c,0x09,0x05,0x06,0x01,0x0d,0x0e,0x00,0 
 x0b,0x03,0x08},
 	  {0x09,0x0e,0x0f,0x05,0x02,0x08,0x0c,0x03,0x07,0x00,0x04,0x0a,0x01,0 
 x0d,0x0b,0x06},
 	  {0x04,0x03,0x02,0x0c,0x09,0x05,0x0f,0x0a,0x0b,0x0e,0x01,0x07,0x06,0 
 x00,0x08,0x0d}
 	 },/*   s6   */
 	 {{0x04,0x0b,0x02,0x0e,0x0f,0x00,0x08,0x0d,0x03,0x0c,0x09,0x08,0x05,0 
 x0a,0x06,0x01},
 	  {0x0d,0x00,0x0b,0x07,0x04,0x09,0x01,0x0a,0x0e,0x03,0x05,0x0c,0x02,0 
 x0f,0x08,0x06},
 	  {0x01,0x04,0x0b,0x0d,0x0c,0x03,0x07,0x0e,0x0a,0x0f,0x06,0x08,0x00,0 
 x05,0x09,0x02},
 	  {0x06,0x0b,0x0d,0x08,0x01,0x04,0x0a,0x07,0x09,0x05,0x00,0x0f,0x0e,0 
 x02,0x03,0x0c}
 	 },/*   s7   */
 	 {{0x0d,0x02,0x08,0x04,0x06,0x0f,0x0b,0x01,0x0a,0x09,0x03,0x0e,0x05,0 
 x00,0x0c,0x07},
 	  {0x01,0x0f,0x0d,0x08,0x0a,0x03,0x07,0x04,0x0c,0x05,0x06,0x0b,0x00,0 
 x0e,0x09,0x02},
 	  {0x07,0x0b,0x04,0x01,0x09,0x9c,0x0e,0x02,0x00,0x06,0x0a,0x0d,0x0f,0 
 x03,0x05,0x08},
 	  {0x02,0x01,0x0e,0x07,0x04,0x0a,0x08,0x0d,0x0f,0x0c,0x09,0x00,0x03,0 
 x05,0x06,0x0b}
 	 }/*    s8   */
 	};
 unsigned char sel_table5[]={
 	 0x10,0x07,0x14,0x15,0x1d,0x0c,0x1c,0x11,
 	 0x01,0x0f,0x17,0x1a,0x05,0x12,0x1f,0x0a,
 	 0x02,0x08,0x18,0x0e,0x20,0x1b,0x03,0x09,
 	 0x13,0x0d,0x1e,0x06,0x16,0x0b,0x04,0x19};
 
 unsigned char init_tran[]={
          0x39,0x31,0x29,0x21,0x19,0x11,0x09,0x01,
          0x3b,0x33,0x2b,0x23,0x1b,0x13,0x0b,0x03,
          0x3d,0x35,0x2d,0x25,0x1d,0x15,0x0d,0x05,
          0x3f,0x37,0x2f,0x27,0x1f,0x17,0x0f,0x07,
          0x38,0x30,0x28,0x20,0x18,0x10,0x08,0x00,
          0x3a,0x32,0x2a,0x22,0x1a,0x12,0x0a,0x02,
          0x3c,0x34,0x2c,0x24,0x1c,0x14,0x0c,0x04,
          0x3e,0x36,0x2e,0x26,0x1e,0x16,0x0e,0x06};
 
 unsigned char uninit_tran[]={
         0x27,0x07,0x2f,0x0f,0x37,0x17,0x3f,0x1f,
         0x26,0x06,0x2e,0x0e,0x36,0x16,0x3e,0x1e,
         0x25,0x05,0x2d,0x0d,0x35,0x15,0x3d,0x1d,
         0x24,0x04,0x2c,0x0c,0x34,0x14,0x3c,0x1c,
         0x23,0x03,0x2b,0x0b,0x33,0x13,0x3b,0x1b,
         0x22,0x02,0x2a,0x0a,0x32,0x12,0x3a,0x1a,
         0x21,0x01,0x29,0x09,0x31,0x11,0x39,0x19,
         0x20,0x00,0x28,0x08,0x30,0x10,0x38,0x18};
 
 unsigned char cyc[]={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
 
 void cyc_move(unsigned char *p,int n);
 void change1(unsigned char *p,int n);
 void init_transform(unsigned char *p);    /* 64bit to 64bit initialize 
  transform  */
 void uninit_transform(unsigned char *p);  /* 64bit to 64bit uninitiali 
 ze transform*/
 void select1(unsigned char *p1,unsigned char *p2); /* 64bit to 56bit t 
 ransform             */
 void select2(unsigned char *p1,unsigned char *p2); /* 56bit to 48bit t 
 ransform             */
 void select3(unsigned char *p1,unsigned char *p2); /* 32bit to 48bit t 
 ransform             */
 void select4(unsigned char *p1,unsigned char *p2); /* 48bit to 32bit S 
  box transform       */
 void select5(unsigned char *p);
 void mod2add(unsigned char *p1,unsigned char *p2,int byte); /* p1 XOR  
 p2                           */
 void descode(unsigned char *text);        /* DES code                  
             */
 void desdecode(unsigned char *text);      /* DES decode                
             */void decipher()
 void encipher();
 void decipher();
 
 
 void init_transform(unsigned char *p)
 {
  int i,j,m,n,k;
  unsigned char t[8]={0,0,0,0,0,0,0,0};
  for(i=0;i<8;i++)
   for(j=0;j<8;j++)
   {
    k=init_tran[i*8+j];m=k/8;n=k%8;
    if (p[m]&0x01<<n) k=1; else k=0;
    t[i]=t[i]|(k<<j);
   }
  for(i=0;i<8;i++)
   p[i]=t[i];
 }
 
 void uninit_transform(unsigned char *p)
 {
  int i,j,m,n,k;
  unsigned char t[8]={0,0,0,0,0,0,0,0};
  for(i=0;i<8;i++)
   for(j=0;j<8;j++)
   {
    k=uninit_tran[i*8+j];m=k/8;n=k%8;
    if (p[m]&0x01<<n) k=1; else k=0;
    t[i]=t[i]|(k<<j);
   }
  for(i=0;i<8;i++)
   p[i]=t[i];
 }
 
 void select5(unsigned char *p)
 {
  int i,j,m=0,n=0,k;
  unsigned char t[4];
  t[0]=0; t[1]=0; t[2]=0; t[3]=0;
  for(i=0;i<4;i++)
   for(j=0;j<8;j++)
   {
    k=sel_table5[i*8+j];m=k/8;n=k%8;
    if (p[m]&0x01<<n) k=1; else k=0;
    t[i]=t[i]|(k<<j);
   }
  for(i=0;i<4;i++)
   p[i]=t[i];
 }
 
 void select4(unsigned char *p1,unsigned char *p2)
 {
  unsigned char s;
  int i,j,m=0,n=0,k,x,y;
  p2[0]=0x00; p2[1]=0x00; p2[2]=0x00; p2[3]=0x00;
  for(i=0;i<8;i++)
  {
   s=0x00;
   for(j=0;j<6;j++)
   {
    if (p1[m]&(0x01<<n)) k=1;else k=0;
    s=s|(k<<j);
    n++;
    if (n==8) {n=0;m++;}
   }
   x=((s&0x20)>>4)|(s&0x01);
   y=(s&0x1e)>>1;
   k=sel_table4[i][x][y];
   if (i%2) p2[i/2]=p2[i/2]|k; else p2[i/2]=p2[i/2]|(k<<4);
  }
 }
 
 
 
 void mod2add(unsigned char *p1,unsigned char *p2,int byte)
 {
  int i,j,k,l;
  for(i=0;i<byte;i++)
  {
   p1[i]=~p1[i]&p2[i]|p1[i]&~p2[i];
  }
 }
 
 void select1(unsigned char *p1,unsigned char *p2)
 {
  int i,j,m,n,k;
  for(i=0;i<7;i++)
  {
   p2[i]=0x00;
   for(j=0;j<8;j++)
   {
    m=sel_table1[i*8+j]/8;n=sel_table1[i*8+j]%8;
    if (p1[m]&(0x01<<n)) k=1;else k=0;
    p2[i]=p2[i]|(k<<j);
   }
  }
 }
 
 void select2(unsigned char *p1,unsigned char *p2)
 {
  int i,j,m,n,k;
  for(i=0;i<6;i++)
  {
   p2[i]=0x00;
   for(j=0;j<8;j++)
   {
    m=sel_table2[i*8+j]/8;n=sel_table2[i*8+j]%8;
    if (p1[m]&(0x01<<n)) k=1;else k=0;
    p2[i]=p2[i]|(k<<j);
   }
  }
 }
 
 void select3(unsigned char *p1,unsigned char *p2)
 {
  int i,j,m,n,k;
  for(i=0;i<6;i++)
  {
   p2[i]=0x00;
   for(j=0;j<8;j++)
   {
    m=sel_table3[i*8+j]/8;n=sel_table1[i*8+j]%8;
    if (p1[m]&(0x01<<n)) k=1;else k=0;
    p2[i]=p2[i]|(k<<j);
   }
  }
 }
 
 void change1(unsigned char *p,int n)
 {
  unsigned char low[4],high[4];
  high[0]=p[0];
  high[1]=p[1];
  high[2]=p[2];
  high[3]=p[3]&0x0f0;
  high[3]>>=4;
  low[0]=((p[3]&0x0f)<<4)|((p[4]&0x0f0)>>4);
  low[1]=((p[4]&0x0f)<<4)|((p[5]&0x0f0)>>4);
  low[2]=((p[5]&0x0f)<<4)|((p[6]&0x0f0)>>4);
  low[3]=p[6]&0x0f;
  cyc_move(high,n);
  cyc_move(low,n);
  p[0]=high[0];
  p[1]=high[1];
  p[2]=high[2];
  p[3]=high[3]<<4|((low[0]&0x0f0)>>4);
  p[4]=(low[0]&0x0f)<<4|((low[1]&0x0f0)>>4);
  p[5]=(low[1]&0x0f)<<4|((low[2]&0x0f0)>>4);
  p[6]=(low[2]&0x0f)<<4|(low[3]&0x0f);
 }
 
 void cyc_move(unsigned char *p,int n)
 {
  int i,k1,k2;
  for(i=0;i<n;i++)
  {
   k1=p[0]&0x01;
   p[0]>>=1;
   p[0]=p[0]&0x7f;
   k2=p[1]&0x01;
   p[1]>>=1;
   p[1]=p[1]&0x7f;
   p[1]=p[1]|(k1<<7);
   k1=p[2]&0x01;
   p[2]>>=1;
   p[2]=p[2]&0x7f;
   p[2]=p[2]|(k2<<7);
   k2=p[3]&0x01;
   p[3]>>=1;
   p[3]=p[3]&0x7f;
   p[3]=p[3]|(k1<<3);
   p[0]=p[0]|(k2<<7);
  }
 }
 
 void descode(unsigned char *text)
 {
  int i,j;
  unsigned char high[4],low[4],temp[4];
  unsigned char a48[6],b48[6];
  init_transform(text);
  for(i=0;i<4;i++)
  {
   high[3-i]=text[i];
   low[3-i]=text[4+i];
   temp[3-i]=text[4+i];
  }
  for(i=0;i<16;i++)
  {
   select3(low,a48);
   mod2add(a48,key48[i],6);
   select4(a48,low);
   mod2add(low,high,4);
   for(j=0;j<4;j++)
   {
    high[j]=temp[j];
    temp[j]=low[j];
   }
  }
  for(i=0;i<4;i++)
  {
   text[i]=high[3-i];
   text[4+i]=low[3-i];
  }
  uninit_transform(text);
 }
 
 void desdecode(unsigned char *text)
 {
  int i,j;
  unsigned char high[4],low[4],temp[4];
  unsigned char a48[6],b48[6];
  init_transform(text);
  for(i=0;i<4;i++)
  {
   high[3-i]=text[i];
   low[3-i]=text[4+i];
   temp[3-i]=text[i];
  }
 
  for(i=0;i<16;i++)
  {
   select3(high,a48);
   mod2add(a48,key48[15-i],6);
   select4(a48,high);
   mod2add(high,low,4);
   for(j=0;j<4;j++)
   {
    low[j]=temp[j];
    temp[j]=high[j];
   }
  }
  for(i=0;i<4;i++)
  {
   text[i]=high[3-i];
   text[4+i]=low[3-i];
  }
  uninit_transform(text);
 }
 
 
 
 -- ※ 修改:.sonicln 于 May 15 11:02:11 修改本文.[FROM: 202.96.53.231] ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.96.53.231]
  | 
 
 
 |