-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSender.java
More file actions
129 lines (85 loc) · 4.09 KB
/
Sender.java
File metadata and controls
129 lines (85 loc) · 4.09 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
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.X509EncodedKeySpec;
public class Sender {
// Have the .txt file be encrypted using AES before being sent
//AES Key for the txt file is itself encrypted with nathan's public key
//What is sent is encrypted AES key and Encrypted txt
// Append MAC to data transmitted
// Creates and returns AES secret key
public static SecretKey createAES() throws NoSuchAlgorithmException {
KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(128);
return generator.generateKey();
}
// Encrypts message using AES CBC
public static EncryptedMessage encryptMessage(File fileName, SecretKey aesKey) throws Exception{
byte[] message = Files.readAllBytes(fileName.toPath());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] iv = new byte[16];
SecureRandom.getInstanceStrong().nextBytes(iv);
IvParameterSpec params = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, aesKey, params);
// Message is encrypted and returned along with iv value
byte[] encryptedM = cipher.doFinal(message);
return new EncryptedMessage(encryptedM, iv);
}
// Encrypts AES key using receivers public key using RSA ECB
public static byte[] encryptAES(SecretKey aesKey, PublicKey pubKey) throws Exception{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return cipher.doFinal(aesKey.getEncoded());
}
// Creates the MAC using HMAC with SHA 256
public static byte[] appendMAC(byte[] encryptedMessage, byte[] encryptedAES, byte[] MACKey) throws Exception{
SecretKey macKey = new SecretKeySpec(MACKey, "HmacSHA256");
Mac senderMAC = Mac.getInstance("HmacSHA256");
senderMAC.init(macKey);
senderMAC.update(encryptedMessage);
senderMAC.update(encryptedAES);
return senderMAC.doFinal();
}
//Transmits the data
public static void transmitData(byte[] data, DataOutputStream writeOut) throws Exception{
writeOut.writeInt(data.length);
writeOut.write(data);
}
public static void main(String[] args) throws Exception {
PublicKey receiverPubKey;
SecretKey senderSecretKey;
EncryptedMessage encryptedMessage;
byte[] encryptedAES;
byte[] mac;
//Loads the receiver public key and message
File pubKey = new File("receiver_public.key");
File message = new File("message.txt");
byte[] keyBytes = Files.readAllBytes(pubKey.toPath());
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
receiverPubKey = KeyFactory.getInstance("RSA").generatePublic(spec);
//Makes the AES sender secret key
senderSecretKey = createAES();
// Encrypts the message using the AES key and encrypts the AES key with the receivers public key
// Then creates the MAC for the encrypted message and the encrypted AES key
encryptedMessage = encryptMessage(message, senderSecretKey);
encryptedAES = encryptAES(senderSecretKey, receiverPubKey);
mac = appendMAC(encryptedMessage.encryptMessage, encryptedAES, senderSecretKey.getEncoded());
DataOutputStream writeOut = new DataOutputStream(new FileOutputStream("Transmitted_Data.txt"));
transmitData(encryptedAES, writeOut);
transmitData(encryptedMessage.encryptMessage,writeOut);
transmitData(encryptedMessage.encryptIV,writeOut);
transmitData(mac,writeOut);
System.out.println("Data has been encrypted and transmitted");
}
}