Cipher.getInstance("AES/ECB/PKCS5Padding");加解密算法
public class CipherUtils {
private static final String ALGORITHM = "AES";
private static final String ALGORITHM_STR = "AES/ECB/PKCS5Padding";
private SecretKeySpec key;
public CipherUtils(String hexKey) {
key = new SecretKeySpec(hexKey.getBytes(), ALGORITHM);
}
public String encryptData(String data) {
try {
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.ENCRYPT_MODE, key);
return new BASE64Encoder().encode(cipher.doFinal(data.getBytes("utf-8")));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String decryptData(String data) {
try {
Cipher cipher = Cipher.getInstance(ALGORITHM_STR);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(new BASE64Decoder().decodeBuffer(data)), "utf-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
- 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