-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLauncher.java
More file actions
423 lines (365 loc) · 18.6 KB
/
Launcher.java
File metadata and controls
423 lines (365 loc) · 18.6 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.security.Key;
import java.security.*;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
public class Launcher extends javax.swing.JFrame {
private final Socket readSocket;
private static int flag=0;
private final Socket writeSocket;
private ObjectOutputStream oos;
private ObjectInputStream ois;
private PublicKey publicKeyServer;
private PrivateKey keyRSAPrivate;
private static Socket socket;
public final static String UPDATE_USERS = "updateuserslist:";
public static String sessionUsername = null;
private Key AESKey,DESKey;
static int val=1;
File file;
String fileName1="E:/java/java project/plain.txt";
public Launcher(Socket readSocket,Socket writeSocket,Key AESKey,Key DESKey,ObjectInputStream ois,ObjectOutputStream oos) throws Exception {
initComponents();
this.readSocket = readSocket;
this.writeSocket = writeSocket;
this.AESKey = AESKey;
this.DESKey = DESKey;
this.oos=oos;
this.ois=ois;
new Thread(){
public void run(){
try {
while(true){
Message encryptedMessage= (Message) ois.readObject();
//int flag=0;
// System.out.println("msg recievd");
// int flag=(int)ois.readInt();
// System.out.println("int rec");
String type = encryptedMessage.getType();
MessageDecryption mess=null;
if(type.equals("AES")){
{
mess = new MessageDecryption(encryptedMessage.getMessage(),AESKey,type);
}
}else{
mess = new MessageDecryption(encryptedMessage.getMessage(),DESKey,type);
}
String plainMessageString = mess.getMessage();
if(flag==1)
{
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName1, true));
//writer.append(' ');
writer.append(plainMessageString+"\n");
writer.close();
flag=0;
}
System.out.println(plainMessageString + " FROM using "+encryptedMessage.getType());
chatBoxTextArea.append(plainMessageString+"\n");
}
} catch (Exception ex) {
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
}
}
}.start();
}
public void AesMessage() throws FileNotFoundException, IOException
{
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
String fileName = file.getAbsolutePath();
System.out.println(fileName);
String msg;
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
while((msg=br.readLine())!=null)
{
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
headerLabel = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
sendDESBtn = new javax.swing.JButton();
sendAESBtn = new javax.swing.JButton();
msgTextField = new javax.swing.JTextField();
sendFileDESBtn = new javax.swing.JButton();
sendFileAESBtn = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
chatBoxTextArea = new javax.swing.JTextArea();
graphButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
headerLabel.setBackground(new java.awt.Color(153, 255, 102));
headerLabel.setFont(new java.awt.Font("Arial", 1, 36)); // NOI18N
headerLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
headerLabel.setText("Cryptography");
headerLabel.setOpaque(true);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(headerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 664, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(headerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
);
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);
jPanel3.setBackground(new java.awt.Color(102, 255, 153));
sendDESBtn.setText("Send (DES)");
sendDESBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendDESBtnActionPerformed(evt);
}
});
sendAESBtn.setText("Send (AES)");
sendAESBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendAESBtnActionPerformed(evt);
}
});
msgTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
msgTextFieldActionPerformed(evt);
}
});
sendFileDESBtn.setText("Send File (DES)");
sendFileDESBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendFileDESBtnActionPerformed(evt);
}
});
sendFileAESBtn.setText("Send File (AES)");
sendFileAESBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendFileAESBtnActionPerformed(evt);
}
});
chatBoxTextArea.setColumns(20);
chatBoxTextArea.setRows(5);
jScrollPane1.setViewportView(chatBoxTextArea);
graphButton.setText("Show on Graph");
graphButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
graphButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(msgTextField)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sendDESBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sendAESBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(sendFileDESBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sendFileAESBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 182, Short.MAX_VALUE)
.addComponent(graphButton, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sendAESBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sendDESBtn))
.addComponent(msgTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sendFileDESBtn)
.addComponent(sendFileAESBtn)
.addComponent(graphButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
getContentPane().add(jPanel2, java.awt.BorderLayout.LINE_END);
pack();
}// </editor-fold>//GEN-END:initComponents
private void sendFileDESBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendFileDESBtnActionPerformed
JFileChooser chooser = new JFileChooser();
int values1 = chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
val=1;
if (values1 == JFileChooser.APPROVE_OPTION)
{
try {
System.out.println(file.getPath());
String fileName = file.getAbsolutePath();
System.out.println(fileName);
String msgStr;
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
chatBoxTextArea.append("file: "+fileName+" has been sent to another client at time "+new Date());
while((msgStr=br.readLine())!=null)
{
MessageEncryption messEncryption = new MessageEncryption(msgStr,DESKey,"DES");
String encryptedMsgStr = messEncryption.getMessageString();
Message encryptedMsg = new Message(encryptedMsgStr,"DES");
chatBoxTextArea.append(msgStr+" \n");
oos.writeObject(encryptedMsg);
flag=1;
// oos.writeInt(1);
}
/* if(val!=0)
{
File f = new File(fileName1);
if(f.exists()){
f.delete();
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
val=0;
}*/
} catch (Exception ex) {
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (values1 == JFileChooser.CANCEL_OPTION) {
System.out.println("No file is selected");
} else if (values1 == JFileChooser.ERROR_OPTION) {
System.out.println("Error!");
} else if (file == null) {
System.out.println("No File is chosen");
}
}//GEN-LAST:event_sendFileDESBtnActionPerformed
private void msgTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_msgTextFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_msgTextFieldActionPerformed
private void sendAESBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendAESBtnActionPerformed
try {
String msgStr = msgTextField.getText();
chatBoxTextArea.append(msgStr+"\n");
MessageEncryption messEncryption = new MessageEncryption(msgStr,AESKey,"AES");
String encryptedMsgStr = messEncryption.getMessageString();
Message encryptedMsg = new Message(encryptedMsgStr,"AES");
oos.writeObject(encryptedMsg);
System.out.println("mes sent");
//oos.writeInt(0);
System.out.println("int sent");
} catch (Exception ex) {
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_sendAESBtnActionPerformed
private void sendDESBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendDESBtnActionPerformed
try {
String msgStr = msgTextField.getText();
chatBoxTextArea.append(msgStr+"\n");
MessageEncryption messEncryption = new MessageEncryption(msgStr,DESKey,"DES");
String encryptedMsgStr = messEncryption.getMessageString();
Message encryptedMsg = new Message(encryptedMsgStr,"DES");
oos.writeObject(encryptedMsg);
// oos.writeInt(0);
} catch (Exception ex) {
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_sendDESBtnActionPerformed
private void graphButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graphButtonActionPerformed
Frame f = new Frame();
f.setBounds(200,200,800,300);
f.add(new Graph());
f.setResizable(false);
f.setVisible(true);
}//GEN-LAST:event_graphButtonActionPerformed
private void sendFileAESBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendFileAESBtnActionPerformed
//String fileName=fileName.replace("\" , '/');
JFileChooser chooser = new JFileChooser();
int values1 = chooser.showOpenDialog(null);
file = chooser.getSelectedFile();
val=1;
if (values1 == JFileChooser.APPROVE_OPTION)
{
try {
System.out.println(file.getPath());
String fileName = file.getAbsolutePath();
System.out.println(fileName);
String msgStr;
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
chatBoxTextArea.append("file: "+fileName+" has been sent to another client at time "+new Date());
while((msgStr=br.readLine())!=null)
{
MessageEncryption messEncryption = new MessageEncryption(msgStr,AESKey,"AES");
String encryptedMsgStr = messEncryption.getMessageString();
Message encryptedMsg = new Message(encryptedMsgStr,"AES");
chatBoxTextArea.append(msgStr+" \n");
flag=1;
oos.writeObject(encryptedMsg);
// oos.writeInt(1);
// System.out.println("int sent");
}
/* if(val!=0)
{
File f = new File(fileName1);
if(f.exists()){
f.delete();
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
val=0;
}*/
} catch (Exception ex) {
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (values1 == JFileChooser.CANCEL_OPTION) {
System.out.println("No file is selected");
} else if (values1 == JFileChooser.ERROR_OPTION) {
System.out.println("Error!");
} else if (file == null) {
System.out.println("No File is chosen");
}
}//GEN-LAST:event_sendFileAESBtnActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea chatBoxTextArea;
private javax.swing.JButton graphButton;
private javax.swing.JLabel headerLabel;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField msgTextField;
private javax.swing.JButton sendAESBtn;
private javax.swing.JButton sendDESBtn;
private javax.swing.JButton sendFileAESBtn;
private javax.swing.JButton sendFileDESBtn;
// End of variables declaration//GEN-END:variables
}