[comp.unix.wizards] Help with AT&T encrypti

ag@crash.cts.com (Keith Gabryelski) (12/10/87)

I recently sent a message to SCO asking if I had found a bug.
I have not received a reply and am forwarding the message to
the net for assistance.

text follows.
------------------------------------------------------------------
To whom it may concern:

I am having some trouble using encrypt(S) on SCO Xenix 2.2.1.  If you
would, could you test the function as speced in your manual.

The code below is a test function that does not seem to work on the SCO
Xenix 2.2.1 I have running on a Kaypro 286i.  It does, however work on
a BSD 4.2 machine.  The manual pages for both systems are identicle,
so it would seem that if this code works on one system, it should
work on the other.

PS, this function also does not work on a 3b1 running Sysv rev 2

------------------------------------------------------------------------------
#define PASSWORD "password"
#define DATA "testdata"


main()
{
    char key[64], block[64];
    char buf[9];

    makeblock(block, DATA);
    puts("Here is the data");
    dumpblock(block);

#ifdef ENCRYPT
    makeblock(key, PASSWORD);
    puts("Here is the key");
    dumpblock(key);

    setkey(key);

    encrypt(block, 0);
    puts("Here is the encrypted data");
    dumpblock(block);

    encrypt(block, 1);
    puts("Here is the decrypted data");
    dumpblock(block);
#endif ENCRYPT

    unmakeblock(buf, block);
    buf[8] = '\0';

    printf("The resulting string is: `%s'\n", buf);

    return strcmp(buf, DATA);
}


dumpblock(block)
char *block;
{
    register i, j;
    for ( i=0 ; i<8 ; ++i )
    {
	for ( j=0 ; j<8 ; ++j )
	    putchar('0' + *block++);
	putchar(' ');
    }
    putchar('\n');
}


/* converts a 8-byte string into a 64-byte */
/* array of 0-or-1 bytes suitable for crypt(3C) */
makeblock(array, string)
char *array, *string;
{
    register unsigned char c;
    register int i, j;

    for ( i=0 ; i<8 ; ++i )
    {
	c = *string++;
	for ( j=0 ; j<8 ; ++j )
	{
	    *array++ = c&1;
	    c >>= 1;
	}
    }
}


/* converts a 64-byte array of 0-or-1 bytes as */
/* returned by crypt(3C) into a 8-byte string */
unmakeblock(string, array)
char *string, *array;
{
    register int j, i;
    register unsigned char c;

    for ( i=0 ; i<8 ; ++i )
    {
	c = 0;
	for ( j=0 ; j<8 ; ++j )
	{
	    c >>= 1;
	    if (*array++)
		c |= 0x80;
	}
	*string++ = c;
    }
}
-- 
Keith Gabryelski       UUCP: {cbosgd, hplabs!hp-sdd, sdcsvax, nosc}!crash!ag
                       ARPA: crash!ag@nosc.mil        INET: ag@crash.CTS.COM