//Download all files @ http://nexos.bplaced.net/otp_xor.rar
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <iterator>
//random
#include "mersenne.cpp"
#include <time.h>
using namespace std;
//mersenne random number generator crap
template <class RG1, class RG2>
class TRandomCombined : private RG1, private RG2 {
public:
TRandomCombined(int32 seed = 19) : RG1(seed), RG2(seed+1) {};
void RandomInit(int32 seed) { // re-seed
RG1::RandomInit(seed);
RG2::RandomInit(seed+1);}
double Random() {
long double r = RG1::Random() + RG2::Random();
if (r >= 1.) r -= 1.;
return r;}
long IRandom(long min, long max){ // output random integer
// get integer random number in desired interval
int iinterval = max - min + 1;
if (iinterval <= 0) return -1; // error
int i = int(iinterval * Random()); // truncate
if (i >= iinterval) i = iinterval-1;
return min + i;}};
//stream/string functions
string IntToString(int iValue)
{
stringstream ssStream;
ssStream << iValue;
return ssStream.str();
}
string GetData(string strFileName)
{
cout<<endl<<"... reading files ..."<<endl;
ifstream FStream;
FStream.open(strFileName.c_str(), ios::in | ios::binary );
FStream.unsetf(ios_base::skipws);
istream_iterator<char> begin(FStream), end;
string strFileData(begin, end);
FStream.close();
return strFileData;
}
//encrypt + decrypt
void encrypt(string strPlainFile, string strKeyFile, string strOutputFile)
{
string strPlainFileData = GetData(strPlainFile);
ofstream OutputFStream;
ofstream KeyFStream;
OutputFStream.open(strOutputFile.c_str(), ios::out | ios::binary);
KeyFStream.open(strKeyFile.c_str(), ios::out | ios::binary);
int32 seed = (int32)time(0);
CRandomMersenne rg(seed);
cout<<endl<<"... doing encryption ..."<<endl<<endl;
for (int i = 0; i < (int) strPlainFileData.size(); i++)
{
char Key = rg.IRandom(0,9);
char Input = strPlainFileData[i];
char Crypted = Input ^ Key;
OutputFStream << Crypted;
KeyFStream << Key;
}
OutputFStream.close();
KeyFStream.close();
cout<<"encryption finished!"<<endl;
}
void decrypt(string strPlainFile, string strKeyFile, string strCryptedFile)
{
string strCryptedFileData = GetData(strCryptedFile);
string strKeyFileData = GetData(strKeyFile);
ofstream PlainFileOFStream;
PlainFileOFStream.open(strPlainFile.c_str(), ios::out | ios::binary);
cout<<endl<<"... doing decryption ..."<<endl<<endl;
for (int i = 0; i < (int) strCryptedFileData.size(); i++)
{
char Key = strKeyFileData[i];
char Crypted = strCryptedFileData[i];
char Plain = Crypted ^ Key;
PlainFileOFStream << Plain;
}
PlainFileOFStream.close();
cout<<"decryption finished!"<<endl;
}
int main(int argc, char* argv[])
{
if(argc != 5 )
{
cout<<"onetimepad xor file encrypter with mersenne twister random number algorythm by nexos"<<endl;
cout<<endl<< "Usage: " << argv[0] << " <0/1> <Plaintext File> <Key File> <Crypted File>" <<endl<<endl;
cout<< "<0>: decrypts <Crypted File> with <Key File> to <Plaintext File>" <<endl<<endl;
cout<< "<1>: encrypts <Plaintext File> with <Key File> (Key is generated) to <Crypted File>" <<endl<<endl;
cout<< "existing files are overwritten" << endl;
return 0;
}
if ( atoi( argv[1]) == 0 )
decrypt(argv[2],argv[3],argv[4]);
if ( atoi( argv[1]) == 1 )
encrypt(argv[2],argv[3],argv[4]);
return 0;
}