Base64Converter.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.fdkankan.common.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.util.Base64;
  4. public class Base64Converter {
  5. public final static Base64.Encoder encoder = Base64.getEncoder();
  6. final static Base64.Decoder decoder = Base64.getDecoder();
  7. /**
  8. * 给字符串加密
  9. * @param text
  10. * @return
  11. */
  12. public static String encode(String text) {
  13. byte[] textByte = new byte[0];
  14. try {
  15. textByte = text.getBytes("UTF-8");
  16. } catch (UnsupportedEncodingException e) {
  17. e.printStackTrace();
  18. }
  19. String encodedText = encoder.encodeToString(textByte);
  20. return encodedText;
  21. }
  22. /**
  23. * 给字符串加密
  24. * @param textByte
  25. * @return
  26. */
  27. public static String encode(byte[] textByte) {
  28. return encoder.encodeToString(textByte);
  29. }
  30. /**
  31. * 将加密后的字符串进行解密
  32. * @param encodedText
  33. * @return
  34. */
  35. public static String decode(String encodedText) {
  36. String text = null;
  37. try {
  38. text = new String(decoder.decode(encodedText), "UTF-8");
  39. } catch (UnsupportedEncodingException e) {
  40. e.printStackTrace();
  41. }
  42. return text;
  43. }
  44. /**
  45. * 根据逻辑截取加密后的密码
  46. * @param text
  47. * @return
  48. */
  49. public static String subText(String text){
  50. //去掉前8位字符串
  51. text = text.substring(8);
  52. //去掉后8位字符串
  53. text = text.substring(0, text.length() - 8);
  54. //最后两个字符串换到前面,并且去掉剩下的后8位字符串
  55. String result = text.substring(text.length() - 2) + text.substring(0, text.length() - 10);
  56. return result;
  57. }
  58. }