-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
112 lines (109 loc) · 5.25 KB
/
Client.java
File metadata and controls
112 lines (109 loc) · 5.25 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
package security_project;
import java.io.*;
import java.util.*;
import java.math.*;
import java.security.*;https://github.com/jolycode/PGP/blob/main/Client.java
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.crypto.*;
import java.util.zip.GZIPOutputStream;
public class Client {
private static DataOutputStream dataOutputStream = null;
private static DataInputStream dataInputStream = null;
static Cipher encryptCipher, decryptCipher;
int keySize = 2048;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String ip="127.0.0.1";int port=990;
System.out.println("Enter the name of the file to be sent");
String fileName = sc.nextLine();
String path="C:/Users/danil/CSE_ALL/439sec/"+fileName;
try(Socket socket = new Socket(ip,port)) {
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
PGP(path); //path
dataInputStream.close();
System.out.println("File is sent");
}catch (Exception e){
e.printStackTrace();
}
}
public static void PGP(String path) throws Exception {
String input = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
//Generating server keys
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPairGenerator keyPairGenerator2 = KeyPairGenerator.getInstance("RSA");
keyPairGenerator2.initialize(2048);//Generating client keys
KeyPair keyPair1=keyPairGenerator.genKeyPair();
KeyPair keyPair2=keyPairGenerator.genKeyPair();
PrivateKey senderPrivateKey = keyPair1.getPrivate();//
PublicKey receiverPubKey = keyPair2.getPublic();// get server public key
try (FileOutputStream out = new FileOutputStream("private.key")) {
out.write(keyPair2.getPrivate().getEncoded());
}
try (FileOutputStream out = new FileOutputStream("public.pub")) {
out.write(keyPair1.getPublic().getEncoded() );
}
String checksum;
//Generating SHA-512 hash of original message
MessageDigest digest=MessageDigest.getInstance("SHA-512");
digest.reset();
digest.update(input.getBytes("utf8"));
checksum = String.format("%040x", new BigInteger(1, digest.digest()));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE,senderPrivateKey);
byte[] utf8 =cipher.doFinal(checksum.getBytes("UTF-8"));
//Encrypt the message hash with sender private keys -> Digital Signature
String encryptedPrivateHash= Base64.getEncoder().encodeToString(utf8);
//Append original message and encrypted hash
String beforeZip[]={input,encryptedPrivateHash};
String afterZip[]=new String[beforeZip.length];
for(int i=0;i<beforeZip.length;i++){
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(beforeZip[i].length());
GZIPOutputStream gZip=new GZIPOutputStream(byteArrayOutputStream);
gZip.write(beforeZip[i].getBytes());
gZip.close();
byte[] compressed=byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
afterZip[i]=Base64.getEncoder().encodeToString(compressed);
}
//Encrypt zipstring with DES
SecretKey key=KeyGenerator.getInstance("DES").generateKey();
String afterZipDES[]=new String[afterZip.length+1];
for(int i=0;i<afterZip.length;i++){
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
byte[] utf8str =afterZip[i].getBytes("UTF8");
byte[] encrypted = encryptCipher.doFinal(utf8str);
afterZipDES[i]=Base64.getEncoder().encodeToString(encrypted);
}
//Encrypt DES key with Receiver Public Key using RSA
String encodedKey=Base64.getEncoder().encodeToString(key.getEncoded());
Cipher cipher2 = Cipher.getInstance("RSA");
cipher2.init(Cipher.ENCRYPT_MODE, receiverPubKey);
byte[] utf8new2 = cipher2.doFinal(encodedKey.getBytes("UTF-8"));
//SecretKey is base64 encoded
String encryptedKey=Base64.getEncoder().encodeToString(utf8new2);
//Decrypting DES key with Receiver Private Key using RSA
afterZipDES[2]=encryptedKey;
String messageToServer[]=afterZipDES;
sendFile(messageToServer);
}
private static void sendFile(String array[]) throws Exception{
for(int i = 0 ; i < 3 ; i++){
System.out.println(array[i]);//messageToServer --> afterZipDES
}
byte[] data=array[0].getBytes("UTF-8");
dataOutputStream.writeInt(data.length);
dataOutputStream.write(data);
byte[] hash=array[1].getBytes("UTF-8");
dataOutputStream.writeInt(hash.length);
dataOutputStream.write(hash);// send hash
byte[] key=array[2].getBytes("UTF-8");
dataOutputStream.writeInt(key.length);
dataOutputStream.write(key);
}
}