a1b3aa70e6bd8fec33545def793e3f31d0a7be1c.svn-base 4.5 KB

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