加密:
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;public class Base64Decoder {
public Object convert(String val,int maxLen) {
String result = val;
try {
result = new String(Base64.decodeBase64(val), "UTF-8");
return truncateString(result, maxLen);
} catch (UnsupportedEncodingException e) {
return val;
}
}
private String truncateString(String value, int maxLength) {
int len = 0;
StringBuilder sb = new StringBuilder();
for (char ch : value.toCharArray()) {
if (ch >= '\uF900' && ch <= '\uFAD9') {
len += 3;
} else if (ch >= '\u4E00' && ch <= '\u9FFF') {
len += 3;
} else if (ch >= '\u3400' && ch <= '\u4DB5') {
len += 3;
} else if (ch >= '\uE000' && ch <= '\uF8FF') {
len += 3;
} else if (Character.isHighSurrogate(ch)) {
len += 6;
} else if (Character.isLowSurrogate(ch)) {
len += 0;
} else {
len++;
}
if (len > maxLength) {
break;
}
sb.append(ch);
}
return sb.toString();
}
public static void main(String[] args) {
Base64Decoder b = new Base64Decoder();
String val = "AAA=";
System.out.println(b.convert(val,1500));
}
}
解密:
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
public class Base64LowEndianDecoder {
public Object convert(String val,int maxLen) {
if(StringUtils.isBlank(val)){
return val;
}
try {
//首先将字符串解码成字节数组
byte[] b = Base64.decodeBase64(val);
String ret = "";
//每两个字节一组循环
if(b.length % 2 != 0){
if(val.length() > maxLen){
return val.substring(0, maxLen);
}
return val;
}
for(int i = 0; i < b.length; i=i+2){
String hex = changeByteToHex(b[i+1]) + changeByteToHex(b[i]);
int n = Integer.parseInt(hex,16);
ret += n;
if(i < b.length-2){
ret += ";";
}
}
return ret.subSequence(0, Math.min(ret.length(), maxLen));
} catch (Exception e) {
return val;
}
}
private String changeByteToHex(byte b){
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
return hex;
}
public static void main(String[] args) {
Base64LowEndianDecoder b = new Base64LowEndianDecoder();
String val = "QUE9PQ==";
System.out.println(b.convert(val,600));
}
}
import org.apache.commons.lang.StringUtils;
public class Base64LowEndianDecoder {
public Object convert(String val,int maxLen) {
if(StringUtils.isBlank(val)){
return val;
}
try {
//首先将字符串解码成字节数组
byte[] b = Base64.decodeBase64(val);
String ret = "";
//每两个字节一组循环
if(b.length % 2 != 0){
if(val.length() > maxLen){
return val.substring(0, maxLen);
}
return val;
}
for(int i = 0; i < b.length; i=i+2){
String hex = changeByteToHex(b[i+1]) + changeByteToHex(b[i]);
int n = Integer.parseInt(hex,16);
ret += n;
if(i < b.length-2){
ret += ";";
}
}
return ret.subSequence(0, Math.min(ret.length(), maxLen));
} catch (Exception e) {
return val;
}
}
private String changeByteToHex(byte b){
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
return hex;
}
public static void main(String[] args) {
Base64LowEndianDecoder b = new Base64LowEndianDecoder();
String val = "QUE9PQ==";
System.out.println(b.convert(val,600));
}
}