EncryptUtils.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package cn.efunbox.base.util;
  2. import org.apache.commons.codec.binary.Base64;
  3. import org.apache.commons.lang3.StringUtils;
  4. import sun.misc.BASE64Decoder;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.KeyGenerator;
  7. import javax.crypto.spec.SecretKeySpec;
  8. /**
  9. * EncryptUtils
  10. * Created by wangys on
  11. */
  12. public class EncryptUtils {
  13. /**
  14. * 密钥
  15. */
  16. private static final String KEY = "GfqqwfctR8gdNGUS";// AES加密要求key必须要128个比特位(这里需要长度为16,否则会报错)
  17. /**
  18. * 算法
  19. */
  20. private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
  21. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050001/00103050001.m3u8";
  22. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050002/00103050002.m3u8";
  23. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050003/00103050003.m3u8";
  24. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050004/00103050004.m3u8";
  25. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050005/00103050005.m3u8";
  26. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050011/00103050011.m3u8";
  27. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050012/00103050012.m3u8";
  28. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050013/00103050013.m3u8";
  29. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050014/00103050014.m3u8";
  30. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050015/00103050015.m3u8";
  31. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050016/00103050016.m3u8";
  32. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050017/00103050017.m3u8";
  33. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050018/00103050018.m3u8";
  34. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050019/00103050019.m3u8";
  35. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050020/00103050020.m3u8";
  36. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050021/00103050021.m3u8";
  37. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050022/00103050022.m3u8";
  38. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050023/00103050023.m3u8";
  39. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050024/00103050024.m3u8";
  40. // private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050025/00103050025.m3u8";
  41. private static String content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050026/00103050026.m3u8";
  42. public static void main(String[] args) throws Exception {
  43. //content = "http://baidu-yuwen-video.ai160.com/vs2m/001/00103050/00103050001/00103050001.m3u8";
  44. System.out.println("加密前:" + content);
  45. System.out.println("加密密钥和解密密钥:" + KEY);
  46. String encrypt = aesEncrypt(content);
  47. System.out.println("加密后:" + encrypt);
  48. String decrypt = aesDecrypt(encrypt, KEY);
  49. System.out.println("解密后:" + decrypt);
  50. }
  51. /**
  52. * base 64 encode
  53. * @param bytes 待编码的byte[]
  54. * @return 编码后的base 64 code
  55. */
  56. private static String base64Encode(byte[] bytes){
  57. return Base64.encodeBase64String(bytes);
  58. }
  59. /**
  60. * base 64 decode
  61. * @param base64Code 待解码的base 64 code
  62. * @return 解码后的byte[]
  63. * @throws Exception 抛出异常
  64. */
  65. private static byte[] base64Decode(String base64Code) throws Exception{
  66. return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
  67. }
  68. /**
  69. * AES加密
  70. * @param content 待加密的内容
  71. * @param encryptKey 加密密钥
  72. * @return 加密后的byte[]
  73. */
  74. private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
  75. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  76. kgen.init(128);
  77. Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
  78. cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
  79. return cipher.doFinal(content.getBytes("utf-8"));
  80. }
  81. /**
  82. * AES加密为base 64 code
  83. *
  84. * @param content 待加密的内容
  85. * @return 加密后的base 64 code
  86. */
  87. public static String aesEncrypt(String content) {
  88. String encode = null;
  89. try {
  90. encode = base64Encode(aesEncryptToBytes(content, KEY));
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. return encode;
  95. }
  96. /**
  97. * AES加密为base 64 code
  98. *
  99. * @param content 待加密的内容
  100. * @return 加密后的base 64 code
  101. */
  102. public static String aesEncrypt(String content,String secret) {
  103. String encode = null;
  104. try {
  105. encode = base64Encode(aesEncryptToBytes(content, secret));
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. }
  109. return encode;
  110. }
  111. /**
  112. * AES解密
  113. *
  114. * @param encryptBytes 待解密的byte[]
  115. * @param decryptKey 解密密钥
  116. * @return 解密后的String
  117. */
  118. private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
  119. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  120. kgen.init(128);
  121. Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
  122. cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
  123. byte[] decryptBytes = cipher.doFinal(encryptBytes);
  124. return new String(decryptBytes);
  125. }
  126. /**
  127. * 将base 64 code AES解密
  128. *
  129. * @param encryptStr 待解密的base 64 code
  130. * @param decryptKey 解密密钥
  131. * @return 解密后的string
  132. */
  133. public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
  134. return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
  135. }
  136. }