-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoAES.java
More file actions
77 lines (62 loc) · 1.72 KB
/
Copy pathCryptoAES.java
File metadata and controls
77 lines (62 loc) · 1.72 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
package atChat;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
public class CryptoAES extends Crypt {
private final Cipher encryptor;
private final Cipher decryptor;
protected final byte[] key;
protected CryptoAES() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
key = keyGen.generateKey().getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
encryptor = Cipher.getInstance("AES");
encryptor.init(Cipher.ENCRYPT_MODE, keySpec);
decryptor = Cipher.getInstance("AES");
decryptor.init(Cipher.DECRYPT_MODE, keySpec);
}
protected CryptoAES(byte[] key) throws Exception {
this.key = key;
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
encryptor = Cipher.getInstance("AES");
encryptor.init(Cipher.ENCRYPT_MODE, keySpec);
decryptor = Cipher.getInstance("AES");
decryptor.init(Cipher.DECRYPT_MODE, keySpec);
}
@Override
protected byte[] encrypt(final String text) {
try {
return encryptor.doFinal(text.getBytes());
} catch (Exception e) {
return null;
}
}
@Override
protected byte[] encrypt(final byte[] bytes) {
try {
return encryptor.doFinal(bytes);
} catch (Exception e) {
return null;
}
}
@Override
protected String decrypt(final byte[] bytes) {
try {
return new String(decryptor.doFinal(bytes));
} catch (Exception e) {
return null;
}
}
@Override
protected byte[] decryptBytes(final byte[] bytes) {
if (decryptor != null) {
try {
return decryptor.doFinal(bytes);
} catch (Exception e) {
return null;
}
}
return null;
}
}