[ Log In | Register ]       
Resource Database Index -> Source Code -> One Time Pad - Xor Crypter [rnd: mersenne twister]
Description // Info



Source Code

  1. //Download all files @ http://nexos.bplaced.net/otp_xor.rar
  2. #include <sstream>
  3. #include <string>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <iterator>
  7.  
  8. //random
  9. #include "mersenne.cpp"
  10. #include <time.h>
  11.  
  12. using namespace std;
  13.  
  14. //mersenne random number generator crap
  15.   template <class RG1, class RG2>
  16.   class TRandomCombined : private RG1, private RG2 {
  17.     public:
  18.     TRandomCombined(int32 seed = 19) : RG1(seed), RG2(seed+1) {};
  19.  
  20.      void RandomInit(int32 seed) {        // re-seed
  21.       RG1::RandomInit(seed);
  22.       RG2::RandomInit(seed+1);}
  23.  
  24.     double Random() {
  25.       long double r = RG1::Random() + RG2::Random();
  26.       if (r >= 1.) r -= 1.;
  27.      return r;}
  28.  
  29.     long IRandom(long min, long max){       // output random integer
  30.       // get integer random number in desired interval
  31.       int iinterval = max - min + 1;
  32.       if (iinterval <= 0) return -1; // error
  33.       int i = int(iinterval * Random()); // truncate
  34.       if (i >= iinterval) i = iinterval-1;
  35.      return min + i;}};
  36.  
  37. //stream/string functions
  38.   string IntToString(int iValue)
  39.   {
  40.       stringstream ssStream;
  41.       ssStream << iValue;
  42.     return ssStream.str();
  43.   }
  44.  
  45.   string GetData(string strFileName)
  46.   {
  47.      cout<<endl<<"... reading files ..."<<endl;
  48.        ifstream FStream;
  49.         FStream.open(strFileName.c_str(), ios::in | ios::binary );
  50.  
  51.         FStream.unsetf(ios_base::skipws);
  52.         istream_iterator<char> begin(FStream), end;
  53.         string strFileData(begin, end);
  54.  
  55.        FStream.close();
  56.  
  57.       return strFileData;
  58.   }
  59.  
  60. //encrypt + decrypt
  61. void encrypt(string strPlainFile, string strKeyFile, string strOutputFile)
  62. {
  63.         string strPlainFileData    = GetData(strPlainFile);
  64.  
  65.         ofstream OutputFStream;
  66.         ofstream KeyFStream;
  67.  
  68.         OutputFStream.open(strOutputFile.c_str(), ios::out | ios::binary);
  69.         KeyFStream.open(strKeyFile.c_str(), ios::out | ios::binary);
  70.  
  71.    int32 seed = (int32)time(0);
  72.    CRandomMersenne rg(seed);
  73.  
  74.       cout<<endl<<"... doing encryption ..."<<endl<<endl;
  75.  
  76.         for (int i = 0; i < (int) strPlainFileData.size(); i++)
  77.         {
  78.                 char Key        = rg.IRandom(0,9);
  79.                 char Input      = strPlainFileData[i];
  80.                 char Crypted    = Input ^ Key;
  81.  
  82.                 OutputFStream << Crypted;
  83.                 KeyFStream << Key;
  84.         }
  85.  
  86.         OutputFStream.close();
  87.         KeyFStream.close();
  88.  
  89.       cout<<"encryption finished!"<<endl;
  90. }
  91.  
  92. void decrypt(string strPlainFile, string strKeyFile, string strCryptedFile)
  93. {
  94.         string strCryptedFileData  = GetData(strCryptedFile);
  95.         string strKeyFileData      = GetData(strKeyFile);
  96.  
  97.         ofstream PlainFileOFStream;
  98.         PlainFileOFStream.open(strPlainFile.c_str(), ios::out | ios::binary);
  99.  
  100.      cout<<endl<<"... doing decryption ..."<<endl<<endl;
  101.  
  102.         for (int i = 0; i < (int) strCryptedFileData.size(); i++)
  103.         {
  104.                 char Key        = strKeyFileData[i];
  105.                 char Crypted    = strCryptedFileData[i];
  106.                 char Plain     = Crypted ^ Key;
  107.  
  108.                 PlainFileOFStream << Plain;
  109.         }
  110.  
  111.         PlainFileOFStream.close();
  112.      cout<<"decryption finished!"<<endl;
  113. }
  114.  
  115. int main(int argc, char* argv[])
  116. {
  117.  
  118.     if(argc != 5 )
  119.     {
  120.         cout<<"onetimepad xor file encrypter with mersenne twister random number algorythm by nexos"<<endl;
  121.         cout<<endl<< "Usage: " << argv[0] << " <0/1> <Plaintext File> <Key File> <Crypted File>" <<endl<<endl;
  122.         cout<< "<0>: decrypts <Crypted File> with <Key File> to <Plaintext File>" <<endl<<endl;
  123.         cout<< "<1>: encrypts <Plaintext File> with <Key File> (Key is generated) to <Crypted File>" <<endl<<endl;
  124.         cout<< "existing files are overwritten" << endl;
  125.         return 0;
  126.     }
  127.  
  128.   if ( atoi( argv[1]) == 0 )
  129.    decrypt(argv[2],argv[3],argv[4]);
  130.  
  131.   if ( atoi( argv[1]) == 1 )
  132.    encrypt(argv[2],argv[3],argv[4]);
  133.  
  134.         return 0;
  135. }
  136.  


Comments

You must be logged in to post comments.

 Network Access...
USER ID
PASSWORD

 Code Information
Language:
C/C++

Version:
1.1


Submitted:
2008-10-10 - 13:16:03


Author:
nexos
E-Mail
Website

Greetz:


[ Download | Report Issue ]

 Code Search
Search by Language
+ Assembly
+ ASP
+ ASP.NET
+ C#
+ C/C++
+ Cobol
+ Delphi
+ Java
+ Javascript
+ Pascal
+ Perl
+ PHP
+ Python
+ VB6
+ VB.NET

Advanced Search




 
 
By continuing past this page, and by your continued use of this site, you agree to be bound by and abide by the User Agreement.

© 2008 r00tsecurity network. All rights reserved.
[ About Us | Contact Us | Support Us | Legal | Advertise | User Agreement | Privacy Policy ]