· 7 years ago · Nov 24, 2018, 12:18 PM
1import java.io.*;
2import java.net.*;
3import java.io.UnsupportedEncodingException;
4import java.security.MessageDigest;
5import java.security.NoSuchAlgorithmException;
6import java.util.Arrays;
7import java.util.Base64;
8import javax.crypto.Cipher;
9import javax.crypto.spec.SecretKeySpec;
10import java.io.File;
11import java.io.IOException;
12import java.nio.file.*;
13import java.util.Random;
14import java.math.BigInteger;
15
16
17public class Server {
18 public static void main(String[] args) throws IOException {
19 ServerSocket serverSocket = null;
20 String key = "brakklucza";
21
22 Random rand = new Random();
23
24
25
26 try {
27 serverSocket = new ServerSocket(4444);
28 } catch (IOException ex) {
29 System.out.println("Can't setup server on this port number. ");
30 }
31
32 Socket socket = null;
33 DataInputStream in = null;
34 FileOutputStream out = null;
35
36 try {
37 socket = serverSocket.accept();
38 } catch (IOException ex) {
39 System.out.println("Can't accept client connection. ");
40 }
41
42 try {
43 in = new DataInputStream(socket.getInputStream());
44 } catch (IOException ex) {
45 System.out.println("Can't get socket input stream. ");
46 }
47
48 try {
49 out = new FileOutputStream("./wyk.pdf");
50 } catch (FileNotFoundException ex) {
51 System.out.println("File not found. ");
52 }
53
54 DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
55
56 byte[] bytes = new byte[16*4096];
57
58 int count;
59
60 BigInteger p = new BigInteger (in.readUTF());
61 System.out.println("P: "+p);
62 BigInteger q = new BigInteger (in.readUTF());
63 System.out.println("Q: "+q);
64 BigInteger Xb = new BigInteger (p.bitCount()-1, new Random());//rand.nextInt(p-1)+1;
65 System.out.println("Xb: "+Xb);
66
67 BigInteger Yb = q.modPow(Xb, p);
68 System.out.println("Yb: "+Yb);
69 dOut.writeUTF(Yb.toString());
70
71 BigInteger Ya = new BigInteger (in.readUTF());
72 System.out.println("Ya: "+Ya);
73
74
75 BigInteger S_B =Ya.modPow(Xb, p);
76
77 System.out.println("S: "+S_B);
78
79 key = S_B.toString();
80
81
82 int length = in.readInt();
83 if (length > 0) {
84 byte[] message = new byte[length];
85 in.readFully(message, 0, message.length);
86 try {
87 byte[] b2 = AES.decrypt(message, key);
88 out.write(b2, 0, b2.length);
89 } catch (Exception e) {
90 e.printStackTrace();
91 }
92 }
93
94
95 out.close();
96 in.close();
97 socket.close();
98 serverSocket.close();
99 }
100}
101
102class AES {
103
104 private static SecretKeySpec secretKey;
105 private static byte[] key;
106
107 private static void setKey(String myKey)
108 {
109 MessageDigest sha = null;
110 try {
111 key = myKey.getBytes("UTF-8");
112 sha = MessageDigest.getInstance("SHA-1");
113 key = sha.digest(key);
114 key = Arrays.copyOf(key, 16);
115 secretKey = new SecretKeySpec(key, "AES");
116 }
117 catch (NoSuchAlgorithmException e) {
118 e.printStackTrace();
119 }
120 catch (UnsupportedEncodingException e) {
121 e.printStackTrace();
122 }
123 }
124
125 public static byte[] encrypt(byte[] Data, String secret) throws Exception {
126 setKey(secret);
127 Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
128 c.init(Cipher.ENCRYPT_MODE, secretKey);
129 byte[] encVal = c.doFinal(Data);
130 return encVal;
131 }
132
133 public static byte[] decrypt(byte[] encryptedData, String secret) throws Exception {
134 setKey(secret);
135 Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
136 c.init(Cipher.DECRYPT_MODE, secretKey);
137
138 byte[] decValue = c.doFinal(encryptedData);
139 return decValue;
140 }
141
142
143
144
145
146
147}