[alt.hackers] DES on SUNS

abbadon@nuchat.UUCP (David Neal) (02/07/90)

From: abbadon@nuchat
Subject: DES on a Sun


I have two programs d1 and d2. They encrypt and decyrpt 
DES encoded text. The work together, but des(1) encrypted
data on a Sun cannot be decypted by the included program, d2.

What the fuck? The only thing I can figure is Sun uses 
a different ivec that what the man page suggests.


#include <stdio.h>
#include <des_crypt.h>

main()
{
	FILE *fp;

	char key[8], ivec[8];

	char data[8192];

	int des, i;

	unsigned datsize, mode;


	mode = DES_DECRYPT;

	key[0] = 'g'; key[1] = 'a'; key[2] = 'd';
	key[3] = 'f'; key[4] = 'l'; key[5] = 'y';
	key[6] = '\0'; key[7] = '\0';

	for ( i = 0; i < 8; i++ ) ivec[i]  = 0;

	des_setparity(key);

	if ( (fp = fopen("foo.o", "r")) == (FILE *) NULL )
		{
			printf("can't open plain file.\n");
			exit(0);
		}

	if ( (datsize = fread(data, sizeof(char), 8190, fp)) == 0)
		{
			printf("couldn't read file.\n");
			fclose(fp);
			exit(0);
		}	

	printf("%d bytes read.\n", datsize);

	if ( datsize % 8 != 0 ) 
		{	
			int c;
			int pad = 8 - (datsize % 8);

			printf("Adding %d pad bytes.\n", pad);
			for ( c = 0; c < pad; c++ )
				data[datsize+c] = '0';

			data[datsize+pad] = pad+'0';
			datsize += pad;

			printf("Datsize now %d or %d %% 8.\n", datsize, datsize % 8);
		}

	des = cbc_crypt(key, data, datsize, mode, ivec);

	printf("des = %d\n", des);
	printf("padbytes = %d\n", data[datsize+1]);
	switch (des) {

		case DESERR_NOHWDEVICE:	printf("No hardwire des device.\n");

		case DESERR_NONE: 	printf("All ok\n");
					wdata(data, datsize);
					break;

		case DESERR_HWERROR:	printf("Des hardware error.\n");
					break;

		case DESERR_BADPARAM:	printf("Bad parameter passed.\n");
					break;
		}

	fclose(fp);
	exit(0);
}

wdata(data, datlen)
char *data;
unsigned datlen;
{
	FILE *ofp;

	int ret;

	if ( (ofp = fopen("foo.d", "w")) == (FILE *) NULL )
		{
			perror("des: open: ");
			exit(0);
		}

	if ( (ret = fwrite(data, sizeof(char), datlen, ofp)) == 0)
		{
			perror("des: write: ");
			exit(0);
		}

	fclose(ofp);

}

abbadon@nuchat.UUCP