import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
public class Base64Utils {
private static final Base64.Decoder decoder = Base64.getDecoder();
private static final Base64.Encoder encoder = Base64.getEncoder();
private static final String encodeCh = "UTF-8";
private static final String encodeStr = "pT7yL2x54w";
private static final String replaceStr = "";
public static String base64Encode(String text) {
try {
if (StringUtils.isNotBlank(text)) {
byte[] textByte = text.getBytes(encodeCh);
String encodedText = encoder.encodeToString(textByte);
StringBuilder encodedTextStr = new StringBuilder(encodedText);
encodedText = encodedTextStr.insert(encodedTextStr.length()/2 - 1, encodeStr).toString();
byte[] textByte1 = encodedText.getBytes(encodeCh);
encodedText = encoder.encodeToString(textByte1);
return encodedText;
} else {
return null;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static String base64Decode(String encodedText) {
try {
String textStr = new String(decoder.decode(encodedText), encodeCh);
String encodedTextStr = textStr.replaceFirst(encodeStr, replaceStr);
String text = new String(decoder.decode(encodedTextStr), encodeCh);
return text;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
}
11-28