forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAES.java
More file actions
97 lines (87 loc) · 3.15 KB
/
Copy pathAES.java
File metadata and controls
97 lines (87 loc) · 3.15 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
package DNA.Cryptography;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidParameterSpecException;
import java.util.Arrays;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.security.auth.DestroyFailedException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class AES {
private static final String KEY_ALGORITHM = "AES";
private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding";
static {
Security.addProvider(new BouncyCastleProvider());
}
public static byte[] decrypt(byte[] encryptedData, byte[] key, byte[] iv) throws IllegalBlockSizeException, BadPaddingException {
if (key.length != 32 || iv.length != 16) {
throw new IllegalArgumentException();
}
try {
SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
AlgorithmParameters params = AlgorithmParameters.getInstance(KEY_ALGORITHM);
params.init(new IvParameterSpec(iv));
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.DECRYPT_MODE, secretKey, params);
return cipher.doFinal(encryptedData);
} catch (NoSuchAlgorithmException | InvalidParameterSpecException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchProviderException ex) {
throw new RuntimeException(ex);
}
}
public static byte[] encrypt(byte[] data, byte[] key, byte[] iv) {
if (key.length != 32 || iv.length != 16) {
throw new IllegalArgumentException();
}
try {
SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
AlgorithmParameters params = AlgorithmParameters.getInstance(KEY_ALGORITHM);
params.init(new IvParameterSpec(iv));
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, params);
return cipher.doFinal(data);
} catch (NoSuchAlgorithmException | InvalidParameterSpecException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | NoSuchProviderException ex) {
throw new RuntimeException(ex);
}
}
public static byte[] generateIV() {
byte[] iv = new byte[16];
SecureRandom rng = new SecureRandom();
rng.nextBytes(iv);
return iv;
}
public static byte[] generateKey() {
SecretKey key = null;
try {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
keyGenerator.init(256);
key = keyGenerator.generateKey();
return key.getEncoded();
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
} finally {
if (key != null) {
try {
key.destroy();
} catch (DestroyFailedException ex) {
}
}
}
}
public static byte[] generateKey(String password) {
byte[] passwordBytes = null, passwordHash = null;
try {
passwordBytes = password.getBytes("UTF-8");
passwordHash = Digest.sha256(passwordBytes);
return Digest.sha256(passwordHash);
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
} finally {
if (passwordBytes != null) {
Arrays.fill(passwordBytes, (byte)0);
}
if (passwordHash != null) {
Arrays.fill(passwordHash, (byte)0);
}
}
}
}