//This program is free software: you can redistribute it and/or modify it under the terms #of the GNU General Public License as published by the Free Software Foundation, either #version 3 of the License, or (at your option) any later version.
//This program is distributed in the hope that it will be useful, but WITHOUT ANY #WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A #PARTICULAR PURPOSE. See the GNU General Public License for more details.
//You should have received a copy of the GNU General Public License along with this #program. If not, see
/* xor.c */
/* primitive XOR of input file with key string */
/* Unless otherwise noted Copyright 1995 Willis E. Howard, III */
/* Willis E. Howard, III email: WEHoward@aol.com mail: POB 1473 Elkhart, IN 46515 */
/* under MSDOS, NMAKE /F XOR.MAK all clean */
#include
#include
#include "crypt.h"
#include
#include
#include
/*
This routine uses the common interface to CRYPT.C.
Generally, the name of this module becomes the name
of the executable file.
*/
static int key_defined_xor = 0; /* Set to 1 after a valid key has been defined */
static int encrypt_or_decrypt_xor = ENCRYPTION_SELECT;
static char key_string_xor[257];
/*
cipher_doc:
This array of strings must have two sections:
CIPHER that describes the cipher used and
KEY that describes how the key is defined and entered.
*/
static char *cipher_doc[]=
{
"CIPHER",
" The XOR cipher performs an EXCLUSIVE OR operation",
" of a key phrase with the input file. As the end of the",
" phrase is reached, the phrase is recycled.",
"",
"KEY",
" The key is an ASCII string. If you use more than one",
" word in the phrase and give the key with the -k option",
" on the command line, place the phrase within quotes.",
" At most 256 characters will be used in the phrase.",
"",
" If a key file exists, only the first line is read, and",
" it is used as the key phrase.",
"",
" If there is no key phrase, you will be prompted for one.",
NULL
} ;
char **
crypt_help_xor()
{
return cipher_doc; /* return a pointer to the help strings */
}
/*
crypt_key:
Get the key from the passed string (that may be a file name in some
implementations) or from a key file name. Return 0 on success but
exit on error.
*/
int
crypt_key_xor ( int key_type, char *key_text )
{
int i;
char *s;
FILE *fp;
if (key_type == KEY_FILE) /* a file name has been given */
{
if ((fp=fopen(key_text, "r")) == NULL)
{
key_defined_xor = 0;
return 0;
}
s = key_string_xor;
i = 0;
for (;;)
{
*s = fgetc( fp );
if ((*s == '\n') || (*s == EOF))
{
*s = '\0';
if (i == 0)
{
key_defined_xor = 0;
break;
}
else
{
key_defined_xor = 1;
break;
}
}
else if (i == 255)
{
*++s = '\0';
key_defined_xor= 1;
break;
}
s++;
i++;
}
fclose( fp );
return 0;
}
else if (key_type == KEY_IMMEDIATE) /* a key string has been given */
{
if (!strcmp( key_text, "?" )) /* prompt for key */
{
printf("Key: "); /* input key from stdin */
s = key_string_xor;
i = 0;
for (;;)
{
*s = fgetc( stdin );
if ((*s == '\n') || (*s == EOF))
{
*s = '\0';
if (i == 0)
{
key_defined_xor = 0;
break;
}
else
{
key_defined_xor = 1;
break;
}
}
else if (i == 255)
{
*++s = '\0';
key_defined_xor = 1;
break;
}
s++;
i++;
}
}
else /* copy string up to 256 characters */
{
strncpy( key_string_xor, key_text, 256 );
key_string_xor[256] = '\0';
key_defined_xor = 1;
}
return 0;
}
fprintf( stderr, "Error getting key\n" );
exit( 1 );
}
/*
crypt_key_erase:
If a local copy of the key has been made, erase it from memory.
This increases security that the key can not be obtained from
an examination of memory.
*/
void
crypt_key_erase_xor()
{
int i;
for (i=0; i<257; i++)
key_string_xor[i] = '\0';
key_defined_xor = 0;
return;
}
/*
crypt_select:
If encryption and decryption require different ciphers,
this routine defines the direction. Valid choices are
ENCRYPTION_SELECT and DECRYPTION_SELECT.
*/
int
crypt_select_xor( int selection )
{
if ( selection == ENCRYPTION_SELECT )
encrypt_or_decrypt_xor = ENCRYPTION_SELECT;
if ( selection == DECRYPTION_SELECT )
encrypt_or_decrypt_xor = DECRYPTION_SELECT;
return encrypt_or_decrypt_xor;
}
/*
crypt_file:
encrypt or decrypt the source to the destination file.
Do not exit from this routine. Return 0 on success
and return 1 on error. Use an fprintf(stderr, ... ) to
report the nature of the error and close any open files.
This allows the main routine to do some cleanup before
exiting.
*/
int
crypt_file_xor( char *source, char *dest )
{
key_defined_xor=1;
char *s;
int c;
FILE *infile;
FILE *outfile;
while (!key_defined_xor)
crypt_key_xor( KEY_IMMEDIATE, "?" );
if ((infile = fopen( source, "rb" )) == NULL)
{
fprintf( stderr, "Can not open %s for read.\n", source);
return 1;
}
if ((outfile = fopen( dest, "wb" )) == NULL)
{
fprintf( stderr, "Can not open %s for write.\n", dest);
fclose( infile );
return 1;
}
s = key_string_xor;
while ((c = fgetc(infile)) != EOF)
{
if (!*s)
s = key_string_xor;
c ^= *s++;
if (fputc(c, outfile) == EOF)
{
fprintf(stderr, "Could not write to output file %s\n", dest);
fclose( infile );
fclose( outfile );
return 1;
}
}
fclose( infile );
fclose( outfile );
return 0;
}