import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class CipherEncrptTest { public static void main(String[] args) { //默认加密的salt key String key = "0123abcd0123efmn"; String text = "123456"; try { byte[] raw = key.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec(key.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] original = cipher.doFinal(text.getBytes()); System.out.println(new String(Base64.getEncoder().encode(original))); } catch (Exception e) { e.printStackTrace(); } } public static void decrpt() { try { String sKey = "0123abcd0123efmn"; byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec(sKey.getBytes()); cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); byte[] encrypted1 = Base64.getDecoder().decode(endSrc);//先用base64解密 byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original); logger.info("解密结果:{}", originalString); return originalString; } catch (Exception e) { System.out.println(e.toString()); return null; } } }