import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;
/**
* @author: lcw
* @createTime: 2024/3/6
*/
public class PasswordValidatorUtils {
private static final String key = "xxxxxxxxxxxxxxxxx";
public static boolean validatePassword(String password) {
String regex = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,}$";
Pattern pattern = Pattern.compile(regex);
return pattern.matcher(password).matches();
}
// 使用AES算法加密
public static String encrypt(String data) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return java.util.Base64.getEncoder().encodeToString(encryptedData);
}
// 使用AES算法解密
public static String decrypt(String encryptedData) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] originalData = cipher.doFinal(java.util.Base64.getDecoder().decode(encryptedData));
return new String(originalData, StandardCharsets.UTF_8);
}
// public static void main(String[] args) {
// try {
// String encrypt = encrypt("123456@Ac..");
// System.out.println(encrypt);
// } catch (Exception e) {
// System.out.println("加密失败");
// e.printStackTrace();
// }
// }
public static void main(String[] args) {
try {
String encrypt = decrypt("JAs24zCPKV12KYP9eFnDig==");
System.out.println(encrypt);
} catch (Exception e) {
System.out.println("解密失败");
e.printStackTrace();
}
}
}
AES算法加密、解密
于 2024-03-06 20:26:11 首次发布