package com.jingewenku.abrahamcaijin.commonutil.encryption; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.security.NoSuchAlgorithmException; /** * @Description:主要功能:AES对称加密(Advanced Encryption Standard,高级数据加密标准,AES算法可以有效抵制针对DES的攻击算法,对称加密算法) * @Prject: CommonUtilLibrary * @Package: com.jingewenku.abrahamcaijin.commonutil.encryption * @author: AbrahamCaiJin * @date: 2017年05月16日 15:54 * @Copyright: 个人版权所有 * @Company: * @version: 1.0.0 */ public class AESUtils { private AESUtils() { throw new UnsupportedOperationException("cannot be instantiated"); } /* * 生成密钥 */ public static byte[] initKey() throws NoSuchAlgorithmException { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(256); //192 256 SecretKey secretKey = keyGen.generateKey(); return secretKey.getEncoded(); } /* * AES 加密 */ public static byte[] encrypt(byte[] data, byte[] key) throws Exception { SecretKey secretKey = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] cipherBytes = cipher.doFinal(data); return cipherBytes; } /* * AES 解密 */ public static byte[] decrypt(byte[] data, byte[] key) throws Exception { SecretKey secretKey = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] plainBytes = cipher.doFinal(data); return plainBytes; } }