Base64和MD5都可用于做信息的简单加密,两者的简单差别如下:
Base64:
1、可逆性。
2、可以将图片等二进制文件转换为文本文件。
3、可以把非ASCII字符的数据转换成ASCII字符,避免不可见字符。
MD5:
1、不可逆性。
2、任意长度的明文字符串,加密后得到的密文字符串是长度固定的。
Base64使用:
package com.example.demo.util;
import net.sf.json.JSONObject;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Base64 {
// 加密
public static String getBase64(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) {
try{
//System.out.println(Base64Util.encode("1".getBytes()));
Map<String, Object> Map = new HashMap<String, Object>();
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> Map1 = new HashMap<String, Object>();
Map1.put("name","菜鸟教程");
Map1.put("url","www.runoob.com");
list.add(Map1);
Map<String, Object> Map2 = new HashMap<String, Object>();
Map2.put("name","google");
Map2.put("url","www.google.com");
list.add(Map2);
Map<String, Object&