public static byte[] encrypt(String content, String password) { try { byte[] enCodeFormat = password.getBytes(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); byte[] byteContent = content.getBytes("utf-8"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] result = cipher.doFinal(byteContent); return result; } catch (Exception e) { // LOG.error(" aes encrypt error: [{}]", password,e); e.printStackTrace(); return null; } }
// aes 二进制转成base64 import org.apache.commons.codec.binary.Base64; String s = new String(Base64.encodeBase64(encryptBytes)); // s 为加密后的base64编码