#include #include #include using namespace std; int main (int argc, char ** argv) { unsigned long maxSize = 0; unsigned long evenSize = 0; unsigned long oddSize= 0; unsigned long fileSize= 0; long long evenP = 0; long long oddP = 0; long long p; long long r; bool isEven; char* tableXor; char* tableInter; int pCoef[4]; char* hashUsed; // Needed since data can be 0 // Spit out the usage if not enough args if(argc < 4){ cout << "Usage: ddec inputFile outputFile \"passphrase\" \n"; return 1; } ifstream inF(argv[1], ios::in|ios::binary); ofstream outF(argv[2], ios::out|ios::binary); srand(time(NULL)); // Find the coefficients from the passwords cout << argv[3] << "\n"; pCoef[0] = argv[3][0]; pCoef[1] = argv[3][1]; pCoef[2] = argv[3][2]; pCoef[3] = argv[3][3]; p = pCoef[0] + pCoef[1] + pCoef[2] + pCoef[3]; cout << p << "\n"; inF.read(reinterpret_cast(&maxSize), sizeof(unsigned long)); cout << maxSize << "\n"; inF.read(reinterpret_cast(&evenP), sizeof(long long)); cout << evenP << "\n"; inF.read(reinterpret_cast(&oddP), sizeof(long long)); cout << oddP << "\n"; inF.read(reinterpret_cast(&evenSize), sizeof(unsigned long)); cout << evenSize << "\n"; inF.read(reinterpret_cast(&oddSize), sizeof(unsigned long)); cout << oddSize << "\n"; hashUsed = (char*) calloc(1, maxSize); tableXor = (char*) calloc(1, maxSize); tableInter = (char*) calloc(1, maxSize); inF.read(tableXor, maxSize); inF.read(tableInter, maxSize); // Check which interleaved data set to use if( p == evenP ) { isEven = true; fileSize = evenSize; } else if ( p == oddP ) { isEven = false; fileSize = oddSize; } else { cerr << "Failed to decrypt\n"; return -1; } // Decrypt for(int i = 0; i < fileSize; i ++){ p = pCoef[0] + pCoef[1] * i + pCoef[2] * i * i + pCoef[3] * i*i*i; if( p < 0 ) p = -p; while(hashUsed[p%maxSize]) p++; r = p % maxSize; hashUsed[r] = true; if( ((!(r%2) && isEven) || ((r%2) && !isEven))) outF.put(tableInter[r]); else outF.put(tableInter[r] ^ tableXor[r]); } outF.close(); return 0; }