AES base64

sample code

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
/**
https://aesencryption.net/
https://www.clickssl.net/blog/128-bit-ssl-encryption-vs-256-bit-ssl-encryption
https://www.quora.com/Is-AES256-more-secure-than-AES128-Whats-the-different
https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
https://stackoverflow.com/questions/273396/aes-encryption-what-are-public-and-private-keys
*/

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.nio.charset.StandardCharsets;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

public class AESEncryptionUtil {

// http://www.allkeysgenerator.com/Random/Security-Encryption-Key-Generator.aspx
private static String key = “customized key”;
public static BASE64Encoder enc = new BASE64Encoder();
public static BASE64Decoder dec = new BASE64Decoder();


// eccrypt by AES, and encode by base64 to transfer by http
public static String encrypt(String clearText) throws Exception {
String encodedEncryptText = “”;
try {
Key aesKey = new SecretKeySpec(key.getBytes(), “AES”);
Cipher cipher = Cipher.getInstance(“AES”);

cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(clearText.getBytes(“UTF-8”));

encodedEncryptText = enc.encode(encrypted);

} catch (NoSuchAlgorithmException e) {
throw new Exception(e.getMessage());
} catch (NoSuchPaddingException e) {
throw new Exception(e.getMessage());
} catch (BadPaddingException e) {
throw new Exception(e.getMessage());
} catch (UnsupportedEncodingException e) {
throw new Exception(e.getMessage());
} catch (IllegalBlockSizeException e) {
throw new Exception(e.getMessage());
} catch (InvalidKeyException e) {
throw new Exception(e.getMessage());
}
return encodedEncryptText;

}

// encode by base64, decrypt by AES
public static String decrypt(String encodedEncryptText) throws Exception {
String decrypted = “”;
try {
Key aesKey = new SecretKeySpec(key.getBytes(), “AES”);

Cipher cipher = Cipher.getInstance(“AES”);
cipher.init(Cipher.DECRYPT_MODE, aesKey);

byte[] decodeBufferArray = dec.decodeBuffer(encodedEncryptText);

decrypted = new String(cipher.doFinal(decodeBufferArray), StandardCharsets.UTF_8);

} catch (NoSuchAlgorithmException e) {
throw new Exception(e.getMessage());
} catch (NoSuchPaddingException e) {
throw new Exception(e.getMessage());
} catch (BadPaddingException e) {
throw new Exception(e.getMessage());
} catch (IllegalBlockSizeException e) {
throw new Exception(e.getMessage());
} catch (InvalidKeyException e) {
throw new Exception(e.getMessage());
} catch (IOException e) {
throw new Exception(e.getMessage());
}
return decrypted;

}

public static void main(String[] args) {
String a = null;
try {
a = encrypt(“mengxu1128@gmail.com”);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(a);

String b = null;
try {
b = decrypt(a);

} catch (Exception e) {
e.printStackTrace();
}
System.out.println(b);
}
}