使用AES128加密字符串

  1 import org.apache.commons.codec.binary.Base64;
  2 import org.apache.commons.lang3.StringUtils;
  3 
  4 import javax.crypto.Cipher;
  5 import javax.crypto.KeyGenerator;
  6 import javax.crypto.SecretKey;
  7 import javax.crypto.spec.SecretKeySpec;
  8 import java.security.SecureRandom;
  9 import java.util.logging.Level;
 10 import java.util.logging.Logger;
 11 
 12 /**
 13  * @author administrator.
 14  * @date 2018-10-11 18:55.
 15  * <p>
 16  * description: 字符串AES128加密 <加盐>
 17  */
 18 public class Aes128Util {
 19 
 20     private static final String KEY_ALGORITHM = "AES";
 21     /*** 字符编码 ***/
 22     private static final String CHARACTER_CODING = "UTF-8";
 23     /*** 默认的加密算法 ***/
 24     private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
 25     /*** 秘钥 ***/
 26     private static final String DEFAULT_SECRET_KEY = "[email protected]";
 27 
 28     /**
 29      * 使用默认盐加密
 30      *
 31      * @param content
 32      * @return 33      */
 34     public static String encrypt(String content) {
 35         return encrypt(content, DEFAULT_SECRET_KEY);
 36     }
 37 
 38     /**
 39      * 自定义盐加密
 40      *
 41      * @param content
 42      * @param key
 43      * @return
 44      */
 45     public static String encrypt(String content, String key) {
 46         if (StringUtils.isBlank(content)) {
 47             return content;
 48         }
 49         if (StringUtils.isBlank(key)) {
 50             key = DEFAULT_SECRET_KEY;
 51         }
 52         try {
 53             Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
 54             byte[] byteContent = content.getBytes(CHARACTER_CODING);
 55             cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));
 56             byte[] result = cipher.doFinal(byteContent);
 57             return Base64.encodeBase64String(result);
 58         } catch (Exception e) {
 59             String msg = "String: [" + content + "] Aes128Util encryption error";
 60             Logger.getLogger(Aes128Util.class.getName()).log(Level.SEVERE, msg, e);
 61         }
 62         return null;
 63     }
 64 
 65     /**
 66      * 使用默认盐解密
 67      *
 68      * @param content
 69      * @return
 70      */
 71     public static String decrypt(String content) {
 72         return decrypt(content, DEFAULT_SECRET_KEY);
 73     }
 74 
 75     /**
 76      * 使用自定义盐解密
 77      *
 78      * @param content
 79      * @param key
 80      * @return
 81      */
 82     public static String decrypt(String content, String key) {
 83         if (StringUtils.isBlank(content)) {
 84             return content;
 85         }
 86         if (StringUtils.isBlank(key)) {
 87             key = DEFAULT_SECRET_KEY;
 88         }
 89         try {
 90             Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
 91             cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
 92             byte[] result = cipher.doFinal(Base64.decodeBase64(content));
 93             return new String(result, CHARACTER_CODING);
 94         } catch (Exception e) {
 95             String msg = "String: [" + content + "] Aes128Util decryption error";
 96             Logger.getLogger(Aes128Util.class.getName()).log(Level.SEVERE, msg, e);
 97         }
 98         return null;
 99     }
100 
101     /**
102      * 生成加密秘钥
103 *
104 * @param key 105 * @return 106 */ 107 private static SecretKeySpec getSecretKey(final String key) throws Exception { 108 KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM); 109 kg.init(128, new SecureRandom(key.getBytes())); 110 SecretKey secretKey = kg.generateKey(); 111 return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM); 112 } 113 114 }

猜你喜欢

转载自www.cnblogs.com/yanwu0527/p/9774550.html