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

  1. /*    cypher3.c    Cypher module        by F.A.Scacchitti
  2. **                            11/09/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. **                        or
  8. **                if key starts with a '-' (dash)
  9. **                calculate a transposition block size
  10. **                and invert (transpose) the file in
  11. **                this size blocks 
  12. **
  13. */
  14.  
  15. #include <stdio.h>
  16.  
  17. #define DASH 45
  18. #define NEWBUF 2000
  19. #define NUMPRIMES 50
  20.  
  21. static int i, j, n, index, length, sum, keylength;
  22. static char *tbuff, c;
  23. static int prime[] = {1009, 1999, 1013, 1997, 1019,
  24.                       1993, 1021, 1987, 1031, 1979,
  25.                       1033, 1973, 1039, 1951, 1049,
  26.                       1949, 1051, 1933, 1061, 1931,
  27.                       1063, 1913, 1069, 1907, 1087,
  28.                       1901, 1091, 1889, 1093, 1879,
  29.                       1097, 1877, 1103, 1873, 1109,
  30.                       1871, 1117, 1867, 1123, 1861,
  31.                       1129, 1847, 1151, 1831, 1153,
  32.                       1823, 1163, 1813, 1171, 1803};
  33.  
  34. cypher(buffer, num, code) char *buffer, *code; int num;{
  35.  
  36. /*
  37. ** allocate a buffer for the new key or transposition
  38. */
  39.    tbuff = malloc(NEWBUF);
  40.  
  41. /*
  42. ** get keylength and sumcheck for each key
  43. */
  44.  
  45.    keylength = sum = 0;
  46.    while((n = code[keylength]) != NULL){
  47.       sum += n;
  48.       keylength++;
  49.    }
  50.  
  51. /*
  52. ** do we transpose or encode ?
  53. */
  54.  
  55.    if((c = *code) == DASH)
  56.       transpose(buffer, num, code);
  57.    else
  58.       encode(buffer, num, code);
  59.  
  60. /*
  61. ** get rid of the buffer
  62. */
  63.  
  64.    cfree(tbuff);
  65.  
  66. }
  67.  
  68. /*
  69. ** Here's where we transpose
  70. */
  71.  
  72. transpose(buffer, num, code) char *buffer, *code; int num;{
  73.  
  74.    length = (((sum + keylength) % 16) & 15) + 2;
  75.  
  76.    printf("-transposing file by %d\n",length);
  77.  
  78.    index = 0;
  79.  
  80.    do{
  81.       for(i = 0; i < length; i++){
  82.          j = length - i - 1;
  83.          tbuff[j] = buffer[index + i];
  84.       }
  85.       for(i = 0; i < length; i++){
  86.          buffer[index + i] = tbuff[i];
  87.       }
  88.       index += length;
  89.  
  90.    }while(index < count);
  91.  
  92. }
  93.  
  94. /*
  95. ** Here's where we encode
  96. */
  97.  
  98. encode(buffer, num, code) char *buffer, *code; int num;{
  99.  
  100. /*
  101. ** Select a prime and generate a new key that length
  102. */
  103.  
  104.    length = prime[sum % NUMPRIMES];
  105.  
  106.    printf("-generating a %d byte key\n",length);
  107.  
  108.    for(i=0; i<length; i++){
  109.       index = i % keylength;
  110.       sum = code[index] + sum & 255;
  111.       tbuff[i] = code[index] ^ sum;
  112.    }
  113.  
  114. /*
  115. ** encrypt the file with the generated key
  116. */
  117.  
  118.    printf("-encoding/decoding buffer\n");
  119.  
  120.    for(i=0; i<=num; i++)
  121.       buffer[i] = buffer[i] ^ tbuff[i % length];
  122.  
  123.  
  124. }
  125.  
  126.