home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / crypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-01-10  |  1.5 KB  |  92 lines

  1. /*
  2.  *    A one-rotor machine designed along the lines of Enigma
  3.  *    but considerably trivialized.
  4.  */
  5.  
  6. #define ECHO 010
  7. #include <stdio.h>
  8. #define ROTORSZ 256
  9. #define MASK 0377
  10. char    t1[ROTORSZ];
  11. char    t2[ROTORSZ];
  12. char    t3[ROTORSZ];
  13. char    *getpass();
  14.  
  15. setup(pw)
  16. char *pw;
  17. {
  18.     int ic, i, k, temp, pf[2];
  19.     unsigned random;
  20.     char buf[13];
  21.     long seed;
  22.  
  23.     strncpy(buf, pw, 8);
  24.     while (*pw)
  25.         *pw++ = '\0';
  26.     buf[8] = buf[0];
  27.     buf[9] = buf[1];
  28.     pipe(pf);
  29.     if (fork()==0) {
  30.         close(0);
  31.         close(1);
  32.         dup(pf[0]);
  33.         dup(pf[1]);
  34.         execl("/usr/lib/makekey", "-", 0);
  35.         execl("/lib/makekey", "-", 0);
  36.         exit(1);
  37.     }
  38.     write(pf[1], buf, 10);
  39.     wait((int *)NULL);
  40.     if (read(pf[0], buf, 13) != 13) {
  41.         fprintf(stderr, "crypt: cannot generate key\n");
  42.         exit(1);
  43.     }
  44.     seed = 123;
  45.     for (i=0; i<13; i++)
  46.         seed = seed*buf[i] + i;
  47.     for(i=0;i<ROTORSZ;i++)
  48.         t1[i] = i;
  49.     for(i=0;i<ROTORSZ;i++) {
  50.         seed = 5*seed + buf[i%13];
  51.         random = seed % 65521;
  52.         k = ROTORSZ-1 - i;
  53.         ic = (random&MASK)%(k+1);
  54.         random >>= 8;
  55.         temp = t1[k];
  56.         t1[k] = t1[ic];
  57.         t1[ic] = temp;
  58.         if(t3[k]!=0) continue;
  59.         ic = (random&MASK) % k;
  60.         while(t3[ic]!=0) ic = (ic+1) % k;
  61.         t3[k] = ic;
  62.         t3[ic] = k;
  63.     }
  64.     for(i=0;i<ROTORSZ;i++)
  65.         t2[t1[i]&MASK] = i;
  66. }
  67.  
  68. main(argc, argv)
  69. char *argv[];
  70. {
  71.     register i, n1, n2;
  72.  
  73.     if (argc != 2){
  74.         setup(getpass("Enter key:"));
  75.     }
  76.     else
  77.         setup(argv[1]);
  78.     n1 = 0;
  79.     n2 = 0;
  80.  
  81.     while((i=getchar()) >=0) {
  82.         i = t2[(t3[(t1[(i+n1)&MASK]+n2)&MASK]-n2)&MASK]-n1;
  83.         putchar(i);
  84.         n1++;
  85.         if(n1==ROTORSZ) {
  86.             n1 = 0;
  87.             n2++;
  88.             if(n2==ROTORSZ) n2 = 0;
  89.         }
  90.     }
  91. }
  92.