Base64Util.java 1011 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.template.common.utils;
  2. import org.apache.tomcat.util.codec.binary.Base64;
  3. import java.nio.charset.Charset;
  4. /**
  5. * <p>Title: Base64Util</p>
  6. * <p>Description: Base64Util工具类 --- 加密和解密</p>
  7. * @author fengyong
  8. * @date 2018年9月7日
  9. */
  10. public class Base64Util {
  11. public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
  12. /**
  13. * 解密
  14. * @param str
  15. * @return
  16. */
  17. public static String decodeStr(String str){
  18. if (str == null) {
  19. return null;
  20. }
  21. if (str.length() == 0) {
  22. return "";
  23. }
  24. return new String(Base64.decodeBase64(new String(str).getBytes(DEFAULT_CHARSET)),DEFAULT_CHARSET).trim();
  25. }
  26. /**
  27. * 加密
  28. *
  29. * @param str
  30. * @return
  31. */
  32. public static String encodeStr(String str){
  33. if (str == null) {
  34. return null;
  35. }
  36. if (str.length() == 0) {
  37. return "";
  38. }
  39. return new String(Base64.encodeBase64Chunked(str.getBytes(DEFAULT_CHARSET)),DEFAULT_CHARSET).trim();
  40. }
  41. }