home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / util / super_c / encrpt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  3.8 KB  |  127 lines

  1. /*                      Encrypt
  2.  
  3.         This program encrypts the standard input and writes it to
  4.         the standard output. It requires one parameter: the key for
  5.         the encryption, which can be up to eight characters long.
  6.         Decryption is done by simply running the program again on the
  7.         encrypted file with the same key -- i.e., encryption and
  8.         decryption are the same operation.
  9. */
  10.  
  11. #include <fcntl.h>
  12. #include <io.h>
  13. #include <stdio.h>
  14.  
  15. #define BUFSIZE 20480    /* I/O buffer size. */
  16. #define KEYSIZE 9       /* Maximum size of key (plus one for null). */
  17.  
  18. #define CALIBRATE       /* Define for start/end prompts. */
  19.  
  20. char buffer[BUFSIZE];   /* The I/O buffer. */
  21.  
  22. #define STDINFILE 0     /* Standard input non-stream file number. */
  23. #define STDOUTFILE 1    /* Standard output file number. */
  24.  
  25. #define TRUE 1
  26. #define FALSE 0
  27.  
  28. /*      main(argc,argv)
  29.  
  30.         Function: Encrypt the standard input and write it to the
  31.         standard output, using the single command line parameter
  32.         as the key.
  33.  
  34.         Algorithm: Get the key and rearrange it to improve security,
  35.         then encrypt using exclusive-or with the key.
  36. */
  37.  
  38. main(argc,argv)
  39.  
  40. int argc;
  41. char *argv[];
  42.  
  43. {
  44.         char key[KEYSIZE];      /* The key. */
  45.         char *cp, *cp2;
  46.         char c;
  47.  
  48.         /* Check if we've got the right number of command line
  49.            parameters. */
  50.         if (argc != 2) {
  51.                 /* If not, remind him how to use this program. */
  52.                 fputs("Usage: encrpt key <plainText >cipherText",stderr);
  53.                 exit(1);
  54.         };
  55.  
  56.         /* Check if we've got a decent sized key. */
  57.         if ((argv[1][0] == 0) || (argv[1][1] == 0)) {
  58.                 /* If not, tell him he needs a bigger key. */
  59.                 fputs("Key must be at least 2 characters in size.",stderr);
  60.                 exit(1);
  61.         };
  62.  
  63.         /* Get the key. */
  64.         for (cp = key, cp2 = argv[1];
  65.              (cp < &key[KEYSIZE-1]) && (*cp2 != 0); *cp++ = *cp2++);
  66.         /* Distribute the last byte amoung the top bits of the earlier
  67.            ones, and toggle some of the bits in each key byte. All of
  68.            this helps ensure a more secure key. */
  69.         for (c = *(--cp), *cp = 0; cp >= key; cp--) {
  70.                 *cp |= (c & 1) << 7;
  71.                 *cp ^= 0x55;
  72.                 c >>= 1;
  73.         };
  74.  
  75.         /* Use binary I/O mode. For two reasons: (1) We want to be
  76.            able to encrypt binary files; (2) characters may be
  77.            turned into LFs in the encryption process, which would
  78.            erroneously be translated into CRLF. */
  79.         setmode(STDINFILE,O_BINARY);
  80.         setmode(STDOUTFILE,O_BINARY);
  81.  
  82.         /* Encrypt it. */
  83. #ifdef CALIBRATE
  84.         fputs("Starting...",stderr);
  85. #endif CALIBRATE
  86.         encrypt(STDINFILE,STDOUTFILE,key);
  87. #ifdef CALIBRATE
  88.         fputs("done.\n",stderr);
  89. #endif CALIBRATE
  90. }
  91.  
  92. /*      encrypt(inFile,outFile,theKey)
  93.  
  94.         Function: Encrypt the file specified by inFile, using
  95.         theKey, and write it to outFile.
  96.  
  97.         Algorithm: Exclusive-or the bytes with theKey, recycling
  98.         theKey as needed.
  99. */
  100.  
  101. encrypt(inFile,outFile,theKey)
  102.  
  103. int inFile;
  104. int outFile;
  105. char *theKey;
  106.  
  107. {
  108.         register char *keyPtr;
  109.         register char *bufPtr;
  110.         int bufCnt;
  111.         int sizeRead;
  112.  
  113.         keyPtr = theKey;
  114.         /* Cycle until nothing left to read. */
  115.         while (TRUE) {
  116.                 /* Read in a bufferfull. */
  117.                 sizeRead = bufCnt = read(inFile,buffer,BUFSIZE);
  118.                 if (sizeRead <= 0) break;
  119.                 /* Encrypt it. */
  120.                 for (bufPtr = buffer; sizeRead-- > 0; *bufPtr++ ^= *keyPtr++)
  121.                         if (*keyPtr == 0) keyPtr = theKey;
  122.                 /* Write it out. */
  123.                 write(outFile,buffer,bufCnt);
  124.         };
  125. }
  126.  
  127.