-
Notifications
You must be signed in to change notification settings - Fork 5
/
AesSecretKeyProducer.java
47 lines (42 loc) · 1.35 KB
/
AesSecretKeyProducer.java
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
package org.thoth.crypto.symmetric;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author Michael Remijan mjremijan@yahoo.com @mjremijan
*/
public class AesSecretKeyProducer {
/**
* Generates a new AES-256 bit {@code SecretKey}.
*
* @return {@code SecretKey}, never null
* @throws RuntimeException All exceptions are caught and re-thrown as {@code RuntimeException}
*/
public SecretKey produce() {
KeyGenerator keyGen;
try {
keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey secretKey = keyGen.generateKey();
return secretKey;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* Generates an AES-256 bit {@code SecretKey}.
*
* @param encodedByteArray The bytes this method will use to regenerate a previously created {@code SecretKey}
*
* @return {@code SecretKey}, never null
* @throws RuntimeException All exceptions are caught and re-thrown as {@code RuntimeException}
*/
public SecretKey produce(byte [] encodedByteArray) {
try {
return new SecretKeySpec(encodedByteArray, "AES");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}