home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part01 / zeecode.c < prev   
Encoding:
C/C++ Source or Header  |  1987-06-16  |  2.1 KB  |  122 lines

  1. /*
  2.  * Encryption/Decrytion program based on crypt but uses A0 and Zee
  3.  * rather than Rotor and Reflector.
  4.  *
  5.  * Robert W. Baldwin, December 1984.
  6.  */
  7.  
  8.  
  9. #include    <stdio.h>
  10.  
  11.  
  12. #define    BLOCKSIZE    256
  13. #define    MODMASK        (BLOCKSIZE-1)
  14. #define    FALSE        0
  15. #define    TRUE        1
  16.  
  17.  
  18. /* Global state. */
  19.  
  20. int        perm[BLOCKSIZE];    /* Current A permutation. */
  21. int        nxtperm[BLOCKSIZE];    /* Next A permutation. */
  22. int        zee[BLOCKSIZE];        /* Zee permutation. */
  23. int        zeeinv[BLOCKSIZE];    /* Inverse of Zee permutation. */
  24.  
  25.  
  26. char    *permfile = "zeecode.perm";
  27.  
  28.  
  29. /* Do the deed.
  30.  */
  31. main()
  32. {
  33.     int    i;
  34.     int    *curperm;
  35.     FILE *fd;
  36.  
  37.     if ((fd = fopen(permfile, "r")) == NULL)  {
  38.         printf("\nCould not open %s to read permutations.\n", permfile);
  39.         exit(0);
  40.         }
  41.  
  42.     readblock(fd, zee);
  43.     for (i = 0 ; i < BLOCKSIZE ; i++)  zeeinv[zee[i]] = i;
  44.     readblock(fd, perm);
  45.  
  46.     fclose(fd);
  47.  
  48.     while (doblock(perm))  {
  49.         pgate(perm, nxtperm, zee, zeeinv);
  50.         for (i = 0 ; i < BLOCKSIZE ; i++)  perm[i] = nxtperm[i];
  51.         }
  52. }
  53.  
  54.  
  55. /* Compute the permutation after inperm using z and its inverse zi.
  56.  * The result is placed in outperm.
  57.  */
  58. pgate(inperm, outperm, z, zi)
  59. int    *inperm;
  60. int    *outperm;
  61. int    *z;
  62. int    *zi;
  63. {
  64.     int        i,x,v;
  65.     int        w;
  66.  
  67.     for (i = 0 ; i < BLOCKSIZE ; i++) {
  68.         w = -1;
  69.         x = z[i];
  70.         if (x != -1) {
  71.             v = inperm[x&MODMASK];
  72.             if (v != -1)
  73.                 w = zi[v&MODMASK];
  74.             }
  75.         outperm[i] = w;
  76.         }
  77. }
  78.  
  79.  
  80. /* Read character from stdin, encrypt them with the given permutation, p,
  81.  * and write them to stdout.
  82.  * Return FALSE if reach end of file.
  83.  */
  84. doblock(p)
  85. int    p[];
  86. {
  87.     int        pos;
  88.     int        sc;
  89.     char    c;
  90.  
  91.     for (pos = 0 ; pos < BLOCKSIZE ; pos++) {
  92.         if ((c=getchar()) == EOF)  return(FALSE);
  93.         sc = p[MODMASK&(c+pos)];
  94.         if (sc == -1)  {putchar('?');}
  95.         else  {putchar(MODMASK & (sc - pos));}
  96.         }
  97.     return(TRUE);
  98. }
  99.  
  100.  
  101. /* Read a block of BLOCKSIZE integers into the given buffer from
  102.  * the given stream.
  103.  * The block is terminated by a newline character.
  104.  */
  105. readblock(fd, buf)
  106. FILE    *fd;
  107. int        buf[];
  108. {
  109.     int    i;
  110.  
  111.     for (i = 0 ; i < BLOCKSIZE ; i++) {
  112.         if (fscanf(fd, "%3d ", &buf[i]) != 1)  {
  113.             printf("\nReadblock error on i = %d\n", i);
  114.             exit(0);
  115.             }
  116.         }
  117.     if (fscanf(fd, "\n") != 0)  {
  118.         printf("\nReadblock error on newline\n");
  119.         exit(0);
  120.         }
  121. }
  122.