home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / des / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-29  |  1.5 KB  |  65 lines

  1. /*
  2.  * Data Encryption Standard front end
  3.  * Usage: des [-e -d] keyvalue infile outfile
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "des.h"
  9.  
  10. static void setparity(char *key);
  11.  
  12. void main(int argc, char *argv[])
  13. {
  14.    FILE *fi, *fo;
  15.    char key[9];
  16.    char blk[8];
  17.  
  18.    if (argc > 4)    {
  19.         strncpy(key, argv[2], 8);
  20.         key[8] = '\0';
  21.         setparity(key);
  22.  
  23.         initkey(key);
  24.        if ((fi = fopen(argv[3], "rb")) != NULL)    {
  25.            if ((fo = fopen(argv[4], "wb")) != NULL)    {
  26.                while (!feof(fi))    {
  27.                    memset(blk, 0, 8);
  28.                    if (fread(blk, 1, 8, fi) != 0)    {
  29.                        if (stricmp(argv[1], "-e") == 0)
  30.                            encrypt(blk);
  31.                        else
  32.                            decrypt(blk);
  33.                           fwrite(blk, 1, 8, fo);
  34.                    }
  35.                }
  36.                fclose(fo);
  37.            }
  38.            fclose(fi);
  39.        }
  40.    }
  41.    else
  42.        printf("\nUsage: des [-e -d] keyvalue infile outfile");
  43. }
  44.  
  45. /* -------- make a character odd parity ---------- */
  46. static unsigned char oddparity(unsigned char s)
  47. {
  48.    unsigned char c = s | 0x80;
  49.    while (s)    {
  50.        if (s & 1)
  51.            c ^= 0x80;
  52.        s = (s >> 1) & 0x7f;
  53.    }
  54.    return c;
  55. }
  56.  
  57. /* ------ make a key odd parity ------- */
  58. void setparity(char *key)
  59. {
  60.    int i;
  61.    for (i = 0; i < 8; i++)
  62.        *(key+i) = oddparity(*(key+i));
  63. }
  64.  
  65.