private static byte[] encrypt(byte[] text, byte[] key) throws Exception { SecretKeySpec aesKey = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, aesKey); return cipher.doFinal(text); } private static byte[] decrypt(byte[] text, byte[] key) throws Exception { SecretKeySpec aesKey = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, aesKey); return cipher.doFinal(text); } public static String encodeAES(String text, String key) throws Exception { byte[] keyBytes = DigestUtils.md5(key);//在线网站上没有md5加密,直接getBytes //等同于 MessageDigest.getInstance("MD5").digest(key.getBytes()) byte[] textBytes = text.getBytes(); byte[] aesBytyes = encrypt(textBytes, keyBytes); return new String(Base64.encodeBase64(aesBytyes)); }
|