home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / SECRET.ZIP / CIPHER.C next >
Encoding:
C/C++ Source or Header  |  1987-02-14  |  2.5 KB  |  100 lines

  1. /* #include "cipher.c"
  2.  
  3.     USAGE:   set_crypt(keystring, seedinteger);  < string ciphers >
  4.          encode( target1string );
  5.          encode( target2string );
  6.          ... etc.
  7.     
  8.          set_crypt(keystring, seedinteger);
  9.          decode( target1string );
  10.          decode( target2string );
  11.          ... etc.
  12.  
  13.     OR:     set_crypt(key, seed);   < single-character ciphers >
  14.          c0 = cryptic(c0); 
  15.          c1 = cryptic(c1); 
  16.          ... ; 
  17.          cN = cryptic(cN); 
  18.  
  19.          set_crypt(key, seed);
  20.          c0 = uncrypt(c0); 
  21.          c1 = uncrypt(c1); 
  22.          ... ; 
  23.          cN = uncrypt(cN); 
  24.  
  25. */
  26.  
  27.  
  28.  
  29. /*=======================================================================*/
  30. /*  An Elementary Holocryptic Cipher:                     */
  31. /*  The following is a variation on what was described as Hogg's code    */
  32. /*  from World War I in the Sept. 1984 FOGHORN (First Osborne Group).     */
  33. /*  The point of the additional bells & whistles is to make the entire   */
  34. /*  text of the Zaphod Adventure as nearly holocryptic as possible.     */
  35. /*  It works by spinning the key modulus on successive lines; so, while  */
  36. /*  line appearance is preserved, analysis of the characters of each     */
  37. /*  line depends on recovering the initial LCG seed, as well as the key. */ 
  38. /*  Bright adventurers are discouraged from trying to cheat by dumping   */
  39. /*  the Zaphod files, hopefully.  -D. C. Oshel, 10/6/84             */
  40. /*=======================================================================*/
  41.  
  42. static int kloc, klen;
  43. static unsigned kRNDlxo;
  44. static char kQey[32];
  45.  
  46. set_crypt(key,seed) char *key; int seed; {
  47.  
  48. static int i;
  49.  
  50.     i = 0; 
  51.     while (*key, i < 31) kQey[i++] = *key++; /* local copy of the key */
  52.     kQey[i] = '\0';
  53.     kRNDlxo = seed; klen = strlen(kQey); Krandi();
  54.  
  55. } /* end: set_crypt */
  56.  
  57. encode(p) char *p; {
  58.  
  59.     while (*p) *p = cryptic(*p++); Krandi();
  60.  
  61. } /* end: encode */
  62.  
  63. decode(p) char *p; {
  64.  
  65.     while (*p) *p = uncrypt(*p++); Krandi();
  66.  
  67. } /* end: decode */
  68.  
  69. Krandi() {
  70.  
  71.     kRNDlxo = 2053 * kRNDlxo + 13849; /* a long-period LCG */
  72.     kloc = (abs( kRNDlxo )) % klen;
  73.  
  74. } /* end: Krandi */
  75.  
  76. cryptic( c ) int c; {
  77.  
  78.     if ( c > 31 && c < 127 ) {
  79.         c = ( (c - 32) + (kQey[kloc++] - 32) ) % 95;
  80.         kloc %= klen;
  81.         return ( c + 32 );
  82.     }
  83.     else return ( c );
  84.  
  85. } /* end: cryptic */
  86.  
  87. uncrypt( c ) int c; {
  88.  
  89.     if ( c > 31 && c < 127 ) { 
  90.         c -= 32; c -= kQey[kloc++] - 32; 
  91.         kloc %= klen;
  92.         if ( c < 0 ) c += 95;
  93.         return ( c + 32 );
  94.     }
  95.     else return ( c ); 
  96.  
  97. } /* end: uncrypt */ 
  98.  
  99. /* end of "cipher.c" */
  100.