/*Takes the current BigInt's data value and raises it to the power of the desired exponent*/
	void Power(int exponent) {
		BigInt temp;
		Copy(*this, &temp);
		if (exponent == 0) {
			temp.SetNumber(1);
		}
		for (int i = 0; i < exponent - 1; i++) {
			temp.Multiply(*this);
			//temp.PrintNumber();
		}
		Copy(temp, this);
	}
int main(int argc, char *argv[]) {
	int sd = 0;
	int port = 5000;
	struct sockaddr_in serv_addr;
	struct hostent *server;
	BigInt xa;
	
	if (argc >= 2) {
		//memset(sendbuff, 0, sizeof(sendbuff)); //zeroing send buffer memory
	
		sd = socket(AF_INET, SOCK_STREAM, 0); //creating the TCP socket
		server = gethostbyname(argv[1]); //getting the server information from the ip string
		
		serv_addr.sin_family = AF_INET; //setting the ip version to IPv4
		
		memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, sizeof(server->h_addr)); //copying the server address from the hostent struct to the sockaddr_in struct
		serv_addr.sin_port = htons(port); //setting the port to network byte order
		
		connect(sd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); //connecting the the server
		
		BigInt xa = BigInt(rand() % 200);
		
		string data[10];
		data[0] = "HELLO WORLD THIS IS AN ENCRYPTION TEST";
		data[1] = "LFTR is a type of thorium molten salt reactor (TMSR). Molten-salt-fueled reactors (MSRs) supply the nuclear fuel in the form of a molten salt mixture.";
		data[2] = "In a LFTR, thorium and uranium-233 are dissolved in carrier salts, forming a liquid fuel.";
		data[3] = "The LFTR needs a mechanism to remove the fission products from the fuel salt and recover at least the fissile material. Some fission products in the salt absorb neutrons and reduce the production of new fissile fuel. ";
		data[4] = "The LFTR resists diversion of its fuel to nuclear weapons in four ways: first, the thorium-232 breeds by converting first to protactinium-233, which then decays to uranium-233. If the protactinium remains in the reactor, small amounts of U-232 are also produced. U-232 has a decay chain product (thallium-208) that emits powerful, dangerous gamma rays. These are not a problem inside a reactor, but in a bomb, they complicate bomb manufacture, harm electronics and reveal the bomb's location.";
		data[5] = "Conventional reactors consume less than one percent of the mined uranium, leaving the rest as waste. With well working reprocessing LFTR may consume about 99% of its thorium fuel. The improved fuel efficiency means that 1 ton of natural thorium in a LFTR produces as much energy as 35 t of enriched uranium in conventional reactors (requiring 250 t of natural uranium), or 4,166,000 tons of black coal in a coal power plant.";
		data[6] = "Fluoride salt mixtures have melting points ranging from 300 to over 600 degrees Celsius. The salts, especially those with beryllium fluoride, are very viscous near their freezing point. This requires careful design and freeze protection in the containment and heat exchangers. Freezing must be prevented in normal operation, during transients, and during extended downtime.";
		data[7] = "The FUJI MSR was a design for a 100 to 200 MWe molten-salt-fueled thorium fuel cycle thermal breeder reactor, using technology similar to the Oak Ridge National Laboratory Reactor Experiment. It was being developed by a consortium including members from Japan, the United States, and Russia. As a breeder reactor, it converts thorium into nuclear fuels.";
		data[8] = "Kirk Sorensen, former NASA scientist and Chief Nuclear Technologist at Teledyne Brown Engineering, has been a long time promoter of thorium fuel cycle and particularly liquid fluoride thorium reactors. He first researched thorium reactors while working at NASA, while evaluating power plant designs suitable for lunar colonies.";
		data[9] = "The People's Republic of China has initiated a research and development project in thorium molten-salt reactor technology. It was formally announced at the Chinese Academy of Sciences (CAS) annual conference in January 2011. Its ultimate target is to investigate and develop a thorium based molten salt nuclear system in about 20 years. An expected intermediate outcome of the TMSR research program is to build a 2 MW pebble bed fluoride salt cooled research reactor in 2015, and a 2 MW molten salt fueled research reactor in 2017.";
		string encrypted = "";
		
		BigInt n(3);
		BigInt q(17);
	
		BigInt xb(0);
		
		int sendbuffInt[256];
		char sendbuffChar[1024];
		int bufsizeChar = 1024;
		int bufsize = 256;
		int nbytes = 0;
		int recvbuffInt[256];
		memset(recvbuffInt, 0, sizeof(recvbuffInt)); //zeroing recv buffer memory
		memset(sendbuffChar, 0, sizeof(sendbuffChar));  //zeroing send char buffer memory
		memset(sendbuffInt, 0, sizeof(sendbuffInt));  //zeroing send int buffer memory
		
		BigInt ya;
		BigInt yb;
		BigInt k;
		BigInt::Copy(n, &ya);
		ya.Power(xa.ToInt());
		ya = BigInt(ya.Modulo(q));
		
		for (int ii = 0; ii < bufsize; ii++) {
			sendbuffInt[ii] = ya.data[ii];
		}
		
		write(sd, &sendbuffInt, sizeof(sendbuffInt)); //writing the send buffer data to the socket to the server
		
		nbytes = read(sd, &recvbuffInt, sizeof(recvbuffInt));
		
		if (nbytes > 0 && nbytes <= sizeof(recvbuffInt)) {
			yb.SetNumber(recvbuffInt, 256);
			
			BigInt::Copy(yb, &k);
			k.Power(xa.ToInt());
			k = BigInt(k.Modulo(q));
			
			/*for (int i = 0; i < data.size(); i++) {
				char c = data.at(i) + k.ToInt();
				encrypted += c;
			}*/
			for (int zz = 0; zz < 10; zz++) {
				encrypted = "";
				EncryptPacket(k.ToInt(), data[zz], &encrypted);
			
				strcpy(sendbuffChar, encrypted.c_str());
				int slength = encrypted.size();
				write(sd, &slength, sizeof(int));
				write(sd, &sendbuffChar, sizeof(sendbuffChar));
			
				cout << encrypted << endl << endl;
			}
			
			SendEndPacket(sd);
		}
		//ka.PrintNumber();
		//cout << ka.ToInt() << endl;

		//BigInt kb;
		//BigInt::Copy(ya, &kb);
		//kb.Power(xb.ToInt());
		//kb = BigInt(kb.Modulo(q));
		//kb.PrintNumber();
		//cout << kb.ToInt() << endl;

		/*for (int i = 0; i < encrypted.size(); i++) {
			char c = encrypted.at(i) - kb.ToInt();
			decrypted += c;
		}
		cout << decrypted << endl;*/
		
		close(sd); //closing the socket
	}

	return 0;
}