home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 193_01 / cypher2.c < prev    next >
Text File  |  1985-11-14  |  2KB  |  76 lines

  1. /*    cypher2.c    Cypher module        by F.A.Scacchitti
  2. **                            10/11/85
  3. **
  4. **    Complex cypher module - generates a key of some prime length
  5. **                              between 1024 and 2028 bytes then
  6. **                              encrypts the buffer with this key
  7. **
  8. */
  9.  
  10. #include <stdio.h>
  11.  
  12. #define NEWBUF 2000
  13. #define NUMPRIMES 50
  14.  
  15. static int i, n, index, length, sum, keylength;
  16. static char *newkey;
  17. static int prime[] = {1009, 1999, 1013, 1997, 1019,
  18.                       1993, 1021, 1987, 1031, 1979,
  19.                       1033, 1973, 1039, 1951, 1049,
  20.                       1949, 1051, 1933, 1061, 1931,
  21.                       1063, 1913, 1069, 1907, 1087,
  22.                       1901, 1091, 1889, 1093, 1879,
  23.                       1097, 1877, 1103, 1873, 1109,
  24.                       1871, 1117, 1867, 1123, 1861,
  25.                       1129, 1847, 1151, 1831, 1153,
  26.                       1823, 1163, 1813, 1171, 1803};
  27.  
  28. cypher1(buffer, num, code) char *buffer, *code; int num;{
  29.  
  30. /*
  31. ** allocate a buffer for the generated key 
  32. */
  33.    newkey = malloc(NEWBUF);
  34.  
  35. /*
  36. ** get keylength and sumcheck for each key
  37. */
  38.  
  39.    keylength = sum = 0;
  40.    while((n = code[keylength]) != NULL){
  41.       sum += n;
  42.       keylength++;
  43.    }
  44.  
  45. /*
  46. ** Select a prime and generate a new key that length
  47. */
  48.  
  49.    length = prime[sum % NUMPRIMES];
  50.  
  51.    printf("-generating a %d byte key\n",length);
  52.  
  53.    for(i=0; i<length; i++){
  54.       index = i % keylength;
  55.       sum = code[index] + sum & 255;
  56.       newkey[i] = code[index] ^ sum;
  57.    }
  58.  
  59. /*
  60. ** encrypt the file with the generated key
  61. */
  62.  
  63.    printf("-encoding/decoding buffer\n");
  64.  
  65.    for(i=0; i<=num; i++)
  66.       buffer[i] = buffer[i] ^ newkey[i % length];
  67.  
  68. /*
  69. ** get rid of the buffer
  70. */
  71.  
  72.    cfree(newkey);
  73.  
  74. }
  75.  
  76.