import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;import org.apache.tomcat.util.codec.binary.Base64;public class AESUtils { private static final String ALGORITHM = "AES/ECB/PKCS5Padding"; public static final String AES = "AES"; public static final String CHARSET_UTF8 = "utf-8"; public AESUtils() { } public static String encrypt(String content, String key) throws Exception { byte[] raw = key.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(1, skeySpec); byte[] encrypted = cipher.doFinal(content.getBytes("utf-8")); return Base64.encodeBase64String(encrypted); } public static String dcrypt(String content, String key) throws Exception { byte[] raw = key.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(2, skeySpec); byte[] encrypted1 = (new Base64()).decode(content); byte[] original = cipher.doFinal(encrypted1); return new String(original, "utf-8"); }}
资料