home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / pc / crypto / modified.txt < prev    next >
Encoding:
Text File  |  2003-06-11  |  10.1 KB  |  365 lines

  1. cat crypt
  2. From philip@axis.UUCP Sun Feb 15 14:33:00 1987
  3. Path: beno!seismo!mcvax!inria!axis!philip
  4. From: philip@axis.UUCP
  5. Newsgroups: net.sources
  6. Subject: A crypt program
  7. Keywords: A public domain crypt program
  8. Message-ID: <177@axis.UUCP>
  9. Date: 15 Feb 87 19:33:00 GMT
  10. Organization: Axis Digital, Paris
  11. Lines: 262
  12. Posted: Sun Feb 15 19:33:00 1987
  13.  
  14.  
  15.                         Crypt - Decrypt Program
  16.                         ~~~~~~~~~~~~~~~~~~~~~~~
  17.  
  18. This program is based upon the German WW2 enigma machine.
  19.  
  20. This worked as follows:
  21.  
  22. The machine contained several (3 or 4, depending upon the service) rotors,
  23. with contacts on each face. Each rotor had a different mapping (ie wiring)
  24. scheme between the contacts.
  25.  
  26. Each contact on the input side of rotor 1 was connected to input keys
  27. (letters and numbers). Pressing the key marked 'A' would pass an electric
  28. current into the corresponding contact on the input rotor, this current would
  29. exit on some other contact on the output side, depending upon the internal
  30. wiring.
  31.  
  32. After each key press, the input rotor would turn one position (thus, pressing
  33. 'A' again, would result in a current flowing out of a different output
  34. contact.
  35.  
  36. After completing one revolution, rotor number two would move one position.
  37.  
  38. The key to this depended upon the order of the rotors, and their starting
  39. positions.
  40.  
  41. To decode, simply reverse the current flow, ie, connect the keyboard to the
  42. output, and replace the input by a series of lamps corresponding to the
  43. keys.
  44.  
  45. This program simulates one of these machines, with several refinements:
  46.  
  47. 1)      Each rotor has 256 positions
  48.  
  49. 2)      The key is not the ordering and starting positions of the rotors,
  50.         since new and different rotors are created for each input key.
  51.  
  52. 3)      The movement of the rotors is not a simple as described above.
  53.         It is basically that, but with rotors selected on a random basis,
  54.         they are advanced a random number of positions.
  55.  
  56. The result is (I believe - not being a Cryptologist), somewhat more
  57. secure than the original machines, and certainly more secure than the
  58. original UNIX crypt - which has a single rotor, with a reciprocal
  59. mapping - ie if an input of 'A' maps to an output of 'Z', then in the
  60. same rotor position, a 'Z' must map to an 'A'.
  61.  
  62. if anyone finds a method of breaking this crypt program, I would be glad
  63. to hear about it.
  64.  
  65. Philip Peake    (philip@axis.uucp)
  66.  
  67. NOTES:
  68.         The program must have two links, one called crypt, and the other
  69.         called decrypt. Passing a crypted document through crypt again,
  70.         even with the same key, will not decrypt it.
  71.  
  72. ===============================================================================
  73. #include <stdio.h>
  74.  
  75. #define ROTORSIZ        256
  76. #define MASK            0377
  77. #define EMPTY           07777
  78. #define X_SIZE          4099
  79.  
  80. char    *strrchr();
  81.  
  82. unsigned        r1[ROTORSIZ];
  83. unsigned        r2[ROTORSIZ];
  84. unsigned        r3[ROTORSIZ];
  85.  
  86. unsigned char   x[X_SIZE];
  87.  
  88. init(password, decrypt)
  89. char    *password;
  90. int     decrypt;
  91. {
  92.         register int    index;
  93.         register int    i;
  94.         int             pipe_fd[2];
  95.         unsigned        random;
  96.         long            seed = 123L;
  97.         char            buf[13];
  98.  
  99.         strncpy(buf, password, 8);
  100.         while (*password) *password++ = '\0';
  101.         buf[8] = buf[0];
  102.         buf[9] = buf[1];
  103.  
  104.         pipe(pipe_fd);
  105.  
  106.         if (fork() == 0)
  107.         {
  108.                 close(0);
  109.                 close(1);
  110.                 dup(pipe_fd[0]);
  111.                 dup(pipe_fd[1]);
  112.                 execl("/usr/lib/makekey", "-", 0);
  113.                 execl("/lib/makekey", "-", 0);
  114.                 exit(1);
  115.         }
  116.  
  117.         write(pipe_fd[1], buf, 10);
  118.         wait((int *) NULL);
  119.  
  120.         if (read(pipe_fd[0], buf, 13) != 13)
  121.         {
  122.                 fprintf(stderr, "crypt: cannot generate key\n");
  123.                 exit(1);
  124.         }
  125.  
  126.         for (i = 0 ; i < ROTORSIZ; i++) r1[i] = r2[i] = r3[i] = EMPTY;
  127.  
  128.         for (i = 2; i < 13; i++) seed = seed * buf[i] + i;
  129.  
  130.         i = 0;
  131.         while (i < ROTORSIZ)
  132.         {
  133.                 seed = (long)(5L * seed + (long)i);
  134.                 random = (unsigned)(seed % 65521L);
  135.                 index = (int)(random & MASK);
  136.                 if (r1[index] == EMPTY)
  137.                         r1[index] = i++;
  138.                 else
  139.                         continue;
  140.         }
  141.  
  142.         i = 0;
  143.         while (i < ROTORSIZ)
  144.         {
  145.                 seed = (long)(5L * seed + (long)i);
  146.                 random = (unsigned)(seed % 65521L);
  147.                 index = (int)(random & MASK);
  148.                 if (r2[index] == EMPTY)
  149.                         r2[index] = i++;
  150.                 else
  151.                         continue;
  152.         }
  153.  
  154.         i = 0;
  155.         while (i < ROTORSIZ)
  156.         {
  157.                 seed = (long)(5L * seed + (long)i);
  158.                 random = (unsigned)(seed % 65521L);
  159.                 index = (int)(random & MASK);
  160.                 if (r3[index] == EMPTY)
  161.                         r3[index] = i++;
  162.                 else
  163.                         continue;
  164.         }
  165.  
  166.         for (i = 0; i < X_SIZE; i++)
  167.         {
  168.                 seed = (long)(5L * seed + (long)i);
  169.                 random = (unsigned)(seed % 65521L);
  170.                 x[i] = random & 03;
  171.         }
  172.  
  173.         if (decrypt)
  174.         {
  175.                 invert(r1);
  176.                 invert(r2);
  177.                 invert(r3);
  178.         }
  179. }
  180.  
  181. invert(r)
  182. unsigned r[ROTORSIZ];
  183. {
  184.         unsigned        t[ROTORSIZ];
  185.         register int    i;
  186.  
  187.         for (i = 0; i < ROTORSIZ; i++) t[i] = r[i];
  188.         for (i = 0; i < ROTORSIZ; i++) r[t[i]] = i;
  189. }
  190.  
  191. crypt()
  192. {
  193.         register        int             ch;
  194.         register        int             i    = 0;
  195.         register        unsigned        ofs1 = 0;
  196.         register        unsigned        ofs2 = 0;
  197.         register        unsigned        ofs3 = 0;
  198.  
  199.         while ((ch = getchar()) != EOF)
  200.         {
  201.                 putchar(r3[r2[r1[ch+ofs1&MASK]+ofs2&MASK]+ofs3&MASK]);
  202.  
  203.                 switch (x[i]){
  204.                 case 00:
  205.                                 ofs1 = ++ofs1 & MASK;
  206.                                 break;
  207.                 case 01:
  208.                                 ofs2 = ++ofs2 & MASK;
  209.                                 break;
  210.                 case 02:
  211.                                 ofs3 = ++ofs3 & MASK;
  212.                                 break;
  213.                 }
  214.  
  215.                 if (ofs1 == 0) ofs2 = ++ofs2 & MASK;
  216.                 if (ofs2 == 0) ofs3 = ++ofs3 & MASK;
  217.  
  218.                 if (++i == X_SIZE) i = 0;
  219.         }
  220. }
  221.  
  222. decrypt()
  223. {
  224.         register        int             ch;
  225.         register        int             i    = 0;
  226.         register        unsigned        ofs1 = 0;
  227.         register        unsigned        ofs2 = 0;
  228.         register        unsigned        ofs3 = 0;
  229.  
  230.         while ((ch = getchar()) != EOF)
  231.         {
  232.                 putchar(r1[r2[r3[ch]-ofs3&MASK]-ofs2&MASK]-ofs1&MASK);
  233.  
  234.                 switch (x[i]){
  235.                 case 00:
  236.                                 ofs1 = ++ofs1 & MASK;
  237.                                 break;
  238.                 case 01:
  239.                                 ofs2 = ++ofs2 & MASK;
  240.                                 break;
  241.                 case 02:
  242.                                 ofs3 = ++ofs3 & MASK;
  243.                                 break;
  244.                 }
  245.  
  246.                 if (ofs1 == 0) ofs2 = ++ofs2 & MASK;
  247.                 if (ofs2 == 0) ofs3 = ++ofs3 & MASK;
  248.  
  249.                 if (++i == X_SIZE) i = 0;
  250.         }
  251. }
  252.  
  253. main(argc, argv)
  254. int     argc;
  255. char    *argv[];
  256. {
  257.         int     flag;
  258.         char    *p;
  259.  
  260.         p = strrchr(argv[0], '/');
  261.  
  262.         if (p == NULL) p = argv[0];
  263.         else ++p;
  264.  
  265.         if (strcmp(p, "crypt") == 0) flag = 0;
  266.         else                               flag = 1;
  267.  
  268.         if (argc != 2)
  269.                 init(getpass("Enter key: "), flag);
  270.         else
  271.                 init(argv[1], flag);
  272.  
  273.         if (flag) decrypt();
  274.         else      crypt();
  275. }
  276.  
  277.  
  278. From philip@axis.UUCP Wed Feb 18 06:17:24 1987
  279. Path: beno!seismo!mcvax!inria!axis!philip
  280. From: philip@axis.UUCP
  281. Newsgroups: net.sources
  282. Subject: Crypt program - mods.
  283. Keywords: makekey replacement code
  284. Message-ID: <180@axis.UUCP>
  285. Date: 18 Feb 87 11:17:24 GMT
  286. Organization: Axis Digital, Paris
  287. Lines: 73
  288. Posted: Wed Feb 18 11:17:24 1987
  289.  
  290. In the crypt program I recently posted, there was a call made to a
  291. small program "/usr/lib/makekey". Apparently this does not exist on all
  292. *IX* machines.
  293.  
  294. I really dont see why - it is a 4 or 5 line program which reads a
  295. password on its stdin, and prints the encrypted result on its stdout,
  296. using exactly the same routines as passwd does.
  297.  
  298. Rather than re-write the program, I have made some mods to the crypt source
  299. to make the calls directly.
  300.  
  301. Since the library routine is called 'crypt', the routine within my program
  302. has to change its name, 'encrypt' is also used in the library, so the
  303. final name change is to 'encode'.
  304.  
  305. The following is a diff of the changes required (actually, the source becomes
  306. smaller and simpler .... )
  307.  
  308. ============================================================================
  309.  
  310. 19a20
  311. >       char            *crypt();
  312. 22d22
  313. <       int             pipe_fd[2];
  314. 25c25,27
  315. <       char            buf[13];
  316. ---
  317. >       char            buf[14];
  318. >       char            key[9];
  319. >       char            salt[3];
  320. 27,30c29,32
  321. <       strncpy(buf, password, 8);
  322. <       while (*password) *password++ = '\0';
  323. <       buf[8] = buf[0];
  324. <       buf[9] = buf[1];
  325. ---
  326. >       strncpy(key, password, 8);
  327. >       salt[0] = key[0];
  328. >       salt[1] = key[1];
  329. >       salt[2] = '\0';
  330. 32c34
  331. <       pipe(pipe_fd);
  332. ---
  333. >       strncpy(buf, crypt(key, salt), 13);
  334. 34,53d35
  335. <       if (fork() == 0)
  336. <       {
  337. <               close(0);
  338. <               close(1);
  339. <               dup(pipe_fd[0]);
  340. <               dup(pipe_fd[1]);
  341. <               execl("/usr/lib/makekey", "-", 0);
  342. <               execl("/lib/makekey", "-", 0);
  343. <               exit(1);
  344. <       }
  345. <       write(pipe_fd[1], buf, 10);
  346. <       wait((int *) NULL);
  347. <       if (read(pipe_fd[0], buf, 13) != 13)
  348. <       {
  349. <               fprintf(stderr, "crypt: cannot generate key\n");
  350. <               exit(1);
  351. <       }
  352. 119c101
  353. < crypt()
  354. ---
  355. > encode()
  356. 202c184
  357. <       else      crypt();
  358. ---
  359. >       else      encode();
  360.  
  361.  
  362. [nestey] 32)