Java实现AES算法的实例代码
时间:2023-02-12 10:53:52|栏目:JAVA代码|点击: 次
使用AES算法可用于对数据进行加密码与解密,使用的时候需要注意两点:1)被加密的串越长,加密后的字符串越长,注意数据库字段的设计;2)Linux与Windows环境中可能会出现由于环境差异导致在Windows中测试成功,到Linux上后加密的串无法被正确解密。下列算法已在真实环境中进行实测,应用时也务必做好二次验证避免出现线上事故。
private static final String ALGORITHM_NAME = "AES"; //加密因子,可根据您的需要自定义 private static final String DEFAULT_ENCRYPT_RULE = "AES/CBC/PKCS5Padding"; private static final String RANDOM_KEY_ALGORITHM = "SHA1PRNG"; private static final String RANDOM_KEY_ALGORITHM_PROVIDER = "SUN"; /** * AES加密 * @param content 待加密的内容,为空时为回空 * @return 加密后的base64格式的结果,出现异常时返回null */ public static String encrypt(String content) { if (StringUtils.isEmpty(content)) { return null; } try { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_NAME); SecureRandom secureRandom = SecureRandom.getInstance(RANDOM_KEY_ALGORITHM, RANDOM_KEY_ALGORITHM_PROVIDER); secureRandom.setSeed(DEFAULT_ENCRYPT_RULE.getBytes()); keyGenerator.init(128, secureRandom); SecretKey originalKey = keyGenerator.generateKey(); SecretKey secretKey = new SecretKeySpec(originalKey.getEncoded(), ALGORITHM_NAME); Cipher cipher = Cipher.getInstance(ALGORITHM_NAME); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encrypted = cipher.doFinal(content.getBytes("utf-8")); String result = new String(Base64.getEncoder().encodeToString(encrypted)); return result; } catch (Exception e) { logger.error(e.getMessage(), e); } * 解密 * @param encrypted 加密后的base64格式的密文 * @return 解密后的原文,出现异常时返回null public static String decrypt(String encrypted) { if (StringUtils.isEmpty(encrypted)) { cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encrypted)); return new String(decrypted, "utf-8");
补充:下面看下Java使用AES算法的实例代码。
Java中使用AES(CBC,128位)算法加解密。一般加密后都是用一定编码格式进行传输,此处使用Base64算法进行编解码。实现及测试代码如下:
AESUtil.java
package gj.secure; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.Key; import java.security.spec.AlgorithmParameterSpec; import java.util.Base64; public class AESUtil { private static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding"; private static final String KEY_ALGORITHM = "AES"; public static byte[] initKey() throws Exception { KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM); kg.init(128); SecretKey secretKey = kg.generateKey(); return secretKey.getEncoded(); } public static byte[] encrypt(byte[] data, byte[] key, byte[] iv) throws Exception { Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC); Key k = new SecretKeySpec(key, KEY_ALGORITHM); AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, k, paramSpec); return cipher.doFinal(data); public static byte[] decrypt(byte[] bytes, byte[] key, byte[] iv) throws Exception { cipher.init(Cipher.DECRYPT_MODE, k, paramSpec); return cipher.doFinal(bytes); public static String encodeToBase64String(String data, byte[] key, byte[] iv) throws Exception { return Base64.getEncoder().encodeToString(encrypt(data.getBytes(), key, iv)); public static String decodeFromBase64String(String data, byte[] key, byte[] iv) throws Exception { byte[] bytes = Base64.getDecoder().decode(data); return new String(decrypt(bytes, key, iv)); }
测试代码:
AESUtilTest.java
package gj.secure; /** * created by gj on 2019-12-24 17:17 **/ public class AESUtilTest { public static void main(String[] args) throws Exception { byte[] key = AESUtil.initKey(); byte[] iv = {0x01, 0x23, 0x45, 0x67, 0x89 - 0xFF, 0xAB - 0xFF, 0xCD - 0xFF, 0xEF - 0xFF, 0x01, 0x23, 0x45, 0x67, 0x89 - 0xFF, 0xAB - 0xFF, 0xCD - 0xFF, 0xEF - 0xFF}; String content = "areful1997"; String cipher = AESUtil.encodeToBase64String(content, key, iv); System.out.println(cipher); String plain = AESUtil.decodeFromBase64String(cipher, key, iv); System.out.println(plain); } }
上一篇:MyBatis查询数据,赋值给List集合时,数据缺少的问题及解决
栏 目:JAVA代码
下一篇:没有了
本文标题:Java实现AES算法的实例代码
本文地址:http://www.codeinn.net/misctech/225643.html