import com.alibaba.druid.filter.config.ConfigTools;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
public class Base64Util {
//加密
public static String SetBase64(String str) {
byte[] b = null;
String s = null;
try {
b = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b != null) {
s = new BASE64Encoder().encode(b);
}
return s;
}
//解密
public static String GetFromBase64(String s) {
byte[] b = null;
String result = null;
if (s != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(s);
result = new String(b, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static void main(String[] args) throws Exception {
String s="123456@QUWE";
System.out.println("原密:" + s);
//加密
String s1 = SetBase64(s);
System.out.println("加密:" + s1);
//解密
String s2 = GetFromBase64(s1);
System.out.println("解密:" + s2);
String decrypt = ConfigTools.decrypt("MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJVfpadFl5pKHRWJVxJi08BhqRYMfrkELpZR78HIvacyrXWektWsb5StI94lhXYoonzCpjjPZDDEgSmgxTah4QkCAwEAAQ==","b6U/KvgBRE0QjG+As9Hyb4s8YjWeIQ7dOFWwXz2pE0CXNOcT2QULC4VUS1zzwCC8OMzY95gRmYiZW0BP/K0Pgg==");
System.out.println("####"+decrypt);
}
}
Base64实现加解密
最新推荐文章于 2024-07-02 17:07:34 发布