Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I've made a program a while back that I just discovered was broken. Which is rather strange since it functions correctly on very certain file sizes.I've spend upwards of several days trying to figure out the issue however I am running out of ideas. So I am here to ask can someone spot the error with this code.

C++
#ifndef _code_h_
#define _code_h_

#include <iostream>
#include <string>
#include <thread>
#include <Windows.h>
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <fstream>
#include <array>
#include "lib/aes.h"
#include "lib/modes.h"      
#include "lib/filters.h"  
using namespace std;
using namespace CryptoPP;

class stringAesCypher
{
public:
	void encode (string data,string Skey,string &out)
	{
		byte key[16];
		byte iv[16];
		string bin;
		string theKey(Skey);
		int i(0);
		theKey.append("t;rke;tlrke65409654ytr");

		while(i != 16)
		{
			key[i] = theKey[i];
			iv[i] = theKey[i];
			i++;
		}
		// Encryptor
		CryptoPP::ECB_Mode< CryptoPP::AES >::Encryption 
		//Encryptor( key, sizeof(key), iv );
		Encryptor( key, sizeof(key));
	  
	  // Encryption
	  CryptoPP::StringSource( data, true,
		new CryptoPP::StreamTransformationFilter( Encryptor,
		  new CryptoPP::StringSink( out )
		) // StreamTransformationFilter
	  ); // StringSource
	}

	void decode (string CT,string Skey,string &out)
	{
		byte key[16];
		byte iv[16];
		string bin;
		string theKey(Skey);
		
		int i(0);
		theKey.append("t;rke;tlrke65409654ytr");
		while(i != 16)
		{
			key[i] = theKey[i];
			iv[i] = theKey[i];
			i++;
		}
		// Decryptor
		CryptoPP::ECB_Mode< CryptoPP::AES >::Decryption
		// Decryptor( key, sizeof(key), iv );
		Decryptor( key, sizeof(key) );

	  // Decryption
	  CryptoPP::StringSource( CT, true,
		new CryptoPP::StreamTransformationFilter( Decryptor,
		  new CryptoPP::StringSink( out )
		) // StreamTransformationFilter
	  ); // StringSource
	}
};

class Core
{
private:
	string ram[8];
	string ramTemp[8];
	string theMode;
	string theKey;

public:
	void wipeRam()
	{
		for(int i = 0; i != 8; i++)
		{
			ram[i].resize(0);
			ramTemp[i].resize(0);
		}
	}
	
	void cores(int rRam)
	{
		stringAesCypher Code;
		if(theMode == "E")
		{
			Code.encode(ram[rRam],theKey,ramTemp[rRam]);
			ram[rRam] = ramTemp[rRam];
		}
		if(theMode == "D")
		{
			Code.decode(ram[rRam],theKey,ramTemp[rRam]);	
			ram[rRam] = ramTemp[rRam];
		}

	}
	
	void startCores()
	{
		thread t1(&Core::cores,this,0);
		thread t2(&Core::cores,this,1);	
		thread t3(&Core::cores,this,2);	
		thread t4(&Core::cores,this,3);	
		thread t5(&Core::cores,this,4);	
		thread t6(&Core::cores,this,5);	
		thread t7(&Core::cores,this,6);	
		thread t8(&Core::cores,this,7);	
		t1.join();
		t2.join();
		t3.join();
		t4.join();
		t5.join();
		t6.join();
		t7.join();
		t8.join();
	}

	void splitIT(string &buffer)
	{
		int round(0);
		int theTotal(0);
			if(theMode == "E")
			{
				theTotal = 12500000;
			}
			if(theMode == "D")
			{
				theTotal = 12500016;
			}
		for(int i = 0; i != 8; i++)
		{
			if(round > buffer.length())
			{
				break;
			}
			else
			{
				ram[i] = buffer.substr(round,theTotal);
				round = round + theTotal;
			}
		}
	}
	
	void readWriteSystem(string inFileName,string outFileName,string mode,string key)
	{
		int roundsOnSubBuffer(0);
		int theTotalRamData(0);
		int ramSlot(0);
		int ramPlace(0);
		theKey = key;
		theMode = mode;
		size_t buffer_size; // the read buffer
		// sets the buffer
		if(mode == "E")
		{
			buffer_size = 100000000; // for encode
		}
		if(mode == "D")
		{
			buffer_size = 100000128; // for decode		
		}
		string buffer(buffer_size,'\0'); // the data buffer
		// The File Streams
		ifstream fileStreamIn(inFileName,ios::binary);
		ofstream fileStreamOut(outFileName,ios::binary);
		while(fileStreamIn)
		{
			buffer.resize(buffer_size);
			fileStreamIn.read(&buffer.front(), buffer_size);
			size_t count = fileStreamIn.gcount();
			buffer.resize(count);
			splitIT(buffer);
			startCores();
			buffer = "";
	
			for(int i = 0; i != 8; i++)
			{
				theTotalRamData = theTotalRamData + ram[i]
				.length();
			}
			
			for(int i = 0; i != 8; i++)
			{
				buffer.append(ram[i]);
			}
			//cout << "OUT " << buffer.length() << endl;
			fileStreamOut.write(buffer.c_str(),buffer.length());
			
			wipeRam();
			if (!count) 
				break;  
		}
		fileStreamIn.close();
		fileStreamOut.close();
	}
};

#endif
Posted
Comments
nv3 2-Jan-16 11:58am    
This is just a fragment of the entire program. What makes you believe that the bug is in there. And what is the error you get? We can't see you screen. The more information you give, the larger the possibility that someone can help you.
Member 10657083 2-Jan-16 22:47pm    
I strongly believe that the problem lies within my code. The reason for this is because the encode and decode functions for AES are proven. Crypto++ is pretty solid and I have had no issues with it. However if you feed it bad cipher text it crashes the program. For that reason I have come to the conclusion that it is due to my code. However I am have yet to be able to pinpoint the issue with my code. Their are no compiler errors with my code, however the program does crash during decryption likely due to the bad cyphertext. I simply do not know where the issue lies in what I have written so far and that is why I am here. I do not know how to correct this problem.
Peter_in_2780 2-Jan-16 23:41pm    
Hmmm. Feeding "bad cyphertext" (of the right length) will just result in garbage cleartext. This smells to me like a memory management issue, maybe buffer overrun. Can you get any more information about the crash, perhaps from error logs, or running it in a debugger?
Member 10657083 3-Jan-16 0:56am    
The debuger did not provide any information. But it is worth noting that I compiled the program via command line. But I ran a little test and the result was rather interesting.

string data = "faaf networks is faaf";
string out;
stringAesCypher test;
test.encode(data,"1234",out);
test.decode(out,"1234",data);

out.resize(16);
test.decode(out,"1234",data);// the crash starts here

From this test the program will crash at the last decode function, should the ciphertext be bad. I don't know why it does this when with other ciphers it does not.
KarstenK 3-Jan-16 4:13am    
Check that the libraries are multi-threading safe.
Use some test data to find the bug. And start with a simple one thread case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900