-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.java~
More file actions
92 lines (88 loc) · 2.38 KB
/
server.java~
File metadata and controls
92 lines (88 loc) · 2.38 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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class server extends Thread{
public static ServerSocket serversocket;
private DataInputStream in;
private DataOutputStream out;
private Socket server;
public server() throws IOException{
serversocket=new ServerSocket(1993);
}
public void run(){
while(true){
try {
server=serversocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
while(true){
in =new DataInputStream(server.getInputStream());
out=new DataOutputStream(server.getOutputStream());
String clntmsg=in.readUTF();
String decmsg=decipher(clntmsg);
System.out.println("client says(cipher):"+clntmsg);
System.out.println("client says(plain text):"+decmsg);
System.out.print("server:");
Scanner s=new Scanner(System.in);
out.writeUTF(cipher(s.nextLine()));
}
}
catch(Exception ex){
System.out.println(ex.getLocalizedMessage());
}
}
}
public static String decipher(String cipher){
String decmsg="",tmp="";
DECAES decaes=new DECAES();
for(int i=0;i<cipher.length();){
tmp=decaes.decrypt(cipher.substring(i,i+32));
i+=32;
}
for(int i=0;i<tmp.length()-1;i+=2){
if(tmp.charAt(i)=='5'&&tmp.charAt(i+1)=='e')
break;
decmsg+=""+tmp.charAt(i)+tmp.charAt(i+1);
}
String result="";
char ch;
for(int i=0;i<decmsg.length();i+=2){
ch=(char)Integer.parseInt(decmsg.substring(i,i+2),16);
result+=ch;
}
return result;
}
public static String cipher(String msg){
String result="",tmp;
for(int i=0;i<msg.length();++i){
tmp=Integer.toHexString(msg.charAt(i));
if(tmp.length()<1)
tmp="0"+tmp;
result+=tmp;
}
while(result.length()%32!=0){
tmp=Integer.toHexString('^');
if(tmp.length()<1)
tmp="0"+tmp;
result+=tmp;
}
tmp=result;
result="";
AES aes=new AES();
for(int i=0;i<tmp.length();){
result+=aes.encrypt(tmp.substring(i,i+32));
i+=32;
}
return result;
}
public static void main(String[] args) throws IOException {
server s=new server();
s.start();
}
}