· 7 years ago · Jul 28, 2018, 04:38 PM
1public class Discovery {
2
3 byte[] buffer;
4
5 public Socket socketStartConnect() throws UnknownHostException, IOException {
6 String ip = "192.168.0.100";
7 int port = 1234;
8 Socket clientSocket = new Socket(ip, port);
9 if (clientSocket.isConnected()) {
10 System.out.println("It is connected to the server which is " +clientSocket.getInetAddress());
11
12 } else if (clientSocket.isClosed()) {
13 System.out.println("Connection Failed");
14 }
15 return clientSocket;
16 }
17
18 public byte[] encryption(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException,
19 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
20
21 Cipher aesCipher = Cipher.getInstance("AES");
22
23 aesCipher.init(Cipher.ENCRYPT_MODE, secretkey());
24 byte[] textEncrypted = aesCipher.doFinal(str.getBytes());
25
26 System.out.println("Text encrypted : " + textEncrypted.toString());
27
28 aesCipher.init(Cipher.DECRYPT_MODE, secretkey());
29 byte[] textDecrypted = aesCipher.doFinal(textEncrypted);
30
31 System.out.println("Text Decryted : " + new String(textDecrypted));
32
33 return textEncrypted;
34 }
35
36 public byte[] decryption(String encryption) throws IllegalBlockSizeException, BadPaddingException,
37 InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
38
39 Cipher aesCipher = Cipher.getInstance("AES");
40 aesCipher.init(Cipher.DECRYPT_MODE, secretkey());
41 byte[] textDecrypted = aesCipher.doFinal(encryption.getBytes());
42
43 System.out.println("Text Decryted : " + new String(textDecrypted));
44 return textDecrypted;
45
46 }
47
48 public SecretKey secretkey() {
49 byte[] kBytes = new byte[16];
50 SecretKey secretKey = null;
51
52 try {
53 String passString = "1234567890123456";
54 byte[] passByte = passString.getBytes("UTF-8");
55 System.arraycopy(passByte, 0, kBytes, 0, Math.min(kBytes.length, passByte.length));
56 secretKey = new SecretKeySpec(kBytes, "AES");
57 } catch (UnsupportedEncodingException e) {
58
59 }
60 return secretKey;
61 }
62
63
64
65 public void sendEncryption(byte[] encryption) throws Exception {
66
67 // Get the socket's output stream
68 Socket socket = socketStartConnect();
69 OutputStream socketOutput = socket.getOutputStream();
70
71 // total byte
72 byte[] totalByteCombine = new byte[encryption.length];
73 System.arraycopy(encryption, 0, totalByteCombine, 0, encryption.length);
74
75 socketOutput.write(totalByteCombine, 0, totalByteCombine.length);
76 System.out.println("Content sent successfully");
77
78
79
80
81 //HERE IS THE ISSUE
82 // I am supposed to receive something from Python Side
83 InputStream socketInput = socket.getInputStream();
84 String messagetype = socketInput.readUTF();
85 System.out.println(messagetype);
86
87
88
89 }
90
91
92
93 public static void main(String[] args) throws Exception {
94
95 // Must Be 16 Bytes
96 String passphrase = "abcdefghijklmnop";
97
98 Discovery client = new Discovery();
99 byte[] encryption = client.encryption(passphrase);
100 client.sendEncryption(encryption);
101
102 }
103}
104
105import socket
106import ssl
107import hashlib
108import os
109from Crypto.Cipher import AES
110import hashlib
111from Crypto import Random
112
113sHost = ''
114sPort = 1234
115
116
117def bindSocket():
118 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #IPv4 and TCP
119 try:
120 s.bind((sHost,sPort))
121 print("Socket created and binded")
122 except socket.error as msgError:
123 print(msgError)
124 print("Error in Binding Socket")
125 return s #so that we can use it
126
127
128def socketConnect():
129 s.listen(1) #listen to 1 connection at a time
130
131
132 while True:
133 try:
134 conn, address = s.accept() #Accept connection from client
135 print ("Connected to: " + address[0] + ":" +str(address[1]))
136 except socket.error as error:
137 print ("Error: {0}" .format(e))
138 print ("Unable to start socket")
139 return conn
140
141
142def decrypt(str):
143 key ="1234567890123456"
144 key = key.encode('utf-8')
145
146 decipher = AES.new(key[:16])
147
148 decrypt = decipher.decrypt(str[:16])
149
150 print("it works")
151
152 return decrypt.decode('utf-8')
153
154def encrypt(str):
155 key ="FYP-0028-2018-T1"
156 key = bytes(key,'utf-8')
157 cipher =AES.new(key)
158 encrypt = cipher.encrypt(str)
159
160 return encrypt
161
162
163
164def loopCommand(conn):
165 while True:
166 passphrase = "abcdefghijklmnop"
167 data = conn.recv(1024)#receive the message sent by client
168
169 if decrypt(data) == passphrase:
170 print("Passphrase is the same, generating new passphrase for Client Checking")
171
172
173 #This is the part where I send it to JAVA
174
175 newPassphrase = "serversocket1234"
176 newEncrypt = encrypt(newPassphrase)
177 print(type(newEncrypt))
178 conn.send(newEncrypt)
179 print("Another passphrase sent")
180
181 else:
182 error = "Wrong passphrase"
183 rst = "RST"
184 print(error)
185 conn.send(rst.encode('utf-8'))
186
187 print("DONE")
188
189
190
191
192s = bindSocket()
193
194while True:
195 try:
196 conn = socketConnect()
197 loopCommand(conn)
198 except:
199 pass