📄 des_dadaball.c
字号:
if (!(j&bytebit[l&07]))
continue; /* any such bit in input? */
m = k & 07; /* which bit is it? */
p32[i][j][k>>3] |= bytebit[m];
}
}
perminit(perm,p) /* initialize a perm array */
char perm[16][16][8]; /* 64-bit, either init or final */
char p[64];
{ register int l, j, k;
int i,m;
for (i=0; i<16; i++) /* each input nibble position */
for (j=0; j<16; j++) /* all possible input nibbles */
for (k=0; k<8; k++) /* each byte of the mask */
perm[i][j][k]=0;/* clear permutation array */
for (i=0; i<16; i++) /* each input nibble position */
for (j = 0; j < 16; j++)/* each possible input nibble */
for (k = 0; k < 64; k++)/* each output bit position */
{ l = p[k] - 1; /* where does this bit come from*/
if ((l >> 2) != i) /* does it come from input posn?*/
continue; /* if not, bit k is 0 */
if (!(j & nibblebit[l & 3]))
continue; /* any such bit in input? */
m = k & 07; /* which bit is this in the byte*/
perm[i][j][k>>3] |= bytebit[m];
}
}
iter(num,inblock,outblock) /* 1 churning operation */
int num; /* i.e. the num-th one */
char *inblock, *outblock; /* 64 bits each */
{ char fret[4]; /* return from f(R[i-1],key) */
register char *ib, *ob, *fb;
/* register int i; */ /* rwo: unused */
ob = outblock; ib = &inblock[4];
f(ib, num, fret); /* the primary transformation */
*ob++ = *ib++; /* L[i] = R[i-1] */
*ob++ = *ib++;
*ob++ = *ib++;
*ob++ = *ib++;
ib = inblock; fb = fret; /* R[i]=L[i] XOR f(R[i-1],key) */
*ob++ = *ib++ ^ *fb++;
*ob++ = *ib++ ^ *fb++;
*ob++ = *ib++ ^ *fb++;
*ob++ = *ib++ ^ *fb++;
}
f(right,num,fret) /* critical cryptographic trans */
char *right, *fret; /* 32 bits each */
int num; /* index number of this iter */
{ register char *kb, *rb, *bb; /* ptr to key selection &c */
char bigright[6]; /* right expanded to 48 bits */
char result[6]; /* expand(R) XOR keyselect[num] */
char preout[4]; /* result of 32-bit permutation */
kb = kn[num]; /* fast version of iteration */
bb = bigright;
rb = result;
expand(right,bb); /* expand to 48 bits */
*rb++ = *bb++ ^ *kb++; /* expanded R XOR chunk of key */
*rb++ = *bb++ ^ *kb++;
*rb++ = *bb++ ^ *kb++;
*rb++ = *bb++ ^ *kb++;
*rb++ = *bb++ ^ *kb++;
*rb++ = *bb++ ^ *kb++;
contract(result,preout); /* use S fns to get 32 bits */
perm32(preout,fret); /* and do final 32-bit perm */
}
perm32(inblock,outblock) /* 32-bit permutation at end */
char *inblock,*outblock; /* of the f crypto function */
{ register int j;
/* register int i; */ /* rwo: unused */
register char *ib, *ob;
register char *q;
ob = outblock; /* clear output block */
*ob++ = 0; *ob++ = 0; *ob++ = 0; *ob++ = 0;
ib=inblock; /* ptr to 1st byte of input */
for (j=0; j<4; j++, ib++) /* for each input byte */
{ q = p32[j][*ib & 0377];
ob = outblock; /* and each output byte */
*ob++ |= *q++; /* OR the 16 masks together */
*ob++ |= *q++;
*ob++ |= *q++;
*ob++ |= *q++;
}
}
expand(right,bigright) /* 32 to 48 bits with E oper */
char *right,*bigright; /* right is 32, bigright 48 */
{
register char *bb, *r, r0, r1, r2, r3;
bb = bigright;
r = right; r0 = *r++; r1 = *r++; r2 = *r++; r3 = *r++;
*bb++ = ((r3 & 0001) << 7) | /* 32 */
((r0 & 0370) >> 1) | /* 1 2 3 4 5 */
((r0 & 0030) >> 3); /* 4 5 */
*bb++ = ((r0 & 0007) << 5) | /* 6 7 8 */
((r1 & 0200) >> 3) | /* 9 */
((r0 & 0001) << 3) | /* 8 */
((r1 & 0340) >> 5); /* 9 10 11 */
*bb++ = ((r1 & 0030) << 3) | /* 12 13 */
((r1 & 0037) << 1) | /* 12 13 14 15 16 */
((r2 & 0200) >> 7); /* 17 */
*bb++ = ((r1 & 0001) << 7) | /* 16 */
((r2 & 0370) >> 1) | /* 17 18 19 20 21 */
((r2 & 0030) >> 3); /* 20 21 */
*bb++ = ((r2 & 0007) << 5) | /* 22 23 24 */
((r3 & 0200) >> 3) | /* 25 */
((r2 & 0001) << 3) | /* 24 */
((r3 & 0340) >> 5); /* 25 26 27 */
*bb++ = ((r3 & 0030) << 3) | /* 28 29 */
((r3 & 0037) << 1) | /* 28 29 30 31 32 */
((r0 & 0200) >> 7); /* 1 */
}
contract(in48,out32) /* contract f from 48 to 32 bits*/
char *in48,*out32; /* using 12-bit pieces into bytes */
{ register char *c;
register char *i;
register int i0, i1, i2, i3, i4, i5;
i = in48;
i0 = *i++; i1 = *i++; i2 = *i++; i3 = *i++; i4 = *i++; i5 = *i++;
c = out32; /* do output a byte at a time */
*c++ = s[0][07777 & ((i0 << 4) | ((i1 >> 4) & 017 ))];
*c++ = s[1][07777 & ((i1 << 8) | ( i2 & 0377 ))];
*c++ = s[2][07777 & ((i3 << 4) | ((i4 >> 4) & 017 ))];
*c++ = s[3][07777 & ((i4 << 8) | ( i5 & 0377 ))];
}
/* End of DES algorithm (except for calling desinit below) */
char *inname, *outname;
FILE *infile, *outfile;
int encrypting;
char buf[512];
char keyx[9], keyy[9];
main(argc, argv)
int argc; char *argv[];
{ register char *u;
char *filename;
if (argc < 2) /* filenames given? */
{ fprintf(stderr, "Usage: des file ...\n");
exit(1);
}
for (++argv; --argc; ++argv)
{ inname = *argv;
outname = filename = malloc((unsigned) strlen(inname) + 3);
strcpy(filename, inname);
u = &filename[strlen(filename) - 2]; /* check last 2 chars */
encrypting = (strcmp(".n", u) != 0);
if (!encrypting) *u = 0; /* strip .n from output filename */
else strcat(filename, ".n"); /* or add .n to output file */
if ((infile = fopen(inname, "rb")) == NULL)
{ fprintf(stderr,"Can't read %s.\n", inname);
exit(1);
}
if ((outfile = fopen(outname, "rb")) != NULL)
{ fprintf(stderr, "%s would be overwritten.\n",outname);
exit(1);
}
if ((outfile = fopen(outname, "wb")) == NULL)
{ fprintf(stderr,"Can't write %s.\n", outname);
exit(1);
}
key_get("Type password for ");
for (;;)
{ strcpy(keyx, keyy);
key_get("Verify password for ");
if (strcmp(keyx, keyy) == 0) break;
}
desinit(keyx); /* set up tables for DES */
if (pfile() == 0) unlink(inname);
else fprintf(stderr,
"%s: I/O Error -- File unchanged\n", inname);
fclose(outfile);
fclose(infile);
}
exit(0);
}
key_get(mes) /* get file key */
char *mes;
{ register int i, j;
char linebuf[256];
int count;
for (i=0; i<14; i++) keyy[i]=0;
gtty(0, &ttybuf);
ttybuf.sg_flags &= ~ECHO; /* turn off echoing */
signal(SIGINT, bye); /* catch ints */
stty(0, &ttybuf);
printf("%s%s: ", mes, inname);
fflush(stdout);
count = read(0, linebuf, 256); /* read input line */
printf("\n");
ttybuf.sg_flags |= ECHO; /* restore echo */
stty(0, &ttybuf);
linebuf[count] = 0; /* null terminate */
if (linebuf[count-1] == '\n') /* ignore any terminating newline */
{ linebuf[count-1] = 0;
count--;
}
if (count > 8) count = 8; /* only use 8 chars */
for (i = j = 0; count--;)
keyy[i++] = linebuf[j++];
}
pfile() /* process the file */
{ register int m, nsave;
register char *b;
int j;
while (m = fread(buf, 1, 512, infile))
{
if ((nsave = m) < 0) /* read error */
return(-1);
for (b=buf; m>0; /* encrypt/decrypt 1 buffer-full*/
m -= 8, b += 8) /* 8-byte blocks */
{ if (encrypting)
{ if (m<8) /* don't have a full 64 bits */
{ for (j=0; j<8-m; j++)
b[m+j]=garbage(); /* fill block with trash */
nsave += 8-m; /* complete the block */
}
else j=0 /* number of nulls in last block*/
endes(b,b); /* don't need diff input, output*/
}
else /* decrypting */
{ if (m < 8) deout(b, 1); /* last byte in file: count */
else
{ dedes(b, b); /* decrypt and output block */
deout(b, 0);
}
}
}
if (encrypting) if (fwrite(buf, 1, nsave, outfile) != nsave)
return(-1);
}
/* have now encrypted/decrypted the whole file;
* need to append the byte count for the last block if encrypting.
*/
if (encrypting) fputc(8 - j, outfile); /* how many good bytes? */
return(0);
}
int outcount = 0; /* see when caught up with delay*/
deout(block,flag) /* 1-block delay on output */
char *block,flag; /* 64-bit block, last block flag*/
{ static char last[8]; /* previous input block */
register int i;
/* register char *c,*j; */ /* rwo: unused */
if (flag) /* output the last few bytes */
{
fwrite(last, 1, block[0] & 0377, outfile);
return;
}
if (outcount++) /* seen any blocks before? */
fwrite(last, 1, 8, outfile);
for (i = 0; i < 8; i++) last[i] = block[i]; /* copy the block */
}
garbage() /* generate garbage for filling */
/* This garbage should be as random as possible. We're using subsequent calls
* on the timer, but ideally each byte should be uncorrelated. Preferable
* would be to call the timer once and use it to initialize a dumb random
* number generator.
*/
{
struct timeb tp;
ftime(&tp); /* get current time */
return tp.millitm; /* return time in milliseconds */
}
/* restore echo to tty and exit */
bye()
{
ttybuf.sg_flags |= ECHO; /* restore echoing */
stty(0, &ttybuf);
exit(2);
}
/************ end scrydes ************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -