📄 bdes.c
字号:
* do the transformation */ DES_XFORM(UBUFFER(msgbuf)); WRITE(BUFFER(msgbuf), 8); } /* * at EOF or last block -- in either ase, the last byte contains * the character representation of the number of bytes in it */ bn++; MEMZERO(&CHAR(msgbuf, n), 8 - n); CHAR(msgbuf, 7) = n; DES_XFORM(UBUFFER(msgbuf)); WRITE(BUFFER(msgbuf), 8);}/* * This decrypts using the Electronic Code Book mode of DES */ecbdec(){ register int n; /* number of bytes actually read */ register int c; /* used to test for EOF */ register int bn; /* block number */ Desbuf msgbuf; /* I/O buffer */ for (bn = 1; (n = READ(BUFFER(msgbuf), 8)) == 8; bn++) { /* * do the transformation */ DES_XFORM(UBUFFER(msgbuf)); /* * if the last one, handle it specially */ if ((c = getchar()) == EOF) { n = CHAR(msgbuf, 7); if (n < 0 || n > 7) err(bn, "decryption failed (block corrupted)"); } else (void)ungetc(c, stdin); WRITE(BUFFER(msgbuf), n); } if (n > 0) err(bn, "decryption failed (incomplete block)");}/* * This encrypts using the Cipher Block Chaining mode of DES */cbcenc(){ register int n; /* number of bytes actually read */ register int bn; /* block number */ Desbuf msgbuf; /* I/O buffer */ /* * do the transformation */ for (bn = 1; (n = READ(BUFFER(msgbuf), 8)) == 8; bn++) { for (n = 0; n < 8; n++) CHAR(msgbuf, n) ^= CHAR(ivec, n); DES_XFORM(UBUFFER(msgbuf)); MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8); WRITE(BUFFER(msgbuf), 8); } /* * at EOF or last block -- in either case, the last byte contains * the character representation of the number of bytes in it */ bn++; MEMZERO(&CHAR(msgbuf, n), 8 - n); CHAR(msgbuf, 7) = n; for (n = 0; n < 8; n++) CHAR(msgbuf, n) ^= CHAR(ivec, n); DES_XFORM(UBUFFER(msgbuf)); WRITE(BUFFER(msgbuf), 8);}/* * This decrypts using the Cipher Block Chaining mode of DES */cbcdec(){ register int n; /* number of bytes actually read */ Desbuf msgbuf; /* I/O buffer */ Desbuf ibuf; /* temp buffer for initialization vector */ register int c; /* used to test for EOF */ register int bn; /* block number */ for (bn = 0; (n = READ(BUFFER(msgbuf), 8)) == 8; bn++) { /* * do the transformation */ MEMCPY(BUFFER(ibuf), BUFFER(msgbuf), 8); DES_XFORM(UBUFFER(msgbuf)); for (c = 0; c < 8; c++) UCHAR(msgbuf, c) ^= UCHAR(ivec, c); MEMCPY(BUFFER(ivec), BUFFER(ibuf), 8); /* * if the last one, handle it specially */ if ((c = getchar()) == EOF) { n = CHAR(msgbuf, 7); if (n < 0 || n > 7) err(bn, "decryption failed (block corrupted)"); } else (void)ungetc(c, stdin); WRITE(BUFFER(msgbuf), n); } if (n > 0) err(bn, "decryption failed (incomplete block)");}/* * This authenticates using the Cipher Block Chaining mode of DES */cbcauth(){ register int n, j; /* number of bytes actually read */ Desbuf msgbuf; /* I/O buffer */ Desbuf encbuf; /* encryption buffer */ /* * do the transformation * note we DISCARD the encrypted block; * we only care about the last one */ while ((n = READ(BUFFER(msgbuf), 8)) == 8) { for (n = 0; n < 8; n++) CHAR(encbuf, n) = CHAR(msgbuf, n) ^ CHAR(ivec, n); DES_XFORM(UBUFFER(encbuf)); MEMCPY(BUFFER(ivec), BUFFER(encbuf), 8); } /* * now compute the last one, right padding with '\0' if need be */ if (n > 0) { MEMZERO(&CHAR(msgbuf, n), 8 - n); for (n = 0; n < 8; n++) CHAR(encbuf, n) = CHAR(msgbuf, n) ^ CHAR(ivec, n); DES_XFORM(UBUFFER(encbuf)); } /* * drop the bits * we write chars until fewer than 7 bits, * and then pad the last one with 0 bits */ for (n = 0; macbits > 7; n++, macbits -= 8) (void)putchar(CHAR(encbuf, n)); if (macbits > 0) { CHAR(msgbuf, 0) = 0x00; for (j = 0; j < macbits; j++) CHAR(msgbuf, 0) |= (CHAR(encbuf, n)&bits[j]); (void)putchar(CHAR(msgbuf, 0)); }}/* * This encrypts using the Cipher FeedBack mode of DES */cfbenc(){ register int n; /* number of bytes actually read */ register int nbytes; /* number of bytes to read */ register int bn; /* block number */ char ibuf[8]; /* input buffer */ Desbuf msgbuf; /* encryption buffer */ /* * do things in bytes, not bits */ nbytes = fbbits / 8; /* * do the transformation */ for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) { MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (n = 0; n < 8 - nbytes; n++) UCHAR(ivec, n) = UCHAR(ivec, n+nbytes); for (n = 0; n < nbytes; n++) UCHAR(ivec, 8-nbytes+n) = ibuf[n] ^ UCHAR(msgbuf, n); WRITE(&CHAR(ivec, 8-nbytes), nbytes); } /* * at EOF or last block -- in either case, the last byte contains * the character representation of the number of bytes in it */ bn++; MEMZERO(&ibuf[n], nbytes - n); ibuf[nbytes - 1] = n; MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (n = 0; n < nbytes; n++) ibuf[n] ^= UCHAR(msgbuf, n); WRITE(ibuf, nbytes);}/* * This decrypts using the Cipher Block Chaining mode of DES */cfbdec(){ register int n; /* number of bytes actually read */ register int c; /* used to test for EOF */ register int nbytes; /* number of bytes to read */ register int bn; /* block number */ char ibuf[8]; /* input buffer */ char obuf[8]; /* output buffer */ Desbuf msgbuf; /* encryption buffer */ /* * do things in bytes, not bits */ nbytes = fbbits / 8; /* * do the transformation */ for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) { MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (c = 0; c < 8 - nbytes; c++) CHAR(ivec, c) = CHAR(ivec, c+nbytes); for (c = 0; c < nbytes; c++) { CHAR(ivec, 8-nbytes+c) = ibuf[c]; obuf[c] = ibuf[c] ^ UCHAR(msgbuf, c); } /* * if the last one, handle it specially */ if ((c = getchar()) == EOF) { n = obuf[nbytes-1]; if (n < 0 || n > nbytes-1) err(bn, "decryption failed (block corrupted)"); } else (void)ungetc(c, stdin); WRITE(obuf, n); } if (n > 0) err(bn, "decryption failed (incomplete block)");}/* * This encrypts using the alternative Cipher FeedBack mode of DES */cfbaenc(){ register int n; /* number of bytes actually read */ register int nbytes; /* number of bytes to read */ register int bn; /* block number */ char ibuf[8]; /* input buffer */ char obuf[8]; /* output buffer */ Desbuf msgbuf; /* encryption buffer */ /* * do things in bytes, not bits */ nbytes = fbbits / 7; /* * do the transformation */ for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) { MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (n = 0; n < 8 - nbytes; n++) UCHAR(ivec, n) = UCHAR(ivec, n+nbytes); for (n = 0; n < nbytes; n++) UCHAR(ivec, 8-nbytes+n) = (ibuf[n] ^ UCHAR(msgbuf, n)) |0200; for (n = 0; n < nbytes; n++) obuf[n] = CHAR(ivec, 8-nbytes+n)&0177; WRITE(obuf, nbytes); } /* * at EOF or last block -- in either case, the last byte contains * the character representation of the number of bytes in it */ bn++; MEMZERO(&ibuf[n], nbytes - n); ibuf[nbytes - 1] = ('0' + n)|0200; MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (n = 0; n < nbytes; n++) ibuf[n] ^= UCHAR(msgbuf, n); WRITE(ibuf, nbytes);}/* * This decrypts using the alternative Cipher Block Chaining mode of DES */cfbadec(){ register int n; /* number of bytes actually read */ register int c; /* used to test for EOF */ register int nbytes; /* number of bytes to read */ register int bn; /* block number */ char ibuf[8]; /* input buffer */ char obuf[8]; /* output buffer */ Desbuf msgbuf; /* encryption buffer */ /* * do things in bytes, not bits */ nbytes = fbbits / 7; /* * do the transformation */ for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) { MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (c = 0; c < 8 - nbytes; c++) CHAR(ivec, c) = CHAR(ivec, c+nbytes); for (c = 0; c < nbytes; c++) { CHAR(ivec, 8-nbytes+c) = ibuf[c]|0200; obuf[c] = (ibuf[c] ^ UCHAR(msgbuf, c))&0177; } /* * if the last one, handle it specially */ if ((c = getchar()) == EOF) { if ((n = (obuf[nbytes-1] - '0')) < 0 || n > nbytes-1) err(bn, "decryption failed (block corrupted)"); } else (void)ungetc(c, stdin); WRITE(obuf, n); } if (n > 0) err(bn, "decryption failed (incomplete block)");}/* * This encrypts using the Output FeedBack mode of DES */ofbenc(){ register int n; /* number of bytes actually read */ register int c; /* used to test for EOF */ register int nbytes; /* number of bytes to read */ register int bn; /* block number */ char ibuf[8]; /* input buffer */ char obuf[8]; /* output buffer */ Desbuf msgbuf; /* encryption buffer */ /* * do things in bytes, not bits */ nbytes = fbbits / 8; /* * do the transformation */ for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) { MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (n = 0; n < 8 - nbytes; n++) UCHAR(ivec, n) = UCHAR(ivec, n+nbytes); for (n = 0; n < nbytes; n++) { UCHAR(ivec, 8-nbytes+n) = UCHAR(msgbuf, n); obuf[n] = ibuf[n] ^ UCHAR(msgbuf, n); } WRITE(obuf, nbytes); } /* * at EOF or last block -- in either case, the last byte contains * the character representation of the number of bytes in it */ bn++; MEMZERO(&ibuf[n], nbytes - n); ibuf[nbytes - 1] = n; MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (c = 0; c < nbytes; c++) ibuf[c] ^= UCHAR(msgbuf, c); WRITE(ibuf, nbytes);}/* * This decrypts using the Output Block Chaining mode of DES */ofbdec(){ register int n; /* number of bytes actually read */ register int c; /* used to test for EOF */ register int nbytes; /* number of bytes to read */ register int bn; /* block number */ char ibuf[8]; /* input buffer */ char obuf[8]; /* output buffer */ Desbuf msgbuf; /* encryption buffer */ /* * do things in bytes, not bits */ nbytes = fbbits / 8; /* * do the transformation */ for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) { MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (c = 0; c < 8 - nbytes; c++) CHAR(ivec, c) = CHAR(ivec, c+nbytes); for (c = 0; c < nbytes; c++) { CHAR(ivec, 8-nbytes+c) = UCHAR(msgbuf, c); obuf[c] = ibuf[c] ^ UCHAR(msgbuf, c); } /* * if the last one, handle it specially */ if ((c = getchar()) == EOF) { n = obuf[nbytes-1]; if (n < 0 || n > nbytes-1) err(bn, "decryption failed (block corrupted)"); } else (void)ungetc(c, stdin); /* * dump it */ WRITE(obuf, n); } if (n > 0) err(bn, "decryption failed (incomplete block)");}/* * This authenticates using the Cipher FeedBack mode of DES */cfbauth(){ register int n, j; /* number of bytes actually read */ register int nbytes; /* number of bytes to read */ char ibuf[8]; /* input buffer */ Desbuf msgbuf; /* encryption buffer */ /* * do things in bytes, not bits */ nbytes = fbbits / 8; /* * do the transformation */ while ((n = READ(ibuf, nbytes)) == nbytes) { MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (n = 0; n < 8 - nbytes; n++) UCHAR(ivec, n) = UCHAR(ivec, n+nbytes); for (n = 0; n < nbytes; n++) UCHAR(ivec, 8-nbytes+n) = ibuf[n] ^ UCHAR(msgbuf, n); } /* * at EOF or last block -- in either case, the last byte contains * the character representation of the number of bytes in it */ MEMZERO(&ibuf[n], nbytes - n); ibuf[nbytes - 1] = '0' + n; MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8); DES_XFORM(UBUFFER(msgbuf)); for (n = 0; n < nbytes; n++) ibuf[n] ^= UCHAR(msgbuf, n); /* * drop the bits * we write chars until fewer than 7 bits, * and then pad the last one with 0 bits */ for (n = 0; macbits > 7; n++, macbits -= 8) (void)putchar(CHAR(msgbuf, n)); if (macbits > 0) { CHAR(msgbuf, 0) = 0x00; for (j = 0; j < macbits; j++) CHAR(msgbuf, 0) |= (CHAR(msgbuf, n)&bits[j]); (void)putchar(CHAR(msgbuf, 0)); }}#ifndef FASTWAY/* * change from 8 bits/Uchar to 1 bit/Uchar */expand(from, to) Desbuf from; /* 8bit/unsigned char string */ char *to; /* 1bit/char string */{ register int i, j; /* counters in for loop */ for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) *to++ = (CHAR(from, i)>>(7-j))&01;}/* * change from 1 bit/char to 8 bits/Uchar */compress(from, to) char *from; /* 1bit/char string */ Desbuf to; /* 8bit/unsigned char string */{ register int i, j; /* counters in for loop */ for (i = 0; i < 8; i++) { CHAR(to, i) = 0; for (j = 0; j < 8; j++) CHAR(to, i) = ((*from++)<<(7-j))|CHAR(to, i); }}#endif/* * message about usage */usage(){ (void)fprintf(stderr, "%s\n", "usage: bdes [-abdp] [-F bit] [-f bit] [-k key] [-m bit] [-o bit] [-v vector]"); exit(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -