home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / DDJ0190.ZIP / STOUT.LST < prev    next >
File List  |  1989-12-26  |  10KB  |  335 lines

  1. _S-CODER FOR DATA ENCRYPTION_
  2. by Robert Stout
  3.  
  4. [LISTING ONE]
  5.  
  6. /****************************************************************/
  7. /*      Simple S-CODER file encryptor/decryptor                 */
  8. /****************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <assert.h>
  13.  
  14. #define BUF_SIZ 32768
  15.  
  16. extern char *cryptext;
  17. extern int  crypt_length;
  18. void crypt(char *);
  19.  
  20. main(int argc, char *argv[])
  21. {
  22.         unsigned i, n;
  23.         char *buf, *p;
  24.         FILE *infile, *outfile;
  25.  
  26.         if (4 > argc)
  27.         {
  28.                 puts("\aUsage: CRYPT input_file output_file key");
  29.                 abort();
  30.         }
  31.         assert(buf = (char *)malloc(BUF_SIZ));
  32.         assert(infile  = fopen(argv[1], "rb"));
  33.         assert(outfile = fopen(argv[2], "wb"));
  34.         cryptext = argv[3];
  35.         crypt_length = strlen(cryptext);
  36.         while (n = fread(buf, 1, BUF_SIZ, infile))
  37.         {
  38.                 p = buf;
  39.                 for (i = 0; i < n; ++i)
  40.                         crypt(p++);
  41.                 fwrite(buf, 1, n, outfile);
  42.         }
  43.         fclose(infile);
  44.         fclose(outfile);
  45.         exit(0);
  46. }
  47.  
  48.  
  49. [LISTING TWO]
  50.  
  51. /****************************************************************/
  52. /*      Simple S-CODER stream encryptor/decryptor               */
  53. /****************************************************************/
  54.  
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <assert.h>
  58.  
  59. extern char *cryptext;
  60. extern int  crypt_length;
  61. void crypt(char *);
  62.  
  63. main(int argc, char *argv[])
  64. {
  65.         char cch;
  66.         int ich;
  67.         FILE *infile;
  68.         void setraw(void);
  69.  
  70.         if (2 > argc)
  71.         {
  72.                 puts("\aUsage: SCRYPT key");
  73.                 puts("encrypts stdin to stdout");
  74.                 abort();
  75.         }
  76.         cryptext = argv[1];
  77.         crypt_length = strlen(cryptext);
  78.         setraw();  /* NOTE: setraw() will be compiler-dependent. It is used 
  79.                       to set stdin and stdout to raw binary mode. This is 
  80.                       necessary to avoid CR/LF translation and to avoid 
  81.                       sensing 0x1a as EOF during decryption. */
  82.         while (EOF != (ich = getchar()))
  83.         {
  84.                 cch = (char)ich;
  85.                 crypt(&cch);
  86.                 fputc(cch, stdout);
  87.         }
  88.         exit(0);
  89. }
  90.  
  91.  
  92.  
  93. [LISTING THREE] 
  94.  
  95. /****************************************************************/
  96. /*      Zortech C routine to set stin and stdout to binary mode */
  97. /****************************************************************/
  98.  
  99. #include <stdio.h>
  100.  
  101. extern FILE _iob[_NFILE];
  102.  
  103. void setraw(void)
  104. {
  105.         _iob[0]._flag &= ~_IOTRAN;
  106.         _iob[1]._flag &= ~_IOTRAN;
  107. }
  108.  
  109.  
  110.  
  111. [LISTING FOUR]
  112.  
  113. /****************************************************************/
  114. /*      Enhanced security S-CODER file encryptor/decryptor      */
  115. /****************************************************************/
  116.  
  117. #include <stdio.h>
  118. #include <stdlib.h>
  119. #include <string.h>
  120. #include <assert.h>
  121.  
  122. #define MIN_KEYL 6
  123.  
  124. extern char *cryptext;
  125. extern int  crypt_length;
  126. void crypt(char *);
  127. int cryptqual(void);
  128. long fsize;
  129. union {                         /* Transposition cipher buffer  */
  130.         char in[16384];
  131.         char out[128][128];
  132. } buf;
  133. FILE *infile, *outfile;
  134.  
  135. main(int argc, char *argv[])
  136. {
  137.         void encrypt(void);
  138.         void decrypt(void);
  139.  
  140.         if (5 > argc || NULL == strchr("EeDd", argv[1][0]))
  141.         {
  142.                 puts("\aUsage: HI-CRYPT { E | D } input_file output_file key");
  143.                 puts("where: E = Encrypt");
  144.                 puts("       D = Decrypt");
  145.                 abort();
  146.         }
  147.         assert(infile  = fopen(argv[2], "rb"));
  148.         assert(outfile = fopen(argv[3], "wb"));
  149.         cryptext = argv[4];
  150.         crypt_length = strlen(cryptext);
  151.         if (cryptqual())
  152.         {
  153.                 puts("\aHI-CRYPT: Key is not sufficiently complex");
  154.                 abort();
  155.         }
  156.         if (strchr("Ee", argv[1][0]))
  157.                 encrypt();
  158.         else    decrypt();
  159.         fclose(infile);
  160.         fclose(outfile);
  161.         exit(0);
  162. }
  163. int cryptqual(void)
  164. {
  165.         int i, j = 0;
  166.         static char found[MIN_KEYL + 1];   /* Statics initialized to zeros */
  167.  
  168.         if (6 > crypt_length)
  169.                 return -1;
  170.         for (i = 0; i < crypt_length; ++i)
  171.         {
  172.                 if (strchr(found, cryptext[i]))
  173.                         continue;
  174.                 found[j++] = cryptext[i];
  175.                 if ((MIN_KEYL - 1) < j)
  176.                         return 0;
  177.         }
  178.         return -1;
  179. }
  180. void encrypt(void)
  181. {
  182.         unsigned i, j, n;
  183.  
  184.         fseek(infile, 0L, SEEK_END);
  185.         fsize = ftell(infile);                          /* Save size    */
  186.         rewind(infile);
  187.         fwrite(&fsize, sizeof(long), 1, outfile);
  188.         srand((unsigned)fsize);
  189.         crypt_ptr = fsize % crypt_length;
  190.         while (n = fread(buf.in, 1, 16384, infile))
  191.         {
  192.                 while (16384 > n)
  193.                         buf.in[n++] = rand();
  194.                 for (i = 0; i < 128; ++i)
  195.                         for (j = 0; j < 128; ++j)
  196.                                 crypt(&buf.out[j][i]);
  197.                 fwrite(buf.in, 1, 16384, outfile);
  198.         }
  199. }
  200. void decrypt(void)
  201. {
  202.         unsigned i, j, n;
  203.         fread(&fsize, sizeof(long), 1, infile);
  204.         crypt_ptr = fsize % crypt_length;
  205.         while (n = fread(buf.in, 1, 16384, infile))     /* Read size    */
  206.         {
  207.                 for (i = 0; i < 128; ++i)
  208.                         for (j = 0; j < 128; ++j)
  209.                                 crypt(&buf.out[j][i]);
  210.                 if (16384 <= fsize)
  211.                         fwrite(buf.in, 1, 16384, outfile);
  212.                 else    fwrite(buf.in, 1, fsize, outfile);
  213.                 fsize -= n;
  214.         }
  215. }
  216.  
  217.  
  218.  
  219. [LISTING FIVE]
  220.  
  221. /****************************************************************/
  222. /*      Collect file statistics                                 */
  223. /****************************************************************/
  224.  
  225. #include <stdio.h>
  226. #include <math.h>
  227. #include <assert.h>
  228.  
  229. main(int argc, char *argv[])
  230. {
  231.         int i, ch, hist = 0;
  232.         long n = 0L;
  233.         double mean = 0., stdev = 0., ftmp;
  234.         static unsigned bins[256];
  235.         FILE *infile;
  236.  
  237.         assert(infile = fopen(argv[1], "rb"));
  238.         while (!feof(infile))
  239.         {
  240.                 if (EOF == (ch = fgetc(infile)))
  241.                         break;
  242.                 bins[ch] += 1;
  243.                 ++n;
  244.         }
  245.         fclose(infile);
  246.         for (i = 0; i < 256; ++i)
  247.         {
  248.                 mean += (double)(bins[i]);
  249.                 if (bins[i])
  250.                         ++hist;
  251.         }
  252.         mean /= 256.;
  253.         for (i = 0; i < 256; ++i)
  254.         {
  255.                 ftmp = (double)(bins[i]) - mean;
  256.                 stdev += (ftmp * ftmp);
  257.         }
  258.         ftmp  = stdev / 255.;
  259.         stdev = sqrt(ftmp);
  260.         printf("%ld Characters were read from %s\n"
  261.                 "There are an average of %f occurances of each character\n"
  262.                 "%d Characters out of 256 possible were used\n"
  263.                 "The standard deviation is %f\n"
  264.                 "The coefficient of variation is %f%%\n",
  265.                 n, argv[1], mean, hist, stdev, (100. * stdev) / mean);
  266. }
  267.  
  268.         
  269.  
  270.  
  271. Figure 1: A polyalphabetic substitution cipher 
  272.  
  273.  
  274.  
  275. /****************************************************************/
  276. /* Simple encrypt/decrypt function using exclusive-ORing        */
  277. /* NOTE: This is included for demonstration only! Data encrypted*/ 
  278. /* with this code will be subject to simple cryptanalysis.      */
  279. /****************************************************************/
  280. char *cryptext;                /* The encryption/decryption key */
  281. void crypt(char *buf)
  282. {
  283.         int crypt_ptr = 0;     /* Circular pointer to elements of key  */
  284.         *buf ^= cryptext[crypt_ptr];
  285.         if (++crypt_ptr >= strlen(cryptext))
  286.                 crypt_ptr = 0;
  287. }
  288.  
  289.  
  290.  
  291. Figure 2:  The S-CODER algorithm 
  292.  
  293. /****************************************************************/
  294. /*      S-CODER - Encrypt/decrypt data                          */
  295. /*      Copyright 1987-1989 by Robert B. Stout dba MicroFirm    */
  296. /*      Originally written by Bob Stout with modifications      */
  297. /*      suggested by Mike Smedley.                              */
  298. /*      This code may be used freely in any program for any     */
  299. /*      application, personal or commercial.                    */
  300. /*  Current commercial availability:                            */
  301. /*      1. MicroFirm Toolkit ver 3.00: LYNX and CRYPT utilities */
  302. /*      2. CXL libraries (MSC, TC, ZTC/C++, PC): fcrypt()       */
  303. /*         dedicated file encryption function                   */
  304. /*      3. SMTC & SMZT libraries: crypt() function              */
  305. /****************************************************************/
  306.  
  307. char *cryptext;         /* The actual encryption/decryption key */
  308. int   crypt_ptr = 0;    /* Circular pointer to elements of key  */
  309. int   crypt_length;     /* Set externally to strlen(cryptext)   */
  310.  
  311. /* NOTES: cryptext should be set and qualified (to something over
  312.           5-6 chars, minimum) by the calling program, which should
  313.           also set crypt_ptr in the range of 0 to strlen(cryptext)
  314.           before each use. If crypt() is used to encrypt several
  315.           buffers, cryptext should be reloaded and crypt_ptr reset
  316.           before each buffer is encrypted. The encryption is both
  317.           reversible - to decrypt data, pass it back through crypt()
  318.           using the original key and original initial value of
  319.           crypt_ptr - and multiple passes are commutative. */
  320.  
  321. /**** Encrypt/decrypt buffer datum ******************************/
  322. void crypt(char *buf)
  323. {
  324.         *buf ^= cryptext[crypt_ptr] ^ (cryptext[0] * crypt_ptr);
  325.         cryptext[crypt_ptr] += ((crypt_ptr < (crypt_length - 1)) ?
  326.                 cryptext[crypt_ptr + 1] : cryptext[0]);
  327.         if (!cryptext[crypt_ptr])
  328.                 cryptext[crypt_ptr] += 1;
  329.         if (++crypt_ptr >= crypt_length)
  330.                 crypt_ptr = 0;
  331. }
  332.  
  333.  
  334.  
  335.