TLinxAESCoder.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * @Filename: TLinxAESCoder.java
  3. * @Author锛歝aiqf
  4. * @Date锛�016-4-12
  5. */
  6. package com.happy.Unitil_nsh;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.spec.SecretKeySpec;
  9. /**
  10. * @Class: TLinxAESCoder.java
  11. * @Description: AES加解密类
  12. * @Author:caiqf
  13. * @Date:2016-4-12
  14. */
  15. public class TLinxAESCoder {
  16. private static String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
  17. private static String KEY_ALGORITHM = "AES";
  18. public static String decrypt(String sSrc, String sKey) throws Exception {
  19. SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes("ASCII"), KEY_ALGORITHM);
  20. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
  21. cipher.init(2, skeySpec);
  22. byte[] encrypted1 = TLinx2Util.hex2byte(sSrc);
  23. byte[] original = cipher.doFinal(encrypted1);
  24. return new String(original, "UTF-8");
  25. }
  26. public static String encrypt(String sSrc, String sKey) throws Exception {
  27. System.out.println("====data加密前的明文= " + sSrc);
  28. SecretKeySpec skeySpec = new SecretKeySpec(sKey.getBytes("ASCII"), KEY_ALGORITHM);
  29. Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
  30. cipher.init(1, skeySpec);
  31. byte[] encrypted = cipher.doFinal(sSrc.getBytes("UTF-8"));
  32. return TLinx2Util.byte2hex(encrypted);
  33. }
  34. public static void main(String[] args) {
  35. String data = "";
  36. String str = null;
  37. try {
  38. str = decrypt(data, "");
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. System.out.println(str);
  43. }
  44. }