Java Base64加密解密例子

2020-04-03 16:12:14来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

package cn.ljs.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Miracle Luna
 * @version 1.0
 * @date 2019/7/3 18:55
 */
public class Base64Util {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String sotcbb = "sotcbb_";//自定义盐值
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", "1");
        map.put("name", "地铁");
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("id", "2");
        map1.put("name", "公交车");
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        list.add(map);
        list.add(map1);
        for (int i = 0; i < list.size(); i++) {
            map = list.get(i);
            String aString = null;
            try {
                aString = Base64Util.encoder(sotcbb+(String) map.get("name"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("编码:"+aString);
            map.put("name", aString);
        }
        System.out.println("编码:"+list);
        
        for (int i = 0; i < list.size(); i++) {
            map = list.get(i);
            String aString = null;
            try {
                aString = Base64Util.decoder((String) map.get("name"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("解码:"+aString.split("_")[1]);
            map.put("name", aString.split("_")[1]);
        }
        System.out.println("解码:"+list);
        
        String string = "李某";
        String eString = "5p2O5p+Q";
        System.out.println(string+"<---编码--->"+Base64Util.encoder(string));
        System.out.println(eString+"<---解码--->"+Base64Util.decoder(eString));
    }

    /**
     * base64编码
     * @param string
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encoder(String string) throws UnsupportedEncodingException {
        Encoder encoder = Base64.getEncoder();
        byte[] textByte = string.getBytes("UTF-8");
        return encoder.encodeToString(textByte);
    }
    
    /**
     * base64解码
     * @param string
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String decoder(String string) throws UnsupportedEncodingException {
        Decoder decoder = Base64.getDecoder();
        return new String(decoder.decode(string), "UTF-8");
    }
}

 


原文链接:https://www.cnblogs.com/ljmm/p/12627563.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:java新时间类

下一篇:LeetCode 36. 有效的数独