-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffieHellmanSend.java
More file actions
122 lines (92 loc) · 2.49 KB
/
DiffieHellmanSend.java
File metadata and controls
122 lines (92 loc) · 2.49 KB
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
package project1;
import java.util.*;
import java.io.*;
import java.net.*;
public class DeffieHellmanSend
{
private static String message;
DatagramSocket theSocket = null;
int serverPort = 9099;
Scanner sc;
static int x2,n,q,y2,b;
DatagramPacket theSendPacket;
DatagramPacket theReceivedPacket;
InetAddress theServerAddress;
byte[] outBuffer;
byte[] inBuffer;
public DeffieHellmanSend()
{
try
{
theSocket = new DatagramSocket();
// but if you want to connect to your remote server, then alter the
//theServer address below
InetAddress theServer = InetAddress.getLocalHost();
theSocket.connect(theServer,serverPort);
System.out.println("Requested user");
}
catch (SocketException ExceSocket)
{
System.out.println("Socket creation error:"
+ExceSocket.getMessage());
}
catch (UnknownHostException ExceHost)
{
System.out.println("Socket host unknown : "+ExceHost.getMessage());
}
}
public void keyGen()
{
sc=new Scanner(System.in);
System.out.print("Enter n=");
n=sc.nextInt();
System.out.print("Enter q=");
q=sc.nextInt();
System.out.print("Enter x2=");
x2=sc.nextInt();
y2= (int) ((Math.pow(n,x2)) % q);
System.out.println("Computed key of requested user "+y2);
}
public void send()
{
// the place to store the sending and receiving data
inBuffer = new byte[500];
outBuffer = new byte[50];
try
{
message=y2+"";
// System.out.println("private key:"+message);
outBuffer = message.getBytes();
// the server details
theServerAddress = theSocket.getLocalAddress();
// build up a packet to send to the server
theSendPacket = new DatagramPacket(outBuffer, outBuffer.length,
theServerAddress, serverPort);
// send the data
theSocket.send(theSendPacket);
}
catch (IOException ExceIO)
{
System.out.println("Client getting data error : "+ExceIO.getMessage
());
}
}
public void receive()
{
try
{
// get the servers response within this packet
theReceivedPacket = new DatagramPacket(inBuffer, inBuffer.length);
theSocket.receive(theReceivedPacket);
// the server response is...
message = new String(theReceivedPacket.getData
(),0,theReceivedPacket.getLength());
System.out.println("Computed key of Big data owner : "+message);
b=Integer.parseInt(message);
}
catch(IOException | NumberFormatException e)
{
System.out.println("E1"+e);
}
}
}