You are on page 1of 3

// *********************************************************************

// Author: Alfredo Liu-Perez


// This program is released under Creative Commons license //(http://creativecom
mons.org/licenses/by/2.5/au/legalcode)
// Do an XOR operation: <input file> XOR <key> = <output file>
// input:
//
char *paczInputString - string to encode/decode
//
char *paczKeyString
- string used to XOR the input string
//
char *paczOutputString - output string
// *********************************************************************
#include <stdlib.h>
// malloc
#include <string.h>
// memset
#include <stdio.h>
// printf
// PROTOTYPES
int GetFileSize(char *pacFilename, int *piInputSize);
int ReadFile(char *pFilename, int *piInputSize, char *paczText );
int XOR_Encode_Decode( char *paczInputStr, int iInputSize,
char *paczKey, int iKeySize, char *paczOutput);
int main(void)
{
int iStat, i;
char ch;
int iInputSize;
char acInFilename[80];
char *paczIn;
int iKeySize;
char acKeyFilename[80];
char *paczKey;
int
char
char
FILE

iOutSize;
*paczOutput;
acOutFilename[80];
*pOutFILE;

// INITIALIZATIONS
iInputSize = 0;
iOutSize = 0;
iKeySize = 0;
// ******************************
// READ INPUT FILE
// ******************************
printf("Enter name of input file : ");
scanf ("%80s", acInFilename);
iStat = GetFileSize(acInFilename, &iInputSize);
paczIn = (char*) malloc(iInputSize+1);
memset(paczIn, '\0', iInputSize+1);
iStat = ReadFile(acInFilename, &iInputSize, paczIn );
// ******************************
// GET KEY
// ******************************
printf("Enter name of file with key : ");
scanf ("%80s", acKeyFilename);

iStat = GetFileSize(acKeyFilename, &iKeySize);


paczKey = (char*) malloc(iKeySize+1);
memset(paczKey, '\0', iKeySize+1);
iStat = ReadFile(acKeyFilename, &iKeySize, paczKey );
// ******************************
// ENCODE TEXT USING XOR
// ******************************
paczOutput = (char*) malloc(iInputSize+1);
memset(paczOutput, '\0', iInputSize+1);
iStat = XOR_Encode_Decode(paczIn, iInputSize, paczKey, iKeySize, paczOutput);
// ******************************
// WRITE TO OUTPUT FILE
// ******************************
printf("Enter name of output file : ");
scanf ("%80s", acOutFilename);
if ( (pOutFILE = fopen (acOutFilename, "wb")) == (FILE *) NULL )
{ fprintf(stderr, "Cannot open %s for writing.\n", acOutFilename);
exit(1);
}
for(i=0;i<iInputSize;i++)
{ fprintf(pOutFILE, "%c", paczOutput[i]);
}
fclose(pOutFILE);
return 0;
}
// *****************************************************************
int GetFileSize(char *pacFilename, int *piInputSize)
{
FILE *pInFILE;
int iInputSize = 0;
//
//
//
if
{

------------------GET FILE SIZE


------------------( (pInFILE = fopen (pacFilename, "rb")) == (FILE *) NULL )
fprintf(stderr, "Cannot open %s for reading.\n", pacFilename);
exit(1);

}
while (getc(pInFILE)!= EOF){iInputSize++;}
*piInputSize = iInputSize;
fclose(pInFILE);
return 1;
}
// *****************************************************************
int ReadFile(char *pFilename, int *piInputSize, char *paczText )
{
FILE *pInFILE;
int i, iInputSize;
int iStat = 1;
// -------------------

//
//
if
{

READ FILE TO ENCODE


------------------( (pInFILE = fopen (pFilename, "rb")) == (FILE *) NULL )
// print message if we can't open file for reading then exit
fprintf(stderr, "Cannot open %s for reading.\n", pFilename);
exit(1);

}
for(i=0;i<*piInputSize;i++)
{ paczText[i] = getc(pInFILE);
}
return iStat;
}
// *****************************************************************
// XOR FUNCTION
// IN:
//
char *paczInput - input text
//
int iInputSize - length of input text
//
char *paczKey
- text used to XOR with input (key)
//
int iKeySize - length of key
//
char *paczOutput - result of XOR function applied to input text and key
// *****************************************************************
int XOR_Encode_Decode(char *paczInput, int iInputSize,
char *paczKey, int iKeySize, char *paczOutput)
{
int liIndex=0;
while (liIndex<iInputSize)
{ *(paczOutput+liIndex) = (*(paczInput+liIndex) ^ *(paczKey+(liIndex % iKeySiz
e)));
liIndex++;
}
*(paczOutput+liIndex) = '\0';
return 1;
}

You might also like