AES算法加密、解密

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();
      }
   }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值