常用編碼格式算法

1、base64

Base64 是網絡上最常見的用於傳輸 8Bit 字節碼的編碼方式之一,Base64 就是一種基於 64 個可打印字符來表示二進制數據的方法。可查看 RFC2045 ~ RFC2049 ,上面有MIME的詳細規範。

Base64 編碼是從二進制到字符的過程,可用於在 HTTP 環境下傳遞較長的標識信息。採用 Base64 編碼具有不可讀性,需要解碼後才能閱讀。

Base64 由於以上優點被廣泛應用於計算機的各個領域,然而由於輸出內容中包括兩個以上 “符號類” 字符(+, /, =),不同的應用場景又分別研製了 Base64 的各種 “變種”。爲統一和規範化 Base64 的輸出,Base62x 被視爲無符號化的改進版本。

引用:Base64百度百科

按照 RFC2045 的定義,Base64 被定義爲:Base64 內容傳送編碼被設計用來把任意序列的 8位字節 描述爲一種不易被人直接識別的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.)
常見於郵件、http 加密,截取 http 信息,你就會發現登錄操作的用戶名、密碼字段通過 BASE64 加密的。

BASE64 的加密解密是雙向的,可以求反解。

案例實現:

/**
 * Base64 編碼、解碼測試
 * <br/>
 */
class Base64Test {
    public static void main(String[] args) {

        // 待編碼的字符
        String originalStr = "111111";
        System.out.println(String.format("待編碼的字符: %s", originalStr));

        /* jdk 實現 */
        System.out.println("=================jdk實現=================");

        // 獲取編碼器
        Base64.Encoder encoder1 = Base64.getEncoder();

        // 獲取解碼器
        Base64.Decoder decoder1 = Base64.getDecoder();

        // 編碼測試
        byte[] encode1 = encoder1.encode(originalStr.getBytes());
        System.out.println(String.format("編碼結果:%s", new String(encode1)));

        // 解碼測試
        byte[] decode1 = decoder1.decode(encode1);
        System.out.println(String.format("解碼結果:%s", new String(decode1)));

        /* Apache commons-codec 實現 */
        System.out.println("=================Apache commons-codec 實現=================");

        // 編碼測試
        byte[] encode2 = org.apache.commons.codec.binary.Base64.encodeBase64(originalStr.getBytes());
        System.out.println(String.format("編碼結果:%s", new String(encode2)));

        // 解碼測試
        byte[] decode2 = org.apache.commons.codec.binary.Base64.decodeBase64(encode2);
        System.out.println(String.format("解碼結果:%s", new String(decode2)));

        /* Spring 實現 */
        System.out.println("------------Spring 實現------------");
        String encodeToString = Base64Utils.encodeToString(originalStr.getBytes());
        System.out.println(String.format("編碼結果:%s", encodeToString));
        System.out.println(String.format("解碼結果:%s", new String(Base64Utils.decodeFromString(encodeToString))));
    }
}

運行結果如下:

20191023215143.png


2、base62x

Base62x被視爲無符號化的改進版本。

碼雲地址:https://gitee.com/xenxin/Base62x

目前用的人不多,還沒有很好的 Java 實現,不過感覺未來會有發展。

相關文章:Base62x比Base64的編碼速度更快嗎?


3、url編碼

url編碼 是一種瀏覽器用來打包表單輸入的格式。瀏覽器從表單中獲取所有的name和其中的值 ,將它們以name/value參數編碼(移去那些不能傳送的字符,將數據排行等等)作爲URL的一部分或者分離地發給服務器。不管哪種情況,在服務器端的表單輸入格式樣子象這樣:

theName=Ichabod+Crane&gender=male&status=missing& ;headless=yes

當URL地址裏包含非西歐字符的字符串時,系統會將這些字符轉換成 application/x-www-form-urlencoded 字符串,表單裏提交時也是如此,當包含非西歐字符的字符串時,系統也會將這些字符轉換成 application/x-www-form-urlencoded 字符串。

URL編碼遵循下列規則:

每對name/value由&;符分開;每對來自表單的name/value由=符分開。如果用戶沒有輸入值給這個name,那麼這個name還是出現,只是無值。任何特殊的字符(就是那些不是簡單的七位ASCII,如漢字)將以百分符%用十六進制編碼,當然也包括象 =,&;,和 % 這些特殊的字符。其實url編碼就是一個字符ascii碼的十六進制。不過稍微有些變動,需要在前面加上“%”。比如“\”,它的ascii碼是92,92的十六進制是5c,所以“\”的url編碼就是%5c。

相關文章:


Java實現:

/**
    * URLEncoder & URLDecoder 測試
    */
public static void main(String[] args) throws UnsupportedEncodingException {
    /* 測試編碼 */
    // 將普通字符串轉換成 application/x-www-form-urlencoded 字符串 採用 UTF-8 字符集進行編碼
    String encode = URLEncoder.encode("北京大學", "UTF-8");
    System.out.println(String.format("編碼結果:%s", encode));

    /* 測試解碼 */
    //將 application/x-www-form-urlencoded 字符串轉換成普通字符串 採用 UTF-8 字符集進行解碼
    String decode = URLDecoder.decode(encode, "UTF-8");
    System.out.println(String.format("解碼結果:%s", decode));
}

運行結果如下:

20191023172337.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章