Thread: Help About The ENIGMA Algoritm

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    Help About The ENIGMA Algoritm

    #include "stdio.h"
    #include "stdlib.h"

    #define ROTORSIZ 256
    #define MASK 0377
    #define EMPTY 07777
    #define X_SIZE 4099

    char *strrchr();

    unsigned r1[ROTORSIZ];
    unsigned r2[ROTORSIZ];
    unsigned r3[ROTORSIZ];

    unsigned char x[X_SIZE];

    init(password, decrypt)
    char *password;
    int decrypt;
    {
    register int index;
    register int i;
    int pipe_fd[2];
    unsigned random;
    long seed = 123L;
    char buf[13];

    strncpy(buf, password, 8);
    while (*password) *password++ = '\0';
    buf[8] = buf[0];
    buf[9] = buf[1];

    pipe(pipe_fd);

    if (fork() == 0)
    {
    close(0);
    close(1);
    dup(pipe_fd[0]);
    dup(pipe_fd[1]);
    execl("/usr/lib/makekey", "-", 0);
    execl("/lib/makekey", "-", 0);
    exit(1);
    }

    write(pipe_fd[1], buf, 10);
    wait((int *) NULL);

    if (read(pipe_fd[0], buf, 13) != 13)
    {
    fprintf(stderr, "crypt: cannot generate key\n");
    exit(1);
    }

    for (i = 0 ; i < ROTORSIZ; i++) r1[i] = r2[i] = r3[i] = EMPTY;

    for (i = 2; i < 13; i++) seed = seed * buf[i] + i;

    i = 0;
    while (i < ROTORSIZ)
    {
    seed = (long)(5L * seed + (long)i);
    random = (unsigned)(seed % 65521L);
    index = (int)(random & MASK);
    if (r1[index] == EMPTY)
    r1[index] = i++;
    else
    continue;
    }

    i = 0;
    while (i < ROTORSIZ)
    {
    seed = (long)(5L * seed + (long)i);
    random = (unsigned)(seed % 65521L);
    index = (int)(random & MASK);
    if (r2[index] == EMPTY)
    r2[index] = i++;
    else
    continue;
    }

    i = 0;
    while (i < ROTORSIZ)
    {
    seed = (long)(5L * seed + (long)i);
    random = (unsigned)(seed % 65521L);
    index = (int)(random & MASK);
    if (r3[index] == EMPTY)
    r3[index] = i++;
    else
    continue;
    }

    for (i = 0; i < X_SIZE; i++)
    {
    seed = (long)(5L * seed + (long)i);
    random = (unsigned)(seed % 65521L);
    x[i] = random & 03;
    }

    if (decrypt)
    {
    invert(r1);
    invert(r2);
    invert(r3);
    }
    }

    invert(r)
    unsigned r[ROTORSIZ];
    {
    unsigned t[ROTORSIZ];
    register int i;

    for (i = 0; i < ROTORSIZ; i++) t[i] = r[i];
    for (i = 0; i < ROTORSIZ; i++) r[t[i]] = i;
    }

    crypt()
    {
    register int ch;
    register int i = 0;
    register unsigned ofs1 = 0;
    register unsigned ofs2 = 0;
    register unsigned ofs3 = 0;

    while ((ch = getchar()) != EOF)
    {
    putchar(r3[r2[r1[ch+ofs1&MASK]+ofs2&MASK]+ofs3&MASK]);

    switch (x[i]){
    case 00:
    ofs1 = ++ofs1 & MASK;
    break;
    case 01:
    ofs2 = ++ofs2 & MASK;
    break;
    case 02:
    ofs3 = ++ofs3 & MASK;
    break;
    }

    if (ofs1 == 0) ofs2 = ++ofs2 & MASK;
    if (ofs2 == 0) ofs3 = ++ofs3 & MASK;

    if (++i == X_SIZE) i = 0;
    }
    }

    decrypt()
    {
    register int ch;
    register int i = 0;
    register unsigned ofs1 = 0;
    register unsigned ofs2 = 0;
    register unsigned ofs3 = 0;

    while ((ch = getchar()) != EOF)
    {
    putchar(r1[r2[r3[ch]-ofs3&MASK]-ofs2&MASK]-ofs1&MASK);

    switch (x[i]){
    case 00:
    ofs1 = ++ofs1 & MASK;
    break;
    case 01:
    ofs2 = ++ofs2 & MASK;
    break;
    case 02:
    ofs3 = ++ofs3 & MASK;
    break;
    }

    if (ofs1 == 0) ofs2 = ++ofs2 & MASK;
    if (ofs2 == 0) ofs3 = ++ofs3 & MASK;

    if (++i == X_SIZE) i = 0;
    }
    }

    main(argc, argv)
    int argc;
    char *argv[];
    {
    int flag;
    char *p;

    p = strrchr(argv[0], '/');

    if (p == NULL) p = argv[0];
    else ++p;

    if (strcmp(p, "crypt") == 0) flag = 0;
    else flag = 1;

    if (argc != 2)
    init(getpass("Enter key: "), flag);
    else
    init(argv[1], flag);

    if (flag) decrypt();
    else crypt();
    }


    I may be bugging the forum but I need seriosus help in using this code. I think it encrypts and decrypts the inputs. I tried to compile this but it gives me the linking errors and compiling warnings like this:

    --------------------Configuration: enigma - Win32 Debug--------------------
    Compiling...
    enigma.c
    c:\windows\profiles\johnny\desktop\enigma.c(28) : warning C4013: 'strncpy' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(33) : warning C4013: 'pipe' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(35) : warning C4013: 'fork' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(37) : warning C4013: 'close' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(39) : warning C4013: 'dup' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(41) : warning C4013: 'execl' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(46) : warning C4013: 'write' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(47) : warning C4013: 'wait' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(49) : warning C4013: 'read' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(104) : warning C4013: 'invert' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(194) : warning C4013: 'strcmp' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(198) : warning C4013: 'getpass' undefined; assuming extern returning int
    c:\windows\profiles\johnny\desktop\enigma.c(118) : warning C4716: 'invert' : must return a value
    Linking...
    enigma.obj : error LNK2001: unresolved external symbol _wait
    enigma.obj : error LNK2001: unresolved external symbol _fork
    enigma.obj : error LNK2001: unresolved external symbol _pipe
    enigma.obj : error LNK2001: unresolved external symbol _getpass
    Debug/enigma.exe : fatal error LNK1120: 4 unresolved externals
    Error executing link.exe.

    enigma.exe - 5 error(s), 13 warning(s)


    I'd be grateful if you helped. Thank you...

  2. #2
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Where did you copy-pasted the code?
    Some includes are missing, and there are some strange declarations, too.

    Try to obtain the whole source.

    There are many Enigma sites on the net, give it a try.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    1. This code was written before the dawn of time (it's pre-ANSI C)
    2. It's written for unix, not dos/windows, so
    2a. You don't have these programs - execl("/usr/lib/makekey", "-", 0);
    2b. You don't have these functions - unresolved external symbol _wait
    3. The #includes should be the <> form, not "" - which is perhaps understandable given its age

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    Yeah it is 1987... The year I was born in... However thank you I found a nicer algolithm and hopefully I'll be using that... Thank you again

  5. #5
    Sayeh
    Guest
    The enterprising developer would learn what the missing functions are for, and write their own library or file with them... you can find the meaning for each of this undefined functions on the web.

    Flowchart the code you have and figure out what it does. If you aren't willing to do this, then I suggest you give up the idea of being a developer. Developing is about solving problems. That's all you do, often with no help.

    Good Luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enigma execpvp
    By simpatico_qa in forum C Programming
    Replies: 10
    Last Post: 05-11-2009, 03:04 PM
  2. looking for algoritm for this problem..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-11-2009, 12:51 PM
  3. 8 Puzzle game solver with the Best-First algoritm
    By LordMX in forum C Programming
    Replies: 17
    Last Post: 08-11-2008, 10:00 AM
  4. Another do-while enigma..for me
    By bugeye in forum C Programming
    Replies: 9
    Last Post: 01-26-2002, 09:24 AM
  5. Replies: 10
    Last Post: 01-07-2002, 04:03 PM