溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

base64實現(xiàn)解碼轉(zhuǎn)化的方法

發(fā)布時間:2020-06-09 16:55:07 來源:億速云 閱讀:310 作者:元一 欄目:編程語言

                                                           概念

Base64是網(wǎng)絡上最常見的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個可打印字符來表示二進制數(shù)據(jù)的方法??刹榭碦FC2045~RFC2049,上面有MIME的詳細規(guī)范。
Base64編碼是從二進制到字符的過程,可用于在HTTP環(huán)境下傳遞較長的標識信息。采用Base64編碼具有不可讀性,需要解碼后才能閱讀。
Base64由于以上優(yōu)點被廣泛應用于計算機的各個領域,然而由于輸出內(nèi)容中包括兩個以上“符號類”字符(+, /, =),不同的應用場景又分別研制了Base64的各種“變種”。為統(tǒng)一和規(guī)范化Base64的輸出,Base62x被視為無符號化的改進版本。
  1. 示例一

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.6</version>
</dependency>

public static void main(String[] args) throws UnsupportedEncodingException {

    String str = "PERJVj53d2RhZGZhZGYgYXNkZmFzZiBhc2Zhc2QgZnNhZGZmYTwvRElWPg==";
    String decodeStr = new String(Base64.decodeBase64(str),"GBK");
    System.out.println(decodeStr);
}

  1. 示例二

// 項目 www.1b23.com
public class Base64 {

    private static final char[] S_BASE64CHAR = {
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
        'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
        'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 
        'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
        'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
        'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', 
        '8', '9', '+', '/'
    };
    
    private static final char S_BASE64PAD = '=';
    private static final byte[] S_DECODETABLE = new byte[128];
    static {
        for (int i = 0;  i <S_DECODETABLE.length;  i ++)
            S_DECODETABLE[i] = Byte.MAX_VALUE;  // 127
        for (int i = 0;  i <S_BASE64CHAR.length;  i ++) // 0 to 63
            S_DECODETABLE[S_BASE64CHAR[i]] = (byte)i;
    }

    private static int decode0(char[] ibuf, byte[] obuf, int wp) {
        int outlen = 3;
        if (ibuf[3] == S_BASE64PAD)  outlen = 2;
        if (ibuf[2] == S_BASE64PAD)  outlen = 1;
        int b0 = S_DECODETABLE[ibuf[0]];
        int b1 = S_DECODETABLE[ibuf[1]];
        int b2 = S_DECODETABLE[ibuf[2]];
        int b3 = S_DECODETABLE[ibuf[3]];
        switch (outlen) {
          case 1:
            obuf[wp] = (byte)(b0 <<2 & 0xfc | b1>> 4 & 0x3);
            return 1;
          case 2:
            obuf[wp++] = (byte)(b0 <<2 & 0xfc | b1>> 4 & 0x3);
            obuf[wp] = (byte)(b1 <<4 & 0xf0 | b2>> 2 & 0xf);
            return 2;
          case 3:
            obuf[wp++] = (byte)(b0 <<2 & 0xfc | b1>> 4 & 0x3);
            obuf[wp++] = (byte)(b1 <<4 & 0xf0 | b2>> 2 & 0xf);
            obuf[wp] = (byte)(b2 <<6 & 0xc0 | b3 & 0x3f);
            return 3;
          default:
            throw new RuntimeException("Internal Errror");
        }
    }

    public static byte[] decode(String data) {
        char[] ibuf = new char[4];
        int ibufcount = 0;
        byte[] obuf = new byte[data.length()/4*3+3];
        int obufcount = 0;
        for (int i = 0;  i <data.length();  i ++) {
            char ch = data.charAt(i);
            if (ch == S_BASE64PAD
                || ch <S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                ibuf[ibufcount++] = ch;
                if (ibufcount == ibuf.length) {
                    ibufcount = 0;
                    obufcount += decode0(ibuf, obuf, obufcount);
                }
            }
        }
        if (obufcount == obuf.length)
            return obuf;
        byte[] ret = new byte[obufcount];
        System.arraycopy(obuf, 0, ret, 0, obufcount);
        return ret;
}

public static void main(String[] args) throws UnsupportedEncodingException {

     String str = "PERJVj53d2RhZGZhZGYgYXNkZmFzZiBhc2Zhc2QgZnNhZGZmYTwvRElWPg==";
     String decodeStr = new String(Base64.decode(str),"GBK");
     System.out.println(decodeStr);
}


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI