home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / term33so.lha / termCrypt.c < prev    next >
C/C++ Source or Header  |  1993-04-30  |  3KB  |  129 lines

  1. /*
  2. **    termCrypt.c
  3. **
  4. **    Data encryption routines.
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* The width of the cell ring is defined here. A word of warning: never
  13.      * encrypt a file and forget the access password, since it will be
  14.      * extremely hard to break the code (with 30 cells there are 256^30
  15.      * possible values each cell may be initialized to which means that
  16.      * you could have to try more than 1.7e+72 combinations for each single
  17.      * password character before actually finding the matching password).
  18.      * See `Random sequence generation by cellular automata' by Steven
  19.      * Wolfram (Institute for Advanced Study; Advances in Applied
  20.      * Mathematics) for more information.
  21.      */
  22.  
  23. #define CELL_WIDTH 32
  24.  
  25.     /* The cell ring and the ring index pointers. */
  26.  
  27. STATIC UBYTE Cell[2][CELL_WIDTH],*Src,*Dst;
  28.  
  29.     /* The pattern the cell ring will be filled with. The pattern
  30.      * may seem familiar: these are the first 32 digits of Pi.
  31.      */
  32.  
  33. STATIC UBYTE Pi[32] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5 };
  34.  
  35.     /* Automaton():
  36.      *
  37.      *    A cellular automaton working on a ring of cells, producing
  38.      *    random data in each single cell.
  39.      */
  40.  
  41. STATIC UBYTE
  42. Automaton(VOID)
  43. {
  44.     register UBYTE    *A = Src,
  45.             *B = Dst;
  46.     register WORD     i;
  47.  
  48.         /* Operate on the cell ring... */
  49.  
  50.     for(i = 1 ; i < CELL_WIDTH - 1 ; i++)
  51.         B[i] = A[i - 1] ^ (A[i] | A[i + 1]);
  52.  
  53.         /* Operate on first and last element. */
  54.  
  55.     B[0]            = A[CELL_WIDTH - 1] ^ (A[0]              | A[1]);
  56.     B[CELL_WIDTH - 1]    = A[CELL_WIDTH - 2] ^ (A[CELL_WIDTH - 1] | A[0]);
  57.  
  58.         /* Swap cell rings. */
  59.  
  60.     {
  61.         register UBYTE *Swap;
  62.  
  63.         Swap    = Src;
  64.         Src    = Dst;
  65.         Dst    = Swap;
  66.     }
  67.  
  68.         /* Return contents of first cell. */
  69.  
  70.     return(A[0]);
  71. }
  72.  
  73.     /* Encrypt(UBYTE *Source,UBYTE *Destination,UBYTE *Key):
  74.      *
  75.      *    Encrypt data using cellular automaton as a random number generator.
  76.      */
  77.  
  78. VOID
  79. Encrypt(UBYTE *Source,WORD SourceLen,UBYTE *Destination,UBYTE *Key,WORD KeyLen)
  80. {
  81.     register WORD i;
  82.  
  83.         /* Set up cell ring index pointers. */
  84.  
  85.     Src = Cell[0];
  86.     Dst = Cell[1];
  87.  
  88.     if(KeyLen < 32)
  89.         memcpy(&Src[KeyLen],Pi,32 - KeyLen);
  90.  
  91.     memcpy(Src,Key,KeyLen);
  92.  
  93.         /* Encrypt the source data. */
  94.  
  95.     for(i = 0 ; i < SourceLen ; i++)
  96.         *Destination++ = ((WORD)Source[i] + Automaton()) % 256;
  97. }
  98.  
  99.     /* Decrypt(UBYTE *Source,UBYTE *Destination,UBYTE *Key):
  100.      *
  101.      *    Decrypt data using cellular automaton as a random number generator.
  102.      */
  103.  
  104. VOID
  105. Decrypt(UBYTE *Source,WORD SourceLen,UBYTE *Destination,UBYTE *Key,WORD KeyLen)
  106. {
  107.     register WORD i,Code;
  108.  
  109.         /* Set up cell ring index pointers. */
  110.  
  111.     Src = Cell[0];
  112.     Dst = Cell[1];
  113.  
  114.     if(KeyLen < 32)
  115.         memcpy(&Src[KeyLen],Pi,32 - KeyLen);
  116.  
  117.     memcpy(Src,Key,KeyLen);
  118.  
  119.         /* Decrypt the source data. */
  120.  
  121.     for(i = 0 ; i < SourceLen ; i++)
  122.     {
  123.         if((Code = Source[i] - Automaton()) < 0)
  124.             Code += 256;
  125.  
  126.         *Destination++ = Code;
  127.     }
  128. }
  129.