-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAESCrypto.java
70 lines (49 loc) · 1.79 KB
/
AESCrypto.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @(#)AESCrypto.java
*
*
* @author : Ghabx
* @version 1.00 2015/6/17
*/
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.Base64;
public class AESCrypto {
//Constructor-------------------------------
public AESCrypto(String sKey, byte[] ivKey) throws Exception {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] bTemp = sha256.digest(sKey.getBytes("UTF-8"));
bKey = new byte[16];
for(int i = 0; i < bKey.length; i++){
bKey[i] = bTemp[i];
}
bTemp = sha256.digest(bKey);
iv = ivKey;
}
//------------END OF CONSTRUCTOR------------
private byte[] bKey;
private byte[] iv;
//Public Methods-------------------------------
public String decrypt(String encData) throws Exception{
Key kKey = new SecretKeySpec(this.bKey, "AES");
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivspec = new IvParameterSpec(iv);
c.init(Cipher.DECRYPT_MODE, kKey, ivspec);
byte[] bEncData = Base64.getDecoder().decode(encData);
byte[] bDecrypted = c.doFinal(bEncData);
return new String(bDecrypted);
}
public String encrypt(String strData) throws Exception{
Key kKey = new SecretKeySpec(this.bKey, "AES");
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivspec = new IvParameterSpec(iv);
c.init(Cipher.ENCRYPT_MODE, kKey, ivspec);
byte[] enc = c.doFinal(strData.getBytes());
return Base64.getEncoder().encodeToString(enc);
}
//------------END OF PUBLIC METHODS------------
//Private Methods-------------------------------
//------------END OF PRIVATE METHODS------------
}