• Base64编码学习


    Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。

    “我们知道在计算机中任何数据都是按ascii码存储的,而ascii码的128~255之间的值是不可见字符。而在网络上交换数据时,比如说从A地传到B地,往往要经过多个路由设备,由于不同的设备对字符的处理方式有一些不同,这样那些不可见字符就有可能被处理错误,这是不利于传输的。所以就先把数据先做一个Base64编码,统统变成可见字符,这样出错的可能性就大降低了。”
    以上片段:
    作者:郭无心
    链接:https://www.zhihu.com/question/36306744/answer/71626823
    来源:知乎
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    实现原理:自行百度百科。


    以下是3种实现base64编码的代码方式:

    • jdk
    • Commons Codec
    • Bouncy Castle
      测试代码以及自行娱乐代码:
    package base64Test;
    
    import java.io.IOException;
    
    import org.apache.commons.codec.binary.Base64;
    import org.bouncycastle.util.encoders.Base64Encoder;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    /**
    *author:https://blog.csdn.net/qq_32953185
    *time:2018-4-4
    */
    public class Base64Test {
    
        private static String src = "base64 test";
    
        public static void main(String[] args) {
            jdkBase64();
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
            commonsCodesBase64();
            bouncyCastleBase64();
            System.out.println("----------------------");
            String x=Base64_repeat("base64 test",6);    //加密多次
            System.out.println(x);
            System.out.println(Base64_repeat_break(x,6)); //解密多次
        }
    
        //定义一个静态的Base64加密方法
        public static String Base64_jiami(String s){
            String result = null;
            BASE64Encoder encoder = new BASE64Encoder();
            result=encoder.encode(s.getBytes());
            return result;
        }
    
        //定义一个静态的可自行定义加密次数的、重复Base64加密的方法
        public static String Base64_repeat(String s,int x){
            String result = s;
            BASE64Encoder encoder = new BASE64Encoder();
            for(int i=0;i<x;i++){
                result=encoder.encode(result.getBytes());
            }
            return result;
        }
        //定义一个静态的可自行定义解密次数的、重复Base64解密的方法
        public static String Base64_repeat_break(String s,int x){
            String result = s;
            try {
                BASE64Decoder decoder = new BASE64Decoder();
                for(int i=0;i<x;i++){
                    result=new String(decoder.decodeBuffer(result));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * jdk API方式
         */
        public static void jdkBase64(){
            try {
                BASE64Encoder encoder = new BASE64Encoder();
                String encode=encoder.encode(src.getBytes());
                //试试加密2次。。。
                String encode2=encoder.encode(encode.getBytes());
                System.out.println("encode:"+encode);
                System.out.println("加密两次的结果:"+encode2);
    
                BASE64Decoder decoder = new BASE64Decoder();
                System.out.println("decode:"+ new String(decoder.decodeBuffer(encode)));
                //解密2次。。。
                String encode2_1=new String(decoder.decodeBuffer(encode2));
                System.out.println("第一次:"+encode2_1);
                System.out.println("解密两次的结果:"+ new String(decoder.decodeBuffer(encode2_1)));
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * Comons Codec方式  CC
         */
        public static void commonsCodesBase64(){
            byte[] encodeBytes = Base64.encodeBase64(src.getBytes());
            System.out.println("encode:"+new String(encodeBytes));
    
            byte[] decodeBytes = Base64.decodeBase64(encodeBytes);
            System.out.println("decode:"+new String(decodeBytes));
    
        }
    
        /**
         * Bouncy Castle方式  BC
         */
        public static void bouncyCastleBase64(){
            byte[] encodeBytes=org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
            System.out.println("encode:"+new String(encodeBytes));
    
            byte[] decodeBytes=org.bouncycastle.util.encoders.Base64.decode(encodeBytes);
            System.out.println("decode:"+new String(decodeBytes));
    
        }
    }
    
  • 相关阅读:
    零基础学习Java Web开发(一)
    域名的定义
    MyEclipse使用(一)
    VB与C#语言部分不用的地方Part1
    使用XmlWriter创建XML文件
    Spring源码
    Websocket原理
    阿里云
    CSS中position属性( absolute | relative | static | fixed )详解
    C#UDP广域网,局域网通信-原理分析
  • 原文地址:https://www.cnblogs.com/famine/p/9124715.html
Copyright © 2020-2023  润新知