⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 risto-des.shar

📁 多种环境下的实现方式
💻 SHAR
📖 第 1 页 / 共 2 页
字号:
XEND_OF_destest.cif test 2103 -ne `wc -c <destest.c`; then    echo shar: \"destest.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f mdes.c -a "${1}" != "-c" ; then   echo shar: Will not over-write existing file \"mdes.c\"elseecho shar: Extracting \"mdes.c\" \(7722 characters\)sed "s/^X//" >mdes.c <<'END_OF_mdes.c'X/* mdes.c -- minimal des implementation (and very slow, too)X * Public Domain, 1993 by Risto Paasivirta, paasivir@jyu.fiX * No warranty, try with your own risk.X */XX#include <string.h>X#include "mdes.h"XX/*X * left shift table for key schedule LSX */XXstatic const unsigned char LS[16] = {X   1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1X};XX/*X * permuted-choice tables for key schedule PC1, PC2X */XXstatic const unsigned char PC1[56] = {X  57,49,41,33,25,17, 9, 1,58,50,42,34,26,18,10, 2,X  59,51,43,35,27,19,11, 3,60,52,44,36,63,55,47,39,X  31,23,15, 7,62,54,46,38,30,22,14, 6,61,53,45,37,X  29,21,13, 5,28,20,12, 4X};XXstatic const unsigned char PC2[48] = {X  14,17,11,24, 1, 5, 3,28,15, 6,21,10,23,19,12, 4,X  26, 8,16, 7,27,20,13, 2,41,52,31,37,47,55,30,40,X  51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32X};XX/*X * initial permutation IPX */XXstatic const unsigned char IP[64] = {X  58,50,42,34,26,18,10, 2,60,52,44,36,28,20,12, 4,X  62,54,46,38,30,22,14, 6,64,56,48,40,32,24,16, 8,X  57,49,41,33,25,17, 9, 1,59,51,43,35,27,19,11, 3,X  61,53,45,37,29,21,13, 5,63,55,47,39,31,23,15, 7X};XX/*X * bit-selection table EX */XXstatic const unsigned char E[48] = {X  32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9,10,11,X  12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,X  22,23,24,25,24,25,26,27,28,29,28,29,30,31,32, 1X};XX/*X * substitution tables STX */XXstatic const unsigned char ST[8][64] = {X  14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,X   0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,X   4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,X  15,12, 8, 2, 4, 9, 1, 7, 5,11, 3,14,10, 0, 6,13,XX  15, 1, 8,14, 6,11, 3, 4, 9, 7, 2,13,12, 0, 5,10,X   3,13, 4, 7,15, 2, 8,14,12, 0, 1,10, 6, 9,11, 5,X   0,14, 7,11,10, 4,13, 1, 5, 8,12, 6, 9, 3, 2,15,X  13, 8,10, 1, 3,15, 4, 2,11, 6, 7,12, 0, 5,14, 9,XX  10, 0, 9,14, 6, 3,15, 5, 1,13,12, 7,11, 4, 2, 8,X  13, 7, 0, 9, 3, 4, 6,10, 2, 8, 5,14,12,11,15, 1,X  13, 6, 4, 9, 8,15, 3, 0,11, 1, 2,12, 5,10,14, 7,X   1,10,13, 0, 6, 9, 8, 7, 4,15,14, 3,11, 5, 2,12,XX   7,13,14, 3, 0, 6, 9,10, 1, 2, 8, 5,11,12, 4,15,X  13, 8,11, 5, 6,15, 0, 3, 4, 7, 2,12, 1,10,14, 9,X  10, 6, 9, 0,12,11, 7,13,15, 1, 3,14, 5, 2, 8, 4,X   3,15, 0, 6,10, 1,13, 8, 9, 4, 5,11,12, 7, 2,14,XX   2,12, 4, 1, 7,10,11, 6, 8, 5, 3,15,13, 0,14, 9,X  14,11, 2,12, 4, 7,13, 1, 5, 0,15,10, 3, 9, 8, 6,X   4, 2, 1,11,10,13, 7, 8,15, 9,12, 5, 6, 3, 0,14,X  11, 8,12, 7, 1,14, 2,13, 6,15, 0, 9,10, 4, 5, 3,XX  12, 1,10,15, 9, 2, 6, 8, 0,13, 3, 4,14, 7, 5,11,X  10,15, 4, 2, 7,12, 9, 5, 6, 1,13,14, 0,11, 3, 8,X   9,14,15, 5, 2, 8,12, 3, 7, 0, 4,10, 1,13,11, 6,X   4, 3, 2,12, 9, 5,15,10,11,14, 1, 7, 6, 0, 8,13,XX   4,11, 2,14,15, 0, 8,13, 3,12, 9, 7, 5,10, 6, 1,X  13, 0,11, 7, 4, 9, 1,10,14, 3, 5,12, 2,15, 8, 6,X   1, 4,11,13,12, 3, 7,14,10,15, 6, 8, 0, 5, 9, 2,X   6,11,13, 8, 1, 4,10, 7, 9, 5, 0,15,14, 2, 3,12,XX  13, 2, 8, 4, 6,15,11, 1,10, 9, 3,14, 5, 0,12, 7,X   1,15,13, 8,10, 3, 7, 4,12, 5, 6,11, 0,14, 9, 2,X   7,11, 4, 1, 9,12,14, 2, 0, 6,10,13,15, 3, 5, 8,X   2, 1,14, 7, 4,10, 8,13,15,12, 9, 0, 3, 5, 6,11X};XX/*X * permutation table PX */XXstatic const unsigned char P[32] = {X  16, 7,20,21,29,12,28,17, 1,15,23,26, 5,18,31,10,X   2, 8,24,14,32,27, 3, 9,19,13,30, 6,22,11, 4,25X};XX/*X * final permutation FPX */XXstatic const unsigned char FP[64] = {X  40, 8,48,16,56,24,64,32,39, 7,47,15,55,23,63,31,X  38, 6,46,14,54,22,62,30,37, 5,45,13,53,21,61,29,X  36, 4,44,12,52,20,60,28,35, 3,43,11,51,19,59,27,X  34, 2,42,10,50,18,58,26,33, 1,41, 9,49,17,57,25X};XX/*X * unbit(to, from, len) -- unpack len bytes to bitsX */XXstatic voidXunbit(unsigned char *to,unsigned char *from,int len)X{X  int j, f;XX  while (len--) {X    for (j = 8, f = *from++; j--;)X      *to++ =  (f >> j) & 1;X  }X}XX/*X * pkbit(to from, len) -- pack bits to len bytes X */XXstatic voidXpkbit(unsigned char *to, unsigned char *from, int len)X{X  int j,t;XX  while (len--) {X    for (j = 8, t = 0; j--;)X      t |= *from++ << j;X    *to++ = t;X  }X}XX/*X * perm(to, OP, from, bits) -- do permutationX */XXstatic voidXperm(unsigned char *to, const unsigned char *op,X  unsigned char *from, int bits)X{X  int i;XX  for (i = 0; i < bits; i++)X    to[i] = from[op[i]-1];X}XX/*X * rot(to, from, len, bits) -- rotate leftX */XXstatic voidXrot(unsigned char *r, int len, int bits)X{X  int i,t;XX  if (len > 1) {X    for (i = 0; i<bits; i++) {X      t = r[0];X      memmove(&r[0], &r[1], len - 1);X      r[len-1] = t;X    }X  }X}XX/*X * add2(to, from, bits) -- add bits modulo 2 (that is, eor)X */XXstatic voidXadd2(unsigned char *to, unsigned char *from, int bits)X{X  int i;XX  for (i=0; i<bits; i++)X    to[i] ^= from[i];X}XX/*X * subs(o,i,s) -- substitute 6 bits to 4 bits using ST[s]X */XXstatic voidXsubs(unsigned char *o, unsigned char *i, int s) X{X  int t;XX  t = ST[s][X    (i[0] << 5) | (i[5] << 4) | /* 1st and 6th bit, row index */X    (i[1] << 3) | (i[2] << 2) | /* 2nd to 5th bit, col index */X    (i[3] << 1) |  i[4]];X  o[0] = (t >> 3) & 1;X  o[1] = (t >> 2) & 1;X  o[2] = (t >> 1) & 1;X  o[3] =  t & 1;X}XX/*X * des_set_key(key,ks) -- generate a keyscheduleX */XXintXdes_set_key(des_cblock *key, des_key_schedule *ks)X{X  int i;X  unsigned char k[64],cd[56];XX  unbit(k,(char *)key,8);X  perm(cd, PC1, k, 56);X  for (i = 0; i < 16; i++) {X    rot(&cd[0], 28, LS[i]);X    rot(&cd[28], 28, LS[i]);X    perm(ks->b[i], PC2, cd, 48);X  }X  return 1;X}XX/*X * des_func(in,out,ks) -- des core functionX */XXstatic voidXdes_func(unsigned char in[64], unsigned char out[64],X  unsigned char b[16][48], int enc)X{X  int i,j;X  unsigned char lr[64], rsto[32], si[48], so[32];X#define LEFT  lrX#define RIGHT  &lr[32]XX  perm(lr, IP, in, 64); /* initial permutation */X  for (i = 0; i < 16; i++) { /* 16 rounds */X    memcpy(rsto, RIGHT,32); /* store right part */X    perm(si, E, RIGHT, 48); /* F-box: permute to subst. input */X    add2(si, b[enc ? i : 15-i], 48); /* add key (enc/dec order) */X    for (j = 0; j < 8; j++) /* 48 to 32 bit substitute */X      subs(&so[j*4],&si[j*6],j);X    perm(RIGHT, P, so, 32); /* permute substitution output: end F*/X    add2(RIGHT, LEFT, 32); /* add left part */X        memcpy(LEFT, rsto, 32); /* old right part will be new left */X  }X  memcpy(LEFT, RIGHT, 32); /* swap left and right part */X  memcpy(RIGHT,rsto, 32);X  perm(out,FP,lr,64); /* final permutation */X}XX/*X * des_ecb_encrypt(in, out, ks, enc) -- des electronic code book modeX */XXintXdes_ecb_encrypt(des_cblock *in, des_cblock *out,X  des_key_schedule *ks, int enc)X{X  unsigned char o[64];XX  unbit(o, (char *)in, 8);X  des_func(o, o, ks->b, enc);X  pkbit((char *)out, o, 8);X  return 8;X}XX/*X * des_cbc_encrypt(in, out, len, ks, iv, enc) -- cipher block chaining modeX */XXintXdes_cbc_encrypt(des_cblock *in, des_cblock *out,X  int len, des_key_schedule *ks, des_cblock *iv, int enc)X{X  int i;X  unsigned char ivb[64], ob[64], ib[64];XX  unbit(ivb,(char *)iv,8);X  for (i = 0; i < len-8; i+=8) {X    unbit(ib,(char *)in++,8);X    if (enc)X      add2(ib, ivb, 64);X    des_func(ib, ob, ks->b, enc);X    if (enc) {X      memcpy(ivb, ob, 64);X    } else {X      add2(ob, ivb, 64);X      memcpy(ivb, ib, 64);X    }X    pkbit((char *)out++, ob, 8);X  }X  memset(ib,0,64); /* pad with zeroes */X  unbit(ib,(char *)in,len-i);X  if (enc)X    add2(ib, ivb, 64);X  des_func(ib, ob, ks->b, enc);X  if (enc) {X    pkbit((char *)iv, ob, 64);X  } else {X    add2(ob, ivb, 64);X    pkbit((char *)iv, ib, 64);X  }X  pkbit((char *)out, ob, 8);X  return (i+8);X}XXvoidXdes_set_odd_parity(des_cblock *key)X{X  int i;X  unsigned char c;XX  for (i = 0; i < 8; i++) {X    c = key->_[i] & 0xfe;X    c = (c >> 4) ^ (c & 0x0f);X    c = (c >> 2) ^ (c & 3);X    key->_[i] = (key->_[i] & 0xfe) |X      ((c >> 1) ^ (c & 1)) ^ 1 ;X  }X}XEND_OF_mdes.cif test 7722 -ne `wc -c <mdes.c`; then    echo shar: \"mdes.c\" unpacked with wrong size!fi# end of overwriting checkfiif test -f mdes.h -a "${1}" != "-c" ; then   echo shar: Will not over-write existing file \"mdes.h\"elseecho shar: Extracting \"mdes.h\" \(747 characters\)sed "s/^X//" >mdes.h <<'END_OF_mdes.h'X/*X * mdes.h -- header for minimal desX * 1993 Risto Paasivirta, paasivir@jyu.fiX * Public Domain, no warranty. X */XX#ifndef MDES_HX#define MDES_HXX#define DES_ENCRYPT  1X#define DES_DECRYPT  0XXtypedef struct des_key_schedule {X  unsigned char b[16][48];X} des_key_schedule;XXtypedef struct des_cblock {X  unsigned char _[8];X} des_cblock;XXvoid des_string_to_key(char *str, des_cblock *key);Xvoid des_set_odd_parity(des_cblock *key);XXint des_set_key(des_cblock *k, des_key_schedule *key);Xint des_ecb_encrypt(des_cblock *input, des_cblock *output,X  des_key_schedule *schedule, int mode);Xint des_cbc_encrypt(des_cblock *input, des_cblock *output,X  int length, des_key_schedule *schedule,X  des_cblock *ivec, int mode);XX#endif /* MDES_H */XEND_OF_mdes.hif test 747 -ne `wc -c <mdes.h`; then    echo shar: \"mdes.h\" unpacked with wrong size!fi# end of overwriting checkfiecho shar: End of shell archive.exit 0-- /*paasivir@jyu.fi*/int a[3302],b=3301,*c=a,d,e,f;main(){for(e=b;--e;*c++=1);*c=2;for(d=2001;d--;printf("%05d",f))for(c=a,e=b;e;f/=e--){f+=*c*1e5;*c++=f%e;}}

⌨️ 快捷键说明

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