-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlice.java
More file actions
267 lines (217 loc) · 6.75 KB
/
Alice.java
File metadata and controls
267 lines (217 loc) · 6.75 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Sender Class for Homework 5 Project
// Robert Bell Spring 2020
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
import java.util.Scanner;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SecureRandom;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.Mac;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class Alice {
// Keygen
private KeyGenerator kGen;
private KeyPairGenerator pairGen;
private SecretKey symKey;
private KeyPair pair;
// HashTest Message
private byte[] hashtest;
private byte[] hashTestMessage;
private int attempts = 0;
// Construct
public Alice() {
// Init kGen with SHA-256
try {
kGen = KeyGenerator.getInstance("HMACSHA256");
} catch (Exception e) {
System.out.println("Exception at Keygen init: " + e);
}
// Generate Symmetric Key
symKey = kGen.generateKey();
// Init pairGen for RSA-2048
try {
pairGen = KeyPairGenerator.getInstance("RSA");
pairGen.initialize(2048, new SecureRandom());
} catch (Exception e) {
System.out.println("Exception at Keypair init: " + e);
}
// Generate key pair
pair = pairGen.generateKeyPair();
}
// ----------------------------------- HMAC --------------------------------------
// Exports The Symmetric Key to File
public void ExportSymKey(String filename) {
FileOutputStream out = null;
// Write To File Safely
try {
out = new FileOutputStream(filename);
out.write(symKey.getEncoded());
out.close();
} catch (Exception e) {
System.out.println("Exception at SymKey export: " + e);
}
}
// Sends MSG+HMAC Code to file
public void SendHMACMessage(String filename, String message) {
byte[] MACText = null;
// Generate Code
try {
Mac mac = Mac.getInstance("HMACSHA256");
mac.init(symKey);
MACText = mac.doFinal(message.getBytes());
} catch (Exception e) {
System.out.println("Exception at HMAC generation: " + e);
}
// Export
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
out.write(MACText);
out.write(message.getBytes());
out.close();
} catch (Exception e) {
System.out.println("Exception at HMAC export: " + e);
}
}
// ---------------------- Collision Testing ---------------------------------------
// Builds an HMAC message for testing
public byte[] GenerateHash(int length) {
hashTestMessage = GenerateByteString(128);
byte[] MACText = null;
byte[] firstbytes = new byte[length];
// Generate Code
try {
Mac mac = Mac.getInstance("HMACSHA256");
mac.init(symKey);
MACText = mac.doFinal(hashTestMessage);
} catch (Exception e) {
System.out.println("Exception at HMAC generation: " + e);
}
// Get first 8 bits
for (int i = 0; i < length; i++) {
firstbytes[i] = MACText[i];
}
// Save first8 var for test
return firstbytes;
}
// Generates random bytestring of length
private byte[] GenerateByteString(int length) {
SecureRandom random = new SecureRandom();
byte[] end = new byte[length];
random.nextBytes(end);
return end;
}
// Generate Hash for testing
public void generateHashTest(int length) {
hashtest = GenerateHash(8);
}
// Get hashtest
public String getHashTestLiteral() {
return new String(hashtest);
}
// Get hashTestMessage
public String getHashTestMessage() {
return new String(hashTestMessage);
}
// Get attempts
public int getAttempts() {
return attempts;
}
// Finds a hash that collides with Alice's testHash
public String FindCollision() {
// Store generated hashes
attempts = 0;
byte[] smashHash = null;
boolean badHash;
// Iterate until collision is found
do {
// Keep iterating until true is triggered
badHash = false;
// Get new hash for smashHash
smashHash = GenerateHash(8);
// Check Hash Matching
for (int i = 0; i < hashtest.length; i++) {
if (Byte.compare(smashHash[i], hashtest[i]) == 0) {
badHash = true;
}
}
attempts++;
} while (!(badHash));
return new String(smashHash);
}
//-------------------------------- RSA --------------------------------------------------
// Export public key to file
public void ExportPublicKey(String filename) {
FileOutputStream out = null;
PublicKey pubComp = pair.getPublic();
byte[] pubBytes = pubComp.getEncoded();
// Write To File Safely
try {
out = new FileOutputStream(filename);
out.write(pubBytes);
out.close();
} catch (Exception e) {
System.out.println("Exception at AsymKey export: " + e);
}
}
// Generate & Export sig message
public void SendSIGMessage(String filename, String message) {
byte[] SIGText = null;
// Generate Code
try {
Signature privSig = Signature.getInstance("SHA256withRSA");
privSig.initSign(pair.getPrivate());
privSig.update(message.getBytes());
SIGText = privSig.sign();
} catch (Exception e) {
System.out.println("Exception at SIG generation: " + e);
}
// Export
FileOutputStream out = null;
try {
out = new FileOutputStream(filename);
out.write(message.getBytes());
out.write(SIGText);
out.close();
} catch (Exception e) {
System.out.println("Exception at SIG export: " + e);
}
}
// ----------------------------------------------- Part 1+2 Main -----------------------------------------
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
// Call init of Alice
Alice alice = new Alice();
// Store private key
System.out.println("Exporting the private key to Bob...");
alice.ExportSymKey("key.txt");
// Generate & send HMAC
System.out.println("Input HMAC message...");
String message = input.nextLine();
System.out.println("Sending signature text To Bob...");
alice.SendHMACMessage("mactext.txt", message);
// Send PubKey
System.out.println("Exporting my public key to Bob...");
alice.ExportPublicKey("APubKey.txt");
// Generate & send Sig
System.out.println("Input Signature message...");
message = input.nextLine();
System.out.println("Sending signature text To Bob...");
alice.SendSIGMessage("sigtext.txt", message);
System.out.println("Done!");
}
}