8c9bc9879b8fc579109098981ee49c010ac355a5.svn-base 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package videoalliance;
  2. import java.io.UnsupportedEncodingException;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.spec.IvParameterSpec;
  5. import javax.crypto.spec.SecretKeySpec;
  6. import org.apache.commons.lang.StringUtils;
  7. import com.chinacreator.common.exception.BusinessException;
  8. /**
  9. * AES加密工具类
  10. */
  11. public class AESUtil {
  12. private static final byte[] IV = new byte[16];
  13. /**
  14. * 二进制转变为16进制
  15. *
  16. * @param buf
  17. * @return
  18. */
  19. public static String parseByte2HexStr(byte[] buf) {
  20. StringBuffer sb = new StringBuffer();
  21. for (int i = 0; i < buf.length; i++) {
  22. String hex = Integer.toHexString(buf[i] & 0xFF);
  23. if (hex.length() == 1) {
  24. hex = '0' + hex;
  25. }
  26. sb.append(hex.toUpperCase());
  27. }
  28. return sb.toString();
  29. }
  30. /**
  31. * 将16进制转变为二进制
  32. *
  33. * @param hexStr
  34. * @return
  35. */
  36. public static byte[] parseHexStr2Byte(String hexStr) {
  37. if (hexStr.length() < 1) {
  38. return null;
  39. }
  40. byte[] result = new byte[hexStr.length() / 2];
  41. for (int i = 0; i < hexStr.length() / 2; i++) {
  42. int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
  43. int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
  44. result[i] = (byte) (high * 16 + low);
  45. }
  46. return result;
  47. }
  48. /**
  49. * AES解密
  50. *
  51. * @param content
  52. * 密文
  53. * @param keyWord
  54. * 密钥
  55. * @return
  56. * @throws BusinessException
  57. * @throws Exception
  58. */
  59. public static byte[] decrypt(byte[] content, String keyWord) throws BusinessException {
  60. if(StringUtils.isEmpty(keyWord)) {
  61. return null;
  62. }
  63. try {
  64. SecretKeySpec key = new SecretKeySpec(keyWord.getBytes(), "AES");
  65. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  66. cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV));
  67. byte[] result = cipher.doFinal(content);
  68. return result;
  69. } catch (Exception e) {
  70. throw new BusinessException("9010", "解密失败");
  71. }
  72. }
  73. /**
  74. * AES 解密
  75. *
  76. * @param content
  77. * 密文
  78. * @param keyWord
  79. * 密钥
  80. * @return
  81. * @throws UnsupportedEncodingException
  82. * @throws BusinessException
  83. */
  84. public static String decrypt(String content, String keyWord) throws UnsupportedEncodingException, BusinessException {
  85. if(StringUtils.isEmpty(content)) {
  86. return null;
  87. }
  88. byte[] contentBytes = parseHexStr2Byte(content);
  89. byte[] result = decrypt(contentBytes, keyWord);
  90. return new String(result, "UTF-8");
  91. }
  92. /**
  93. * AES 加密
  94. *
  95. * @param content
  96. * 内容
  97. * @param keyWord
  98. * 密钥
  99. * @return
  100. * @throws BusinessException
  101. */
  102. public static byte[] encrypt(byte[] content, String keyWord) throws BusinessException {
  103. if(StringUtils.isEmpty(keyWord)) {
  104. return null;
  105. }
  106. try {
  107. SecretKeySpec key = new SecretKeySpec(keyWord.getBytes(), "AES");
  108. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  109. cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));
  110. byte[] encryptedData = cipher.doFinal(content);
  111. return encryptedData;
  112. } catch (Exception e) {
  113. throw new BusinessException("9010", "加密失败");
  114. }
  115. }
  116. /**
  117. * AES 加密
  118. *
  119. * @param content
  120. * 内容
  121. * @param keyWord
  122. * 密钥
  123. * @return
  124. * @throws UnsupportedEncodingException
  125. * @throws BusinessException
  126. */
  127. public static String encrypt(String content, String keyWord) throws UnsupportedEncodingException, BusinessException {
  128. if(StringUtils.isEmpty(content)) {
  129. return null;
  130. }
  131. byte[] result = encrypt(content.getBytes("UTF-8"), keyWord);
  132. return parseByte2HexStr(result);
  133. }
  134. public static void main(String[] args) throws UnsupportedEncodingException, BusinessException {
  135. System.out.println(decrypt("7EED1F816FEFB5EABEA99967BBCC2274".toLowerCase(), "1234567812345678"));
  136. }
  137. }