-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
202 lines (180 loc) · 4.1 KB
/
Copy pathClient.java
File metadata and controls
202 lines (180 loc) · 4.1 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
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
import java.io.*;
import java.net.*;
import javax.crypto.Mac;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Base64;
import java.util.UUID;
public class Client extends JFrame
{
static Socket sk=null;
static DataOutputStream out=null;
static DataInputStream in=null;
JPanel jp;
JLabel jl1,jl2;
JTextField jt1,jt2;
JRadioButton jr1,jr2;
ButtonGroup bg;
JTextArea jta;
JTextField jt3;
JButton jb;
public Client() throws Exception
{
jp=new JPanel();
add(jp);
jl1=new JLabel("IP Address : ");
jt1=new JTextField(15);
jl2=new JLabel("Port No : ");
jt2=new JTextField(5);
jr1=new JRadioButton("connect");
jr2=new JRadioButton("disconnect");
bg=new ButtonGroup();
jta=new JTextArea(20,20);
jt3=new JTextField(10);
jb=new JButton("send");
jp.setLayout(new GridBagLayout());
GridBagConstraints gbc=new GridBagConstraints();
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.gridx=0;
gbc.gridy=0;
jp.add(jl1,gbc);
gbc.gridx=1;
gbc.gridy=0;
jp.add(jt1,gbc);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.gridx=0;
gbc.gridy=1;
jp.add(jl2,gbc);
gbc.gridx=1;
gbc.gridy=1;
jp.add(jt2,gbc);
gbc.fill=GridBagConstraints.HORIZONTAL;
bg.add(jr1);
bg.add(jr2);
gbc.gridx=0;
gbc.gridy=2;
jp.add(jr1,gbc);
jr1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jr1)
{
try{
getConnected();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
}
);
gbc.gridx=1;
gbc.gridy=2;
jr2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jr2)
{
try{
getDisConnected();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
}
);
jp.add(jr2,gbc);
gbc.gridx=0;
gbc.gridy=3;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.gridwidth=3;
jp.add(jta,gbc);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.gridx=0;
gbc.gridy=4;
jp.add(jt3,gbc);
gbc.gridx=3;
gbc.gridy=4;
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb)
{
try{
sending();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
}
);
jp.add(jb,gbc);
addWindowListener(new java.awt.event.WindowAdapter(){
public void windowClosing(java.awt.event.WindowEvent we)
{
System.exit(0);
}
});
}
public void getConnected() throws Exception
{
sk=new Socket(jt1.getText(),Integer.parseInt(jt2.getText()));
jta.setText("Connecting to Server\n");
in=new DataInputStream(sk.getInputStream());
out=new DataOutputStream(sk.getOutputStream());
jta.append("\nI/O streams established\n");
}
public void sending()throws Exception
{
String text="";
text=jt3.getText();
jta.append("\n\nstart generating key");
KeyGenerator k=KeyGenerator.getInstance("HmacMD5");
SecretKey key=k.generateKey();
jta.append("\nfinish generating key");
jta.append("\nkey is "+key);
jta.append("\nstarted generating mac value");
Mac mac=Mac.getInstance("HmacMD5");
mac.init(key);
out.writeUTF(text);
out.flush();
jta.append("\nsent the text to receiver");
jta.append("\noriginal key is "+key);
String encodedKey=Base64.getEncoder().encodeToString(key.getEncoded());
out.writeUTF(encodedKey);
out.flush();
jta.append("\nkey has been sent in encoded format");
jta.append("\nmessage to be sent is "+text);
byte[] plaintext=text.getBytes();
String clientMac=new String(mac.doFinal(plaintext),"UTF8");
jta.append("\nsending the mac value "+clientMac);
out.writeUTF(clientMac);
out.flush();
}
public void getDisConnected() throws Exception
{
sk.close();
jta.setText("\nclient disconnected\n");
in.close();
out.close();
}
public static void main(String args[])throws Exception
{
Client er=new Client();
er.setTitle("Client");
er.setSize(500,500);
er.setVisible(true);
}
}