原 Base64Util 工具类
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1016
package com.fyq.utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Util {
// 加密
public static String encodeBase64(String mingwen){
String code="";
if(mingwen==null||mingwen.equals("")){
}else{
BASE64Encoder encoder = new BASE64Encoder();
try {
code=encoder.encode(mingwen.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println("加密后["+code+"]");
}
return code;
}
// 解密
public static String decodeBase64(String mi){
String mingwen="";
if(mi==null||mi.equals("")){
}else{
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] by = decoder.decodeBuffer(mi);
mingwen = new String(by);
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println("解密后["+mingwen+"]");
}
return mingwen;
}
// 提供加密N次
public static String encodeBase64(String mingwen,int times){
int num=(times<=0)?1:times;
String code="";
if(mingwen==null||mingwen.equals("")){
}else{
code=mingwen;
for(int i=0;i<num;i++){
code=encodeBase64(code);
}
System.out.println("加密"+num+"次后["+code+"]");
}
return code;
}
// 对应提供解密N次
public static String decodeBase64(String mi,int times){
int num=(times<=0)?1:times;
String mingwen="";
if(mi==null||mi.equals("")){
}else{
mingwen=mi;
for(int i=0;i<num;i++){
mingwen=decodeBase64(mingwen);
}
// System.out.println("解密"+num+"次后["+mingwen+"]");
}
return mingwen;
}
public static void main(String[] args) {
String str="dianping123456";
System.out.println("测试明文["+str+"]");
String basecode =Base64Util.encodeBase64(str);
System.out.println("加密后["+basecode+"]");
if(basecode!=null){
String res =Base64Util.decodeBase64(basecode);
System.out.println("解密后["+res+"]");
}
System.out.println("");
System.out.println("N次加密测试--------");
String basecodeN=Base64Util.encodeBase64(str, 2);
String resN=Base64Util.decodeBase64(basecodeN, 2);
String basecodeN3=Base64Util.encodeBase64(str, 5);
String resN3=Base64Util.decodeBase64(basecodeN3, 5);
}
}
2019-08-09 21:29:10 阅读(1204)
名师出品,必属精品 https://www.91mszl.com
博主信息