#include #include using namespace std; int doHash( ifstream* f, int* pCoef, char* hash, char* hashUsed, int hashSize, unsigned long fileSize) { char c = 0; int i = 0; long long p = 0; float percent; while( f->get(c)){ // Hardcoding exponentiation to avoid importing the math libraries p = pCoef[0] + pCoef[1] * i + pCoef[2] * i * i + pCoef[3] * i*i*i; if (p < 0) p = -p; while(hash[ p%hashSize ]) p++; hash[ p%hashSize ] = c; hashUsed[p%hashSize] = true; i ++; cout << "\b\b\b" << (i*100/fileSize) << "%"; } cout << "\n"; } int main (int argc, char ** argv) { ifstream::pos_type size; int sizeP1 = 0; int sizeP2 = 0; long long p; unsigned long file1Size; unsigned long file2Size; int p1Coef[4]; int p2Coef[4]; char* tableXor; char* tableInter; char* hash[2]; char* hashUsed[2]; int i = 0; unsigned long maxSize = 0; long begin, end; // Spit out the usage if not enough args if(argc < 6){ cerr << "Usage: duale inputFile1 inputFile2 outputFile \"passphrase1\" \"passphrase2\"\n"; return 1; } ifstream* inF1 = new ifstream(argv[1], ios::in|ios::binary); ifstream* inF2 = new ifstream(argv[2], ios::in|ios::binary); ofstream outF(argv[3], ios::out|ios::binary); srand(time(NULL)); sizeP1 = strlen(argv[4]); if(sizeP1 < 4) { cerr << "Passphrase 1 must be at least 4 characters long"; return -1; } sizeP2 = strlen(argv[5]); if(sizeP1 < 4) { cerr << "Passphrase 2 must be at least 4 characters long"; return -1; } // Find the coefficients from the passwords printf("%d %d\n", argv[4][0], (argv[4][0] & 0x3F) | (argv[4][0] & 0x3F) << 6); p1Coef[0] = argv[4][0]; p1Coef[1] = argv[4][1]; p1Coef[2] = argv[4][2]; p1Coef[3] = argv[4][3]; p2Coef[0] = argv[5][0]; p2Coef[1] = argv[5][1]; p2Coef[2] = argv[5][2]; p2Coef[3] = argv[5][3]; // Figure out which file is larger begin = inF1->tellg(); inF1->seekg (0, ios::end); end = inF1->tellg(); file1Size = end - begin; begin = inF2->tellg(); inF2->seekg (0, ios::end); end = inF2->tellg(); file2Size = end - begin; inF1->seekg (0, ios::beg); inF2->seekg (0, ios::beg); maxSize = (file1Size > file2Size) ? file1Size : file2Size; // Setup for doing the encryption maxSize += 0xFF; // Make sure some random data is introduced hash[0] = (char*) calloc(1, maxSize); hash[1] = (char*) calloc(1, maxSize); hashUsed[0] = (char*) calloc(1, maxSize); hashUsed[1] = (char*) calloc(1, maxSize); doHash(inF1, p1Coef, hash[0], hashUsed[0], maxSize, file1Size); doHash(inF2, p2Coef, hash[1], hashUsed[1], maxSize, file2Size); // Go through and pad each hash table with random data srand(time(NULL)); for(int i = 0; i < maxSize; i ++) if(!hashUsed[0][i] ) hash[0][i] = rand() % 256; for(int i = 0; i < maxSize; i ++) if(!hashUsed[1][i] ) hash[1][i] = rand() % 256; // Create the tables used for the output file tableXor = (char*) calloc(1, maxSize); tableInter = (char*) calloc(1, maxSize); for(int i = 0; i < maxSize; i ++){ tableXor[i] = hash[0][i] ^ hash[1][i]; tableInter[i] = hash[i%2][i]; } // Spit all needed info to the output file outF.write(reinterpret_cast(&maxSize), sizeof(unsigned long)); p = p1Coef[0] + p1Coef[1] + p1Coef[2] + p1Coef[3]; outF.write(reinterpret_cast(&p), sizeof(long long)); p = p2Coef[0] + p2Coef[1] + p2Coef[2] + p2Coef[3]; outF.write(reinterpret_cast(&p), sizeof(long long)); outF.write(reinterpret_cast(&file1Size), sizeof(unsigned long)); outF.write(reinterpret_cast(&file2Size), sizeof(unsigned long)); outF.write(tableXor, maxSize); outF.write(tableInter, maxSize); outF.close(); return 0; }