home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / des2.zip / encrypt.c < prev    next >
Text File  |  1993-03-19  |  3KB  |  93 lines

  1. /* ---------------------- encrypto.c ----------------------- */
  2. /* Single key text file encryption
  3.  * Usage: encrypto keyvalue infile outfile
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define FALSE 0
  11. #define TRUE !FALSE
  12.  
  13. static void charout(FILE *fo, char prev, int runct, int last);
  14. static void encrypt(FILE *fo, char ch, int last);
  15.  
  16. static char *key = NULL;
  17. static int keylen;
  18. static char *cipher = NULL;
  19. static int clen = 0;
  20.  
  21. void main(int argc, char *argv[])
  22. {
  23.    FILE *fi, *fo;
  24.    char ch, prev = 0;
  25.    int runct = 0;
  26.  
  27.    if (argc > 3)    {
  28.        /* --- alloc memory for the key and cipher blocks --- */
  29.        keylen = strlen(argv[1]);
  30.        cipher = malloc(keylen+1);
  31.        key = malloc(keylen+1);
  32.        strcpy(key, argv[1]);
  33.  
  34.        if (cipher != NULL && key != NULL &&
  35.                (fi = fopen(argv[2], "rb")) != NULL)        {
  36.            if ((fo = fopen(argv[3], "wb")) != NULL)    {
  37.                while ((ch = fgetc(fi)) != EOF)    {
  38.                     /* ---- validate ASCII input ---- */
  39.                    if (ch & 128)    {
  40.                        fprintf(stderr, "%s is not ASCII",
  41.                                    argv[2]);
  42.                        fclose(fi);
  43.                        fclose(fo);
  44.                        remove(argv[3]);
  45.                        free(cipher);
  46.                        free(key);
  47.                        exit(1);
  48.                    }
  49.  
  50.                    /* --- test for duplicate bytes --- */
  51.                    if (ch == prev && runct < 127)
  52.                        runct++;
  53.                    else    {
  54.                        charout(fo, prev, runct, FALSE);
  55.                        prev = ch;
  56.                        runct = 0;
  57.                    }
  58.                }
  59.                charout(fo, prev, runct, TRUE);
  60.                fclose(fo);
  61.            }
  62.            fclose(fi);
  63.        }
  64.        if (cipher)
  65.            free(cipher);
  66.        if (key)
  67.            free(key);
  68.    }
  69. }
  70.  
  71. /* ------- send an encrypted byte to the output file ------ */
  72. static void charout(FILE *fo, char prev, int runct, int last)
  73. {
  74.    if (runct)
  75.        encrypt(fo, (runct+1) | 0x80, last);
  76.    if (prev)
  77.        encrypt(fo, prev, last);
  78. }
  79.  
  80. /* ---------- encrypt a byte and write it ---------- */
  81. static void encrypt(FILE *fo, char ch, int last)
  82. {
  83.    *(cipher+clen) = ch ^ *(key+clen);
  84.    clen++;
  85.    if (last || clen == keylen)    {
  86.        /* ----- cipher buffer full or last buffer ----- */
  87.        int i;
  88.        for (i = 0; i < clen; i++)
  89.            fputc(*(cipher+i), fo);
  90.        clen = 0;
  91.    }
  92. }
  93.