/* xcrypt.&
*
* A simple involutional encryption program.
*
* For theory and expanation,
* Cf. Computer Language Feb 1987 p. 69 (Alan Filipski)
*
*/
#include <stdio.h>
#include <stdfun.h>
#include <string.h>
#define NDELAY 5
#define BYTE( x ) ( (x) & 0xff )
long seed[3];
int p[256], s[256], q[256];
static char _sccsid[] = { " %M% %I% %H% " };
int main( argc, argv )
int argc;
char *argv[];
{
void shuffle();
int rand8();
char *key;
int i, c, r0, r1, r2, keylen;
if( argc != 2 ) {
fprintf( stderr, "Usage: %s key < infile > outfile.\n or\n ... | %s key | ...\n", argv[0], argv[0] );
exit( 1 );
}
if( ( keylen = strlen( key = *++argv ) ) < 4 ) {
fprintf( stderr, "Key must be at least 4 characters.\n" );
exit( 1 );
}
for( i = 0; i < 4; i++ ) {
seed[0] = seed[0] << 8 | key[i];
seed[1] = seed[1] << 8 | key[(keylen - 1) - i];
seed[2] = seed[2] << 8 | key[(keylen / 2) + i - 2];
}
while( *key )
*key++ = 0;
for( i = 0; i < 256; i++ )
p[i] = i;
shuffle( p, 256, 0 );
for( i = 0; i < NDELAY; i++ ) {
shuffle( p, 256, 0 );
shuffle( p, 256, 1 );
shuffle( p, 256, 2 );
}
for( i = 0; i < 256; i += 2 )
s[ s[ p[i] ] = p[i + 1] ] = p[i];
shuffle( p, 256, 1 );
for( i = 0; i < 256; i++ )
q[ p[i] ] = i;
while( ( c = getchar() ) != EOF ) {
r0 = rand8( 0 );
r1 = rand8( 1 );
r2 = rand8( 2 );
c = (c ^ r2) + r0;
c = q[BYTE( s[BYTE( p[BYTE( c )] + r1)] - r1)];
c = (c - r0) ^ r2;
putchar( (char)c );
}
exit( 0 );
return( 0 );
} /* End main() */
/* Return a pseudo random byte from one of several streams. The ith stream
* is seeded by the global long seed[i].
*/
int rand8( i )
int i;
{
seed[i] = seed[i] * 1103515245L + 12345;
return( BYTE( seed[i] >> 19 ) );
}
/* Randomly permute the items in the array[size] using the ith random stream
* from rand8()
*/
void shuffle( a, size, rstream )
int size, *a, rstream;
{
int rand8();
register int i, j, temp;
for( i = size - 1; i >= 0; i-- ) {
j = rand8( rstream ) % (i + 1);
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
Return to Home Page
Return to Metayoga Page
Return to C Language Page
The URL for this document is:
http://graham.main.nc.us/~bhammel/graham/CPROGS/xcrypt.html
Created: 1997
Last Updated: May 28, 2000
Email me, Bill Hammel at
bhammel@graham.main.nc.us
READ WARNING BEFORE SENDING E-MAIL