zoukankan      html  css  js  c++  java
  • Java Base64 加密/解密

    Base64常用来表示字串加密过后的内容,使用Java 程式语言来实作Base64的编码与解码功能

    1.在Java上做Base64的编码与解码,会使用到JDK里sun.misc套件下的BASE64Encoder和BASE64Decoder这两个类别,代码如下

    2.Apache Commons Codec有提供Base64的编码与解码功能,使用到org.apache.commons.codec.binary套件下的Base64类别

      http://commons.apache.org/proper/commons-codec/download_codec.cgi 下载commons-codec-1.11-bin代码

    3.java 8的java.util套件中,新增了Base64的类别,可以用来处理Base64的编码与解码

    package com.test;
    
    import org.apache.commons.codec.binary.Base64;
    import org.junit.Test;
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    /**
     * @author ceshi
     * @Title: JUnitBase64
     * @ProjectName ceshi
     * @Description: TODO
     * @date 2018/6/1320:59
     */
    public class JUnitBase64 {
    
        @Test
        public void test()throws Exception{
            //BASE64Encoder
            BASE64Encoder encoder = new BASE64Encoder();
            BASE64Decoder decoder = new BASE64Decoder();
            String text = "你好!";
            byte[] textByte = text.getBytes("UTF-8");
            //编码
            String encodedText = encoder.encode(textByte);
            System.out.println(encoder.encode(text.getBytes("UTF-8"));
            //解码
            System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));
    
            //Apache Commons Codec
            Base64 base64 = new Base64();
            textByte = text.getBytes("UTF-8");
            //编码
            encodedText = base64.encodeToString(textByte);
            System.out.println(encodedText);
            //解码
            System.out.println(new String(base64.decode(encodedText), "UTF-8"));
    
    
            //Java 8
            Base64.Decoder decoder = Base64.getDecoder();
            Base64.Encoder encoder = Base64.getEncoder();
    
            //编码
            encodedText = encoder.encodeToString(textByte);
            System.out.println(encodedText);
            //解码
            System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
    
    
        }
    }
  • 相关阅读:
    OpenFlow Switch学习笔记(一)——基础概念
    Open vSwitch 给虚拟机网卡限流(QoS)
    MySQL字符集或字符序
    timestamp和datetime
    MySQL Audit日志审计
    sysbench0.4.12测试query_cache_size和query_cache_type
    MySQL 异地 双机房同步之otter
    keep running
    Linux Bonding
    自动化测试-2.seleniumIDE
  • 原文地址:https://www.cnblogs.com/qinxu/p/9180169.html
Copyright © 2011-2022 走看看