crypt.c

来自「详细说明:毕业论文中关于小型宾馆管理系统的详细设计毕 业论文中关于小型宾馆...」· C语言 代码 · 共 688 行 · 第 1/2 页

C
688
字号
    clearmem((char*)eperm32tab, sizeof(eperm32tab));    for(bit = 0; bit < 48; bit++) {      unsigned mask1,comes_from;	      comes_from = perm32[esel[bit]-1]-1;      mask1      = bytemask[comes_from % 8];	      for(j = 256; j--;) {	if(j & mask1)	  eperm32tab[comes_from / 8][j][bit / 24] |= BITMASK(bit % 24);      }    }        /*      * Create the sb tables:     *     * For each 12 bit segment of an 48 bit intermediate     * result, the sb table precomputes the two 4 bit     * values of the sbox lookups done with the two 6     * bit halves, shifts them to their proper place,     * sends them through perm32 and finally E expands     * them so that they are ready for the next     * DES round.     *     */    for(sg = 0; sg < 4; sg++) {      int j1, j2;      int s1, s2;          for(j1 = 0; j1 < 64; j1++) {	s1 = s_lookup(2 * sg, j1);	for(j2 = 0; j2 < 64; j2++) {	  unsigned to_permute, inx;    	  s2         = s_lookup(2 * sg + 1, j2);	  to_permute = ((s1 << 4)  | s2) << (24 - 8 * sg);	  inx = ((j1 << 6)  | j2) << 1;	  sb[sg][inx  ]  = eperm32tab[0][(to_permute >> 24) & 0xff][0];	  sb[sg][inx+1]  = eperm32tab[0][(to_permute >> 24) & 0xff][1];	  sb[sg][inx  ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0];	  sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1];  	  sb[sg][inx  ] |= eperm32tab[2][(to_permute >>  8) & 0xff][0];	  sb[sg][inx+1] |= eperm32tab[2][(to_permute >>  8) & 0xff][1];	  sb[sg][inx  ] |= eperm32tab[3][(to_permute)       & 0xff][0];	  sb[sg][inx+1] |= eperm32tab[3][(to_permute)       & 0xff][1];	}      }    }      /*      * Create an inverse matrix for esel telling     * where to plug out bits if undoing it     */    for(bit=48; bit--;) {      e_inverse[esel[bit] - 1     ] = bit;      e_inverse[esel[bit] - 1 + 32] = bit + 48;    }    /*      * create efp: the matrix used to     * undo the E expansion and effect final permutation     */    clearmem((char*)efp, sizeof efp);    for(bit = 0; bit < 64; bit++) {      int o_bit, o_long;      unsigned word_value, mask1, mask2;      int comes_from_f_bit, comes_from_e_bit;      int comes_from_word, bit_within_word;      /* See where bit i belongs in the two 32 bit long's */      o_long = bit / 32; /* 0..1  */      o_bit  = bit % 32; /* 0..31 */      /*        * And find a bit in the e permutated value setting this bit.       *       * Note: the e selection may have selected the same bit several       * times. By the initialization of e_inverse, we only look       * for one specific instance.       */      comes_from_f_bit = final_perm[bit] - 1;         /* 0..63 */      comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */      comes_from_word  = comes_from_e_bit / 6;        /* 0..15 */      bit_within_word  = comes_from_e_bit % 6;        /* 0..5  */      mask1 = longmask[bit_within_word + 26];      mask2 = longmask[o_bit];      for(word_value = 64; word_value--;) {	if(word_value & mask1)	  efp[comes_from_word][word_value][o_long] |= mask2;      }    }    initialized++;  }/*  * Process the elements of the sb table permuting the * bits swapped in the expansion by the current salt. */static void shuffle_sb(k, saltbits)  unsigned long *k;  unsigned saltbits;  { unsigned j;    unsigned long x;    for(j=4096; j--;) {      x = (k[0] ^ k[1]) & (unsigned long)saltbits;      *k++ ^= x;      *k++ ^= x;    }  }/*  * Setup the unit for a new salt * Hopefully we'll not see a new salt in each crypt call. */static char current_salt[3] = "&&"; /* invalid value */static unsigned current_saltbits = 0;static int direction = 0;static void setup_salt(s)  char *s;  { unsigned i, j, saltbits;    if(!initialized)      init_des();    if(s[0] == current_salt[0] && s[1] == current_salt[1])      return;    current_salt[0] = s[0]; current_salt[1] = s[1];    /*      * This is the only crypt change to DES:     * entries are swapped in the expansion table     * according to the bits set in the salt.     */    saltbits = 0;    for(i = 0; i < 2; i++) {      long c=ascii_to_bin(s[i]);      if(c < 0 || c > 63)	c = 0;      for(j = 0; j < 6; j++) {	if((c >> j) & 0x1)	  saltbits |= BITMASK(6 * i + j);      }    }    /*     * Permute the sb table values     * to reflect the changed e     * selection table     */    shuffle_sb(ufc_sb0, current_saltbits ^ saltbits);     shuffle_sb(ufc_sb1, current_saltbits ^ saltbits);    shuffle_sb(ufc_sb2, current_saltbits ^ saltbits);    shuffle_sb(ufc_sb3, current_saltbits ^ saltbits);    current_saltbits = saltbits;  }static void ufc_mk_keytab(key)  char *key;  { unsigned v1, v2, *k1;    int i;    unsigned long v, *k2 = &ufc_keytab[0][0];    v1 = v2 = 0; k1 = &do_pc1[0][0][0];    for(i = 8; i--;) {      v1 |= k1[*key   & 0x7f]; k1 += 128;      v2 |= k1[*key++ & 0x7f]; k1 += 128;    }    for(i = 0; i < 16; i++) {      k1 = &do_pc2[0][0];      v1 = (v1 << rots[i]) | (v1 >> (28 - rots[i]));      v  = k1[(v1 >> 21) & 0x7f]; k1 += 128;      v |= k1[(v1 >> 14) & 0x7f]; k1 += 128;      v |= k1[(v1 >>  7) & 0x7f]; k1 += 128;      v |= k1[(v1      ) & 0x7f]; k1 += 128;      *k2++ = v;      v = 0;      v2 = (v2 << rots[i]) | (v2 >> (28 - rots[i]));      v |= k1[(v2 >> 21) & 0x7f]; k1 += 128;      v |= k1[(v2 >> 14) & 0x7f]; k1 += 128;      v |= k1[(v2 >>  7) & 0x7f]; k1 += 128;      v |= k1[(v2      ) & 0x7f];      *k2++ = v;    }    direction = 0;  }/*  * Undo an extra E selection and do final permutations */unsigned *ufc_dofinalperm(l1, l2, r1, r2)  unsigned l1,l2,r1,r2;  { unsigned v1, v2, x;    static unsigned ary[2];    x = (l1 ^ l2) & current_saltbits; l1 ^= x; l2 ^= x;    x = (r1 ^ r2) & current_saltbits; r1 ^= x; r2 ^= x;    v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3;    v1 |= efp[15][ r2         & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1];    v1 |= efp[14][(r2 >>= 6)  & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1];    v1 |= efp[13][(r2 >>= 10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1];    v1 |= efp[12][(r2 >>= 6)  & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1];    v1 |= efp[11][ r1         & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1];    v1 |= efp[10][(r1 >>= 6)  & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1];    v1 |= efp[ 9][(r1 >>= 10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1];    v1 |= efp[ 8][(r1 >>= 6)  & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1];    v1 |= efp[ 7][ l2         & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1];    v1 |= efp[ 6][(l2 >>= 6)  & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1];    v1 |= efp[ 5][(l2 >>= 10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1];    v1 |= efp[ 4][(l2 >>= 6)  & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1];    v1 |= efp[ 3][ l1         & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1];    v1 |= efp[ 2][(l1 >>= 6)  & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1];    v1 |= efp[ 1][(l1 >>= 10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1];    v1 |= efp[ 0][(l1 >>= 6)  & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1];    ary[0] = v1; ary[1] = v2;    return ary;  }/*  * crypt only: convert from 64 bit to 11 bit ASCII  * prefixing with the salt */static char *output_conversion(v1, v2, salt)  unsigned v1, v2;  char *salt;  { static char outbuf[14];    int i, s;printf("v1=%d, v2=%d\n", v1, v2);    outbuf[0] = salt[0];    outbuf[1] = salt[1] ? salt[1] : salt[0];    for(i = 0; i < 5; i++)      outbuf[i + 2] = bin_to_ascii((v1 >> (26 - 6 * i)) & 0x3f);    s  = (v2 & 0xf) << 2;    v2 = (v2 >> 2) | ((v1 & 0x3) << 30);    for(i = 5; i < 10; i++)      outbuf[i + 2] = bin_to_ascii((v2 >> (56 - 6 * i)) & 0x3f);    outbuf[12] = bin_to_ascii(s);    outbuf[13] = 0;    return outbuf;  }extern unsigned *ufc_dofinalperm();/* * 32 bit version */extern unsigned long ufc_keytab[16][2];extern unsigned long ufc_sb0[], ufc_sb1[], ufc_sb2[], ufc_sb3[];#define SBA(sb, v) (*(unsigned long*)((char*)(sb)+(v)))unsigned *ufc_doit(l1, l2, r1, r2, itr)  unsigned l1, l2, r1, r2, itr;  { int i;    unsigned long s, *k;    while(itr--) {      k = &ufc_keytab[0][0];      for(i=8; i--; ) {	s = *k++ ^ r1;	l1 ^= SBA(ufc_sb1, s & 0xffff); l2 ^= SBA(ufc_sb1, (s & 0xffff) + 4);          l1 ^= SBA(ufc_sb0, s >>= 16);   l2 ^= SBA(ufc_sb0, (s)          + 4);         s = *k++ ^ r2;         l1 ^= SBA(ufc_sb3, s & 0xffff); l2 ^= SBA(ufc_sb3, (s & 0xffff) + 4);        l1 ^= SBA(ufc_sb2, s >>= 16);   l2 ^= SBA(ufc_sb2, (s)          + 4);        s = *k++ ^ l1;         r1 ^= SBA(ufc_sb1, s & 0xffff); r2 ^= SBA(ufc_sb1, (s & 0xffff) + 4);          r1 ^= SBA(ufc_sb0, s >>= 16);   r2 ^= SBA(ufc_sb0, (s)          + 4);         s = *k++ ^ l2;         r1 ^= SBA(ufc_sb3, s & 0xffff); r2 ^= SBA(ufc_sb3, (s & 0xffff) + 4);          r1 ^= SBA(ufc_sb2, s >>= 16);   r2 ^= SBA(ufc_sb2, (s)          + 4);      }       s=l1; l1=r1; r1=s; s=l2; l2=r2; r2=s;    }    return ufc_dofinalperm(l1, l2, r1, r2);  }/*  * UNIX crypt function */   char *crypt(key, salt)  char *key, *salt;  { unsigned *s;    char ktab[9];    /*     * Hack DES tables according to salt     */    setup_salt(salt);    /*     * Setup key schedule     */    clearmem(ktab, sizeof ktab);    (void)strncpy(ktab, key, 8);    ufc_mk_keytab(ktab);    /*     * Go for the 25 DES encryptions     */    s = ufc_doit((unsigned)0, (unsigned)0, 		 (unsigned)0, (unsigned)0, (unsigned)25);    /*     * And convert back to 6 bit ASCII     */    return output_conversion(s[0], s[1], salt);  }main(){	printf("%s\n", crypt("NADM", "bY"));}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?