import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.UUID;
public class AesUtils {
private static final String PASSWORD = "0123456789123456";
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
public static String encrypt(String message) throws Exception {
if (message == null ){
return null;
}
SecretKeySpec secretKey = new SecretKeySpec(PASSWORD.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
byte[] byteContent = message.getBytes(StandardCharsets.UTF_8);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(byteContent);
return Base64.getEncoder().encodeToString(bytes);
}
public static String decrypt(String encryptMessage)throws Exception {
if (encryptMessage == null){
return null;
}
byte[] decodeArray = Base64.getDecoder().decode(encryptMessage);
SecretKeySpec secretKey = new SecretKeySpec(PASSWORD.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] bytes = cipher.doFinal(decodeArray);
return new String(bytes, StandardCharsets.UTF_8);
}
public static void main(String[] args) throws Exception {
boolean b = true;
for (int i = 0; i < 50 ; i++) {
String uuid = UUID.randomUUID().toString();
System.out.println("uuid=====>" + uuid);
String encrypt = encrypt(uuid);
System.out.println("encrypt=====>" + encrypt);
String decrypt = decrypt(encrypt);
System.out.println("decrypt=====>" + decrypt);
b &= decrypt.equals(uuid);
}
System.out.println("final =====>" + b);
}
}

- 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