-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoUtilities.java
More file actions
345 lines (268 loc) · 9.09 KB
/
CryptoUtilities.java
File metadata and controls
345 lines (268 loc) · 9.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import java.io.*;
import java.util.Arrays;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
* This class contains various utility functions for working with AES encryption
* and decryption, and for working with HMAC-SHA1 message digests.
*
* @author Mike Jacobson
* @version 1.0, October 23, 2013
*/
public class CryptoUtilities {
// AES key length to be used (in bytes)
public static final int AES_KEY_LEN = 16;
// HMAC-SHA1 digest length (in bytes)
public static final int HMAC_SHA1_LEN = 20;
// AES/CBC/PKCS5Padding parameter length
public static final int AES_PARAM_LEN = 18;
// Bit length of prime modulus for Diffie-Hellman
// public static final int DH_PRIME_SIZE = 3072;
public static final int DH_PRIME_SIZE = 1024;
// Certainty value for probably primes
public static final int CERTAINTY = 3;
/**
* Constructs a AES_KEY_LEN byte AES key from a given seed
*
* @param seed (array of bytes)
* @return the resulting AES key
*/
public static SecretKeySpec key_from_seed(byte[] seed) {
// compute SHA-1 hash of the seed
byte[] hashval = null;
try {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
hashval = sha1.digest(seed);
}
catch (Exception e) {
e.printStackTrace();
}
// extract 1st AES_KEY_LEN bytes for the key material
byte[] key = new byte[AES_KEY_LEN];
System.arraycopy(hashval, 0, key, 0, AES_KEY_LEN);
// initialize the key
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
return keySpec;
}
/**
* Computes a HMAC-SHA1 message digest of a given message, appends it to the
* message, returns the output.
*
* @param message the message (in bytes)
* @param keySpec the secret key for HMAC-SHA1
* @return message concatenated with the HMAC-SHA1 digest
*/
public static byte[] append_hash(byte[] message, SecretKeySpec keySpec)
{
byte[] ret = null;
try {
// Initialize the MAC with the given key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(keySpec);
// Compute the MAC
byte[] m = mac.doFinal(message);
// Append the MAC to the message
ret = new byte[message.length+m.length];
System.arraycopy(message, 0, ret, 0, message.length);
System.arraycopy(m, 0, ret, message.length, m.length);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/**
* Extracts a message from a message/digest pair.
*
* @param hash_message the message and digest (in bytes)
* @return message concatenated with the HMAC-SHA1 digest
*/
public static byte[] extract_message(byte[] hash_message)
{
byte[] plaintext = new byte[hash_message.length - HMAC_SHA1_LEN];
System.arraycopy(hash_message, 0, plaintext, 0, plaintext.length);
return plaintext;
}
/**
* Extracts a HMAC-SHA1 message digest form the end of the given message and
* deterimines whether it is valid.
*
* @param messageHash the message including digest (in bytes)
* @param keySpec the secret key for HMAC-SHA1
* @return true if the extracted digest matches the computed digest
*/
public static boolean verify_hash(byte[] messageHash, SecretKeySpec keySpec)
{
boolean ret = false;
try {
// Split the array into the message and the digest
byte[] message = new byte[messageHash.length - HMAC_SHA1_LEN];
byte[] hash = new byte[HMAC_SHA1_LEN];
System.arraycopy(messageHash, 0, message, 0, message.length);
System.arraycopy(messageHash, message.length, hash, 0, hash.length);
// Initialize the MAC with the given key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(keySpec);
// Get the MAC of the message
byte[] m = mac.doFinal(message);
// compare the the MAC sent and the one calculated
ret = Arrays.equals(m, hash);
} catch (Exception e) {
// if there is an error, we know that hash can't be correct
ret = false;
}
return ret;
}
/**
* Encrypts the given message using the given key with AES-CBC.
*
* @param message the message (in bytes)
* @param keySpec the secret key
* @return encrypted message (with algorithm parameters appended
*/
public static byte[] encrypt(byte[] message, SecretKeySpec keySpec)
{
byte[] ret = null;
try {
// Initialize the cipher with the given key
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
// encrypt the message
byte[] cipherText = cipher.doFinal(message);
byte[] params = cipher.getParameters().getEncoded();
// Combine the ciphertext and cipher parameters into one byte array
ret = new byte[cipherText.length+params.length];
System.arraycopy(cipherText, 0, ret, 0, cipherText.length);
System.arraycopy(params, 0, ret, cipherText.length, params.length);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/**
* Decrypts the given message using the given key with AES-CBC.
*
* @param decrypt the message (in bytes)
* @param keySpec the secret key
* @return decrypted message
*/
public static byte[] decrypt(byte[] decrypt, SecretKeySpec keySpec)
{
byte[] message = null;
try {
// Extract the cipher parameters from the end of the input array
byte[] cipherText = new byte[decrypt.length - AES_PARAM_LEN];
byte[] paramsEnc = new byte[AES_PARAM_LEN];
System.arraycopy(decrypt, 0, cipherText, 0, cipherText.length);
System.arraycopy(decrypt, cipherText.length, paramsEnc, 0, paramsEnc.length);
// Initialize the parameters
AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
params.init(paramsEnc);
// Initialize the cipher for decryption
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec, params);
// Decrypt the ciphertext
message = cipher.doFinal(cipherText);
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
/**
* Writes the given message to the supplied stream.
*
* @param message the message (in bytes)
* @param out the output stream
* @throws IOException
*/
public static void send(byte[] message, DataOutputStream out) throws IOException
{
// send the length of the encrypted message
out.writeInt(message.length);
// send the encrypted message bytes
out.write(message);
// flush the stream
out.flush();
}
/**
* Decrypts the given message using the given key with AES-CBC.
*
* @param keySpec the secret key
* @param in the input stream
* @throws IOException
* @return received message
*/
public static byte[] receive(DataInputStream in) throws IOException
{
byte[] message = null;
// read the message size
int size = in.readInt();
// read the message bytes into the array
message = new byte[size];
int i = 0;
int total = 0;
do {
i = in.read(message, total, size-total);
total += i;
} while (total != size);
return message;
}
/**
* Encrypts the given message using the given key with AES-CBC, and writes to
* the supplied stream.
*
* @param message the message (in bytes)
* @param keySpec the secret key
* @param out the output stream
* @throws IOException
*/
public static void encryptAndSend(byte[] message, SecretKeySpec keySpec, DataOutputStream out) throws IOException
{
// encrypt the message
byte[] ciphertext = encrypt(message,keySpec);
// send the message
send(ciphertext,out);
}
/**
* Decrypts the given message using the given key with AES-CBC.
*
* @param keySpec the secret key
* @param in the input stream
* @throws IOException
* @return decrypted message
*/
public static byte[] receiveAndDecrypt(SecretKeySpec keySpec, DataInputStream in) throws IOException
{
// get the message
byte[] message = receive(in);
// decrypt the received message and return the result
return decrypt(message,keySpec);
}
/**
* Converts a byte array to hex string
* This code from http://java.sun.com/j2se/1.4.2/docs/guide/security/jce/JCERefGuide.html#HmacEx
*/
public static String toHexString(byte[] block) {
StringBuffer buf = new StringBuffer();
int len = block.length;
for (int i = 0; i < len; i++) {
byte2hex(block[i], buf);
if (i < len-1) {
buf.append(":");
}
}
return buf.toString();
}
/**
* Converts a byte to hex digit and writes to the supplied buffer
* This code from http://java.sun.com/j2se/1.4.2/docs/guide/security/jce/JCERefGuide.html#HmacEx
*/
public static void byte2hex(byte b, StringBuffer buf) {
char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
int high = ((b & 0xf0) >> 4);
int low = (b & 0x0f);
buf.append(hexChars[high]);
buf.append(hexChars[low]);
}
}