java代码如下:
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
public class BASE64 {
//加密
public static String getBase64(String str){
byte[] b = null;
String s = null;
b = str.getBytes();
System.out.println(b);
if(b != null){
s = Base64.encodeBase64String(b);
}
return s;
}
// 解密
public static String getFromBase64(String s) {
byte[] b = null;
String result = null;
if (s != null) {
b = Base64.decodeBase64(s);
//System.out.println(b);
try {
result = new String(b,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
String str="22";
String ss = getBase64(str);
System.out.println(ss);
String res = getFromBase64(ss);
System.out.println(res);
}
}