| | |
| | | package com.fzzy.api.utils; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.KeyGenerator; |
| | | import javax.crypto.SecretKey; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | |
| | |
| | | * 加解密方式 |
| | | */ |
| | | private final static String ALGORITHM = "AES"; |
| | | private final static String ALGORITHM_ECB = "ECB"; |
| | | private final static int KEY_SIZE_128 = 128; |
| | | |
| | | /** |
| | | *加密模式及填充方式 |
| | |
| | | return new String(encryptData,ENCODING); |
| | | } |
| | | |
| | | /** |
| | | * AES加密-ECB方式 |
| | | * @param plainText |
| | | * @param key |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String encryptByEcb(String plainText, String key) throws Exception { |
| | | if (key == null) { |
| | | System.out.print("Key为空null"); |
| | | return null; |
| | | } |
| | | |
| | | // 判断Key是否为16位 |
| | | // if (key.length() != 16) { |
| | | // System.out.print("Key长度不是16位"); |
| | | // return null; |
| | | // } |
| | | SecretKey secretKey = new SecretKeySpec(key.getBytes(ENCODING), ALGORITHM); |
| | | // AES加密采用pkcs5padding填充 |
| | | Cipher cipher = Cipher.getInstance(PATTERN); |
| | | //用密匙初始化Cipher对象 |
| | | cipher.init(Cipher.ENCRYPT_MODE, secretKey); |
| | | //执行加密操作 |
| | | byte[] encryptData = cipher.doFinal(plainText.getBytes(ENCODING)); |
| | | return Base64.getEncoder().encodeToString(encryptData); |
| | | } |
| | | |
| | | |
| | | } |