Сообщение от :
#include <windows.h>
#include <stdio.h>
#include <io.h>
FILE* infile = NULL;
FILE* outfile = NULL;
BYTE* pass = NULL;
int cur_char = 0;
int pass_len = 0;
long ifsize = 0;
void main( int argc, char* argv[] )
{
if( 5 != argc )
{
printf( "Usage: encdec action infile outfile password\n" );
printf( " action = e - encode or d - decode\n" );
printf( " infile = name of input file\n" );
printf( " outfile = name of output file\n" );
printf( " password = password for operation\n" );
return;
}
infile = fopen( argv[2], "rb" );
outfile = fopen( argv[3], "w+b" );
pass = (BYTE*)argv[4];
pass_len = strlen( argv[4] );
BYTE indata;
BYTE outdata;
ifsize = _filelength( _fileno( infile ) );
for( long i = 0; i < ifsize; i++ )
{
fread( &indata, 1, 1, infile );
outdata = pass[cur_char] ^ indata;
fwrite( &outdata, 1, 1, outfile );
cur_char++;
if( cur_char == pass_len )
cur_char = 0;
}
BYTE data;
if( 0 == strcmp( "e", argv[1] ) )
{
for( int i = 0; i < pass_len; i++ )
{
data = pass[i] ^ pass[cur_char];
fwrite( &data, 1, 1, outfile );
cur_char++;
if( cur_char == pass_len )
cur_char = 0;
}
fclose( infile );
fclose( outfile );
printf( "Encoding complete.\n" );
}
else
{
fclose( infile );
fflush( outfile );
fseek( outfile, -pass_len, SEEK_END );
for( int i = 0; i < pass_len; i++ )
{
fread( &data, 1, 1, outfile );
if( data != pass[i] )
{
fclose( outfile );
remove( argv[3] );
printf( "Wrong password.\n" );
return;
}
}
_chsize( _fileno(outfile), ifsize - pass_len );
fclose( outfile );
printf( "Decoding complete.\n" );
}
}