1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package com.fdkankan.common.utils;
- import java.io.UnsupportedEncodingException;
- import java.util.Base64;
- public class Base64Converter {
- public final static Base64.Encoder encoder = Base64.getEncoder();
- final static Base64.Decoder decoder = Base64.getDecoder();
- /**
- * 给字符串加密
- * @param text
- * @return
- */
- public static String encode(String text) {
- byte[] textByte = new byte[0];
- try {
- textByte = text.getBytes("UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- String encodedText = encoder.encodeToString(textByte);
- return encodedText;
- }
- /**
- * 给字符串加密
- * @param textByte
- * @return
- */
- public static String encode(byte[] textByte) {
- return encoder.encodeToString(textByte);
- }
- /**
- * 将加密后的字符串进行解密
- * @param encodedText
- * @return
- */
- public static String decode(String encodedText) {
- String text = null;
- try {
- text = new String(decoder.decode(encodedText), "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return text;
- }
- /**
- * 根据逻辑截取加密后的密码
- * @param text
- * @return
- */
- public static String subText(String text){
- //去掉前8位字符串
- text = text.substring(8);
- //去掉后8位字符串
- text = text.substring(0, text.length() - 8);
- //最后两个字符串换到前面,并且去掉剩下的后8位字符串
- String result = text.substring(text.length() - 2) + text.substring(0, text.length() - 10);
- return result;
- }
- }
|