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

  1. static char *sccsid = "@(#)crypt.c    4.2 (Berkeley) 7/9/81";
  2.  
  3. /*
  4.  *    A one-rotor machine designed along the lines of Enigma
  5.  *    but considerably trivialized.
  6.  */
  7.  
  8. #define ECHO 010
  9. #include <stdio.h>
  10. #define ROTORSZ 256
  11. #define MASK 0377
  12. char    t1[ROTORSZ];
  13. char    t2[ROTORSZ];
  14. char    t3[ROTORSZ];
  15. char    deck[ROTORSZ];
  16. char    *getpass();
  17. char    buf[13];
  18.  
  19. setup(pw)
  20. char *pw;
  21. {
  22.     int ic, i, k, temp, pf[2];
  23.     unsigned random;
  24.     long seed;
  25.  
  26.     strncpy(buf, pw, 8);
  27.     while (*pw)
  28.         *pw++ = '\0';
  29.     buf[8] = buf[0];
  30.     buf[9] = buf[1];
  31.     pipe(pf);
  32.     if (fork()==0) {
  33.         close(0);
  34.         close(1);
  35.         dup(pf[0]);
  36.         dup(pf[1]);
  37.         execl("/usr/lib/makekey", "-", 0);
  38.         execl("/lib/makekey", "-", 0);
  39.         exit(1);
  40.     }
  41.     write(pf[1], buf, 10);
  42.     wait((int *)NULL);
  43.     if (read(pf[0], buf, 13) != 13) {
  44.         fprintf(stderr, "crypt: cannot generate key\n");
  45.         exit(1);
  46.     }
  47.     seed = 123;
  48.     for (i=0; i<13; i++)
  49.         seed = seed*buf[i] + i;
  50.     for(i=0;i<ROTORSZ;i++) {
  51.         t1[i] = i;
  52.         deck[i] = i;
  53.     }
  54.     for(i=0;i<ROTORSZ;i++) {
  55.         seed = 5*seed + buf[i%13];
  56.         random = seed % 65521;
  57.         k = ROTORSZ-1 - i;
  58.         ic = (random&MASK)%(k+1);
  59.         random >>= 8;
  60.         temp = t1[k];
  61.         t1[k] = t1[ic];
  62.         t1[ic] = temp;
  63.         if(t3[k]!=0) continue;
  64.         ic = (random&MASK) % k;
  65.         while(t3[ic]!=0) ic = (ic+1) % k;
  66.         t3[k] = ic;
  67.         t3[ic] = k;
  68.     }
  69.     for(i=0;i<ROTORSZ;i++)
  70.         t2[t1[i]&MASK] = i;
  71. }
  72.  
  73. main(argc, argv)
  74. char *argv[];
  75. {
  76.     register i, n1, n2, nr1, nr2;
  77.     int secureflg = 0;
  78.  
  79.     if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 's') {
  80.         argc--;
  81.         argv++;
  82.         secureflg = 1;
  83.     }
  84.     if (argc != 2){
  85.         setup(getpass("Enter key:"));
  86.     }
  87.     else
  88.         setup(argv[1]);
  89.     n1 = 0;
  90.     n2 = 0;
  91.     nr2 = 0;
  92.  
  93.     while((i=getchar()) >=0) {
  94.         if (secureflg) {
  95.             nr1 = deck[n1]&MASK;
  96.             nr2 = deck[nr1]&MASK;
  97.         } else {
  98.             nr1 = n1;
  99.         }
  100.         i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
  101.         putchar(i);
  102.         n1++;
  103.         if(n1==ROTORSZ) {
  104.             n1 = 0;
  105.             n2++;
  106.             if(n2==ROTORSZ) n2 = 0;
  107.             if (secureflg) {
  108.                 shuffle(deck);
  109.             } else {
  110.                 nr2 = n2;
  111.             }
  112.         }
  113.     }
  114. }
  115.  
  116. shuffle(deck)
  117.     char deck[];
  118. {
  119.     int i, ic, k, temp;
  120.     unsigned random;
  121.     static long seed = 123;
  122.  
  123.     for(i=0;i<ROTORSZ;i++) {
  124.         seed = 5*seed + buf[i%13];
  125.         random = seed % 65521;
  126.         k = ROTORSZ-1 - i;
  127.         ic = (random&MASK)%(k+1);
  128.         temp = deck[k];
  129.         deck[k] = deck[ic];
  130.         deck[ic] = temp;
  131.     }
  132. }
  133.