Let's just make it abundantly clear, ok? Here's a one-time-pad encryption program:
Code:
/* otp.c -- one time pad
*
* Usage:
*
* To encrypt a file 'data.txt' using 'key.txt', writing output to 'out.txt':
* opt -encrypt data.txt out.txt key.txt
*
* To decrypt a file 'cipher.txt' using 'key.txt', writing output to 'out.txt':
* opt -decrypt cipher.txt out.txt key.txt
*
* To encrypt a file 'data.txt' using a randomly generated key, writing output
* to 'out.txt' and the key to 'key.txt':
* opt -encrypt data.txt out.txt
*
*
* Example, which first encrypts, then decrypts:
* opt -encrypt data.txt out.txt
* opt -decrypt out.txt decrypted.txt key.txt
*/
#include <stdio.h>
#include <stdlib.h>
void puthex( int c, FILE *fp )
{
static const char hexdigit[] = "0123456789ABCDEF";
fputc( hexdigit[ c >> 4 ], fp );
fputc( hexdigit[ c & 15 ], fp );
}
int gethex( FILE *fp )
{
int c = 0;
int h;
h = fgetc( fp );
if( h == EOF )
return EOF;
if( h >= '0' && h <= '9' )
c += 16 * ( h - '0' );
else if( h >= 'A' && h <= 'F' )
c += 16 * ( h - 'A' + 10 );
h = fgetc( fp );
if( h == EOF )
return EOF;
if( h >= '0' && h <= '9' )
c += h - '0';
else if( h >= 'A' && h <= 'F' )
c += h - 'A' + 10;
return c;
}
int main( int argc, char **argv )
{
int i;
int doDecrypt = 0;
int doCreateKey = 0;
const char *inFilename = NULL;
const char *outFilename = NULL;
const char *keyFilename = NULL;
FILE *inFile;
FILE *outFile;
FILE *keyFile;
if( argc < 4 )
{
fprintf( stderr, "Usage error: See source file for usage.\n" );
exit( 1 );
}
if( strcmp( argv[ 1 ], "-decrypt" ) == 0 )
doDecrypt = 1;
if( doDecrypt && argc < 5 )
{
fprintf( stderr, "Usage error: See source file for usage.\n" );
exit( 1 );
}
inFilename = argv[ 2 ];
outFilename = argv[ 3 ];
if( !doDecrypt && argc < 5 )
{
keyFilename = "key.txt";
doCreateKey = 1;
}
else
keyFilename = argv[ 4 ];
inFile = fopen( inFilename, "rb" );
outFile = fopen( outFilename, "wb" );
if( !doCreateKey )
keyFile = fopen( keyFilename, "rb" );
else
keyFile = fopen( keyFilename, "wb" );
if( !doDecrypt )
{
int c1, c2;
for( ; ; )
{
c1 = fgetc( inFile );
if( c1 == EOF )
break;
if( doCreateKey )
{
c2 = rand() & 0xFF;
puthex( c2, keyFile );
}
else
c2 = gethex( keyFile );
if( c2 == EOF )
{
fprintf( stderr, "Key data is not long enough.\n" );
exit( 1 );
}
c1 ^= c2;
puthex( c1, outFile );
}
}
else
{
int c1, c2;
for( ; ; )
{
c1 = gethex( inFile );
if( c1 == EOF )
break;
c2 = gethex( keyFile );
if( c2 == EOF )
{
fprintf( stderr, "Key data is not long enough.\n" );
exit( 1 );
}
c1 ^= c2;
fputc( c1, outFile );
}
}
fclose( inFile );
fclose( outFile );
fclose( keyFile );
return 0;
}
Please write a paragraph of text. Place this into "plaintext.txt". Then run:
Code:
otp -encrypt plaintext.txt ciphertext.txt
This will auto-generate a key into "key.txt". Do NOT tell me the key. Verify that the key works by doing:
Code:
otp -decrypt ciphertext.txt out.txt key.txt
The contents of "out.txt" should match "plaintext.txt".
Then, post the contents of "ciphertext.txt" here, and hopefully I can demonstrate what I'm talking about.