2 * "enigma.c" is in file cbw.tar from
3 * anonymous FTP host watmsg.waterloo.edu: pub/crypt/cbw.tar.Z
5 * A one-rotor machine designed along the lines of Enigma
6 * but considerably trivialized.
8 * A public-domain replacement for the UNIX "crypt" command.
10 * Upgraded to function properly on 64-bit machines.
12 * $FreeBSD: src/usr.bin/enigma/enigma.c,v 1.2.6.3 2001/08/01 23:51:34 obrien Exp $
13 * $DragonFly: src/usr.bin/enigma/enigma.c,v 1.5 2004/07/31 11:18:43 eirikn Exp $
16 #include <sys/types.h>
24 #define MINUSKVAR "CrYpTkEy"
46 strncpy(salt, pw, sizeof(salt));
47 memcpy(buf, crypt(pw, salt), sizeof(buf));
50 seed = seed*buf[i] + i;
51 for(i=0;i<ROTORSZ;i++) {
55 for(i=0;i<ROTORSZ;i++) {
56 seed = 5*seed + buf[i%13];
57 if( sizeof(long) > 4 ) {
58 /* Force seed to stay in 32-bit signed math */
59 if( seed & 0x80000000 )
60 seed = seed | (-1L & ~0xFFFFFFFFL);
66 ic = (rnd&MASK)%(k+1);
71 if(t3[k]!=0) continue;
73 while(t3[ic]!=0) ic = (ic+1) % k;
77 for(i=0;i<ROTORSZ;i++)
82 main(int argc, char **argv)
84 register int i, n1, n2, nr1, nr2;
85 int secureflg = 0, kflag = 0;
88 if (argc > 1 && argv[1][0] == '-') {
89 if (argv[1][1] == 's') {
93 } else if (argv[1][1] == 'k') {
100 if ((cp = getenv(MINUSKVAR)) == NULL) {
101 fprintf(stderr, "%s not set\n", MINUSKVAR);
105 } else if (argc != 2) {
106 setup(getpass("Enter key:"));
114 while((i=getchar()) != -1) {
117 nr2 = deck[nr1]&MASK;
121 i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
127 if(n2==ROTORSZ) n2 = 0;
140 shuffle(char *deckary)
144 static long seed = 123;
146 for(i=0;i<ROTORSZ;i++) {
147 seed = 5*seed + buf[i%13];
150 ic = (rnd&MASK)%(k+1);
152 deckary[k] = deckary[ic];