需要引入bouncycastle库的jar包
package test; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.security.Security; import java.util.Base64; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; public class Aes256Util { private final static String PROVIDER_BC = "BC"; private final static String CHARSET = "UTF-8"; private final static String ALGORITHM = "AES/ECB/PKCS5Padding"; private final static String KEY_ALGORITHM = "AES"; /** * 引入BC库 */ static { try { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); } catch (Exception e) { e.printStackTrace(); } } /** * 初始化一个256位对称加密的秘钥 * @param keyStr 如果加密解密的方法直接用一个String.getByte()的话,那么这个key需要达到128/192/256 bits * @return */ public static byte[] getRandomKey(String keyStr){ KeyGenerator keyGenerator = null; SecureRandom secureRandom = null; try { keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM); secureRandom = SecureRandom.getInstance("SHA1PRNG"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } secureRandom.setSeed(keyStr.getBytes()); keyGenerator.init(256, secureRandom); return keyGenerator.generateKey().getEncoded(); } /** * 加密 * @param content 原明文串 * @param key 对称加密秘钥 * @return 最后对byte进行了base64encode */ public static String encrypt(String content, byte[] key){ String encryptStr = ""; try { //密码器初始化 Cipher cipher = Cipher.getInstance(ALGORITHM, PROVIDER_BC); SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encryptByte = cipher.doFinal(content.getBytes(CHARSET)); encryptStr = Base64.getEncoder().encodeToString(encryptByte); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return encryptStr; } /** * 解密 * @param contentByte 加密后的密文byte * @param key * @return */ public static String decrypt(byte[] contentByte, byte[] key){ String decryptStr = ""; //密码器初始化 try { Cipher cipher = Cipher.getInstance(ALGORITHM, PROVIDER_BC); SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] encryptByte = cipher.doFinal(contentByte); decryptStr = new String(encryptByte, CHARSET); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return decryptStr; } public static void main(String[] args) { String keyStr = "1111111111111111111111111111111"; byte[] rkey = getRandomKey(keyStr); String a = encrypt("test", rkey); String b = decrypt(Base64.getDecoder().decode(a), rkey); System.out.println(a); System.out.println(b); } }