1. import javax.crypto.Cipher;
  2. import javax.crypto.spec.SecretKeySpec;
  3. import org.apache.tomcat.util.codec.binary.Base64;
  4. public class AESUtils {
  5. private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
  6. public static final String AES = "AES";
  7. public static final String CHARSET_UTF8 = "utf-8";
  8. public AESUtils() {
  9. }
  10. public static String encrypt(String content, String key) throws Exception {
  11. byte[] raw = key.getBytes("utf-8");
  12. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  13. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  14. cipher.init(1, skeySpec);
  15. byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
  16. return Base64.encodeBase64String(encrypted);
  17. }
  18. public static String dcrypt(String content, String key) throws Exception {
  19. byte[] raw = key.getBytes("utf-8");
  20. SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  21. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  22. cipher.init(2, skeySpec);
  23. byte[] encrypted1 = (new Base64()).decode(content);
  24. byte[] original = cipher.doFinal(encrypted1);
  25. return new String(original, "utf-8");
  26. }
  27. }

资料