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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
#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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
#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.