Need help with encryption!! c++/cli 
May 8, 2018 at 11:40pm UTC  
Reimplement your calculator to encrypt the communication between the clients and the server. Use symmetric encryption for the messages. Modify your application-level protocol to include the delivery of the secret key at the beginning of the session. The client should encrypt the secret key using asymmetric encryption and send it to the server. Server should decrypt it and then both the client and the server should use it throughout the session.
servermain.cpp
1#include "Server.h" 
//Started, not finished UDP server 
#using "system.dll" 
using  namespace  System;
using  namespace  System::IO;
using  namespace  System::Net;
using  namespace  System::Net::Sockets;
using  namespace  System::Text;
using  namespace  System::Collections;
#include <cstdlib>   //for system("PAUSE") 
int  main(array<String^>^ argv)
{
	int  serverPort = 9999;
	// three possible ways to set the IP address 
	// The current IP address of the server computer 
	// should be set the same way on the client 
	IPAddress^ ipAddress = IPAddress::Parse("127.0.0.1" );
	IPEndPoint^ receivePoint = gcnew IPEndPoint(ipAddress, serverPort);
	//Initialize a new instance of the UdpClient class and bind it to the local endpoint. 
	UdpClient^ udpClient = gcnew UdpClient(receivePoint);
	//The following IPEndPoint object will allow us to read datagrams sent from any source. 
	IPEndPoint^ remoteIpEndPoint = gcnew IPEndPoint(IPAddress::Any, 0);
	while  (true )
	{
		Console::WriteLine("Datagram server waiting for packets" );
		// Block until a message returns on this socket from a remote host. 
		array<Byte>^ receivedBytes = udpClient->Receive(remoteIpEndPoint);
		// receivedData will be input from user example: ( ( 15 / (7 -  
		String^ receivedData = Encoding::ASCII->GetString(receivedBytes);
		
		Console::WriteLine("Packet received:" );
		Console::WriteLine("Length: "  + receivedBytes->Length);
		Console::WriteLine("Containing: "  + receivedData);
		Console::Write("Echo data back to client..." );
		udpClient->Connect(remoteIpEndPoint->Address, remoteIpEndPoint->Port);
		// result 5 
		udpClient->Send(receivedBytes, receivedBytes->Length);
		Console::WriteLine("Packet sent." );
	}
}
server.h
1#ifndef SERVER_H 
#define SERVER_H 
using  namespace  System;
using  namespace  System::Collections;
ref class  Calculator
{
public :
	// constructor 
	Calculator() {}
	// member functions 
	void  parseString(String^);
	void  reversePolishNotation(Queue^);
	Double calculateFunction();
private :
	// variables 
	Queue^ inputQueue = gcnew Queue;
	Queue^ secondQueue = gcnew Queue;
	Queue^ thirdQueue = gcnew Queue;
	Stack^ stack1 = gcnew Stack;
	Stack^ stack2 = gcnew Stack;
};
#endif  
server.cpp
1#include "Server.h" 
// define your functions here 
//System::Void Problem3Server::Form1::convertToInfix(Queue^ parQueue) 
//checks for spaces & parses the string 
void  Calculator::parseString(String^ astr)
{
	String^ currentindex = "" ;
	for  (int  i = 0; i < astr->Length; i++)
	{
		while  (Convert::ToString(astr[i]) != " " )
		{
			currentindex += astr[i];
			//inputQueue->Enqueue(); 
			i += 1;
			if  (i > astr->Length - 1)
			{
				break ;
			}
		}
		inputQueue->Enqueue(currentindex);
		currentindex = "" ;
	}
}
//converts infix to rpn 
void  Calculator:: reversePolishNotation(Queue^ inputQueue)
{
	String^ token;
	while  (inputQueue->Count != 0)
	{
		token = Convert::ToString(inputQueue->Dequeue());
		if  (token != "+"  && token != "-"  && token != "*"  && token != "/"  && token != "("  && token != ")" )
		{
			thirdQueue->Enqueue(token);
		}
		if  (token == "+"  || token == "-"  || token == "*"  || token == "/" )
		{
			while  ((stack1->Count != 0 && (((token == "-"  || token == "+" ) && (stack1->Peek() == "/"  || stack1->Peek() == "*" ))
				|| ((token == "-"  || token == "+" ) && (stack1->Peek() == "-"  || stack1->Peek() == "+" ))
				|| ((token == "*"  || token == "/" ) && (stack1->Peek() == "*"  || stack1->Peek() == "/" )))
				&& (stack1->Peek() != "(" )))
			{
				thirdQueue->Enqueue(stack1->Pop());
			}
			stack1->Push(token);
		}
		if  (token == "(" )
		{
			stack1->Push(token);
		}
		if  (token == ")" )
		{
			String^ idx = Convert::ToString(stack1->Pop());
			while  (idx != "(" )//stack1->Peek() != "(") 
			{
				thirdQueue->Enqueue(idx);
				idx = Convert::ToString(stack1->Pop());
			}
		}
	}
	if  (inputQueue->Count == 0)
	{
		while  (stack1->Count != 0)
		{
			thirdQueue->Enqueue(stack1->Pop());
		}
	}
}
//calculates function 
Double Calculator::calculateFunction()
{
	Double first = 0;
	Double second = 0;
	String^ character = Convert::ToString(thirdQueue->Dequeue());
	// while parQueue is not empty 
	while  (character != "" ) //queue2->Count != 0) 
	{
		if  (character == "+" )//queue2->Peek() == "+") 
		{
			first = Convert::ToDouble(stack2->Pop());
			second = Convert::ToDouble(stack2->Pop());
			// perform addition on the top two popped elements on the stack 
			stack2->Push(first + second);
			// dequeue "+" 
			// queue2->Dequeue(); 
		}
		else  if  (character == "-" ) // queue2->Peek() == "-") 
		{
			first = Convert::ToDouble(stack2->Pop());
			second = Convert::ToDouble(stack2->Pop());
			// perform subtraction on the top two popped elements on the stack 
			stack2->Push(second - first);
			// dequeue "-" 
			//queue2->Dequeue(); 
		}
		else  if  (character == "*" )//queue2->Peek() == "*") 
		{
			first = Convert::ToDouble(stack2->Pop());
			second = Convert::ToDouble(stack2->Pop());
			// perform multiplication on the top two popped elements on the stack 
			stack2->Push(first * second);
			// dequeue "*" 
			//queue2->Dequeue(); 
		}
		else  if  (character == "/" )//queue2->Peek() == "/") 
		{
			first = Convert::ToDouble(stack2->Pop());
			second = Convert::ToDouble(stack2->Pop());
			// perform division on the top two popped elements on the stack 
			stack2->Push(second / first);
			// dequeue "/" 
			//queue2->Dequeue(); 
		}
		else 
		{
			stack2->Push(character);// queue2->Dequeue()); 
		}
		if  (thirdQueue->Count != 0)
			character = Convert::ToString(thirdQueue->Dequeue());
		else 
			break ;
	}
	Double result = Convert::ToDouble(stack2->Pop());
	return  result;
}
Last edited on May 8, 2018 at 11:45pm UTC  
 
May 8, 2018 at 11:46pm UTC  
client (Form1.cpp)
1#include "Form1.h" 
#include <Windows.h> 
using  namespace  System;
using  namespace  System::IO;
using  namespace  Problem1_VS2013Solution;
using  namespace  System::Collections;
int  WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char *, int  nShowCard)
{
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(true );
	Application::Run(gcnew Form1);
	return  0;
}
System::Void Problem1_VS2013Solution::Form1::parse(String^ astr)
{
	String^ currentindex = "" ;
	for  (int  i = 0; i < astr->Length; i++)
	{
		while  (Convert::ToString(astr[i]) != " " )
		{
			currentindex += astr[i];
			//inputQueue->Enqueue(); 
			i += 1;
			if  (i > astr->Length-1)
			{
				break ;
			}
		}
		
		inputQueue->Enqueue(currentindex);
		currentindex = "" ;
	}
}
System::Void Problem1_VS2013Solution::Form1::convertToInfix(Queue^ parQueue)
{
	String^ token;
	while  (parQueue->Count != 0)
	{
		token = Convert::ToString(parQueue->Dequeue());
		if  (token != "+"  && token != "-"  && token != "*"  && token != "/"  && token != "("  && token != ")" )
		{
			queue2->Enqueue(token);
		}
		if  (token == "+"  || token == "-"  || token == "*"  || token == "/" )
		{
			while  ((stack1->Count != 0 && (((token == "-"  || token == "+" ) && (stack1->Peek() == "/"  || stack1->Peek() == "*" ))
				|| ((token == "-"  || token == "+" ) && (stack1->Peek() == "-"  || stack1->Peek() == "+" ))
				|| ((token == "*"  || token == "/" ) && (stack1->Peek() == "*"  || stack1->Peek() == "/" )))
				&& (stack1->Peek() != "(" )))
			{
				queue2->Enqueue(stack1->Pop());
			}
			stack1->Push(token);
		}
		if  (token == "(" )
		{
			stack1->Push(token);
		}
		if  (token == ")" )
		{
			String^ idx = Convert::ToString(stack1->Pop());
			while  (idx != "(" )//stack1->Peek() != "(") 
			{
				queue2->Enqueue(idx);
				idx = Convert::ToString(stack1->Pop());
			}
		}
	}
	if  (parQueue->Count == 0)
	{
		while  (stack1->Count != 0)
		{
			queue2->Enqueue(stack1->Pop());
		}
	}
}
System::Double Problem1_VS2013Solution::Form1::calculateFunction()
{
	// checks if checked box is not checked 
	/*
	if (!(InfixRpn->Checked))
	{
		// while the queue is not empty
		while (queue2->Count != 0)
		{
			// dequeues "(" and ")"
			if (queue2->Peek() == "(" || queue2->Peek() == ")")
			{
				queue2->Dequeue();
			}
			// adds each element in the queue as long as its not "(" and ")"
			parQueue->Enqueue(queue2->Dequeue());
		}
	}*/ 
	Double first = 0;
	Double second = 0;
	String^ character = Convert::ToString(queue2->Dequeue());
	// while parQueue is not empty 
	while  (character != "" ) //queue2->Count != 0) 
	{
		if  (character == "+" )//queue2->Peek() == "+") 
		{
			first = Convert::ToDouble(stack2->Pop());
			second = Convert::ToDouble(stack2->Pop());
			// perform addition on the top two popped elements on the stack 
			stack2->Push(first + second);
			// dequeue "+" 
			// queue2->Dequeue(); 
		}
		else  if  (character == "-" ) // queue2->Peek() == "-") 
		{
			first = Convert::ToDouble(stack2->Pop());
			second = Convert::ToDouble(stack2->Pop());
			// perform subtraction on the top two popped elements on the stack 
			stack2->Push(second - first);
			// dequeue "-" 
			//queue2->Dequeue(); 
		}
		else  if  (character == "*" )//queue2->Peek() == "*") 
		{
			first = Convert::ToDouble(stack2->Pop());
			second = Convert::ToDouble(stack2->Pop());
			// perform multiplication on the top two popped elements on the stack 
			stack2->Push(first * second);
			// dequeue "*" 
			//queue2->Dequeue(); 
		}
		else  if  (character == "/" )//queue2->Peek() == "/") 
		{
			first = Convert::ToDouble(stack2->Pop());
			second = Convert::ToDouble(stack2->Pop());
			// perform division on the top two popped elements on the stack 
			stack2->Push(second / first);
			// dequeue "/" 
			//queue2->Dequeue(); 
		}
		else 
		{
			stack2->Push(character);// queue2->Dequeue()); 
		}
		if  (queue2->Count != 0)
			character = Convert::ToString(queue2->Dequeue());
		else 
			break ;
	}
	Double result = Convert::ToDouble(stack2->Pop());
	return  result;
}
 
Topic archived. No new replies allowed.