Base64編碼

簡述

Base64常用于在通常處理文本數(shù)據(jù)的場合,表示、傳輸、存儲(chǔ)一些二進(jìn)制數(shù)據(jù)。

flag meaning
DEFAULT Encoder/Decoder flag,RFC 2045
NO_PADDING Encoder flag bit to omit the padding '=' characters at the end of the output (if any).
NO_WRAP Encoder flag bit to omit all line terminators(i.e., the output will be on one long line).
CRLF Encoder flag bit to indicate lines should be terminated with a CRLF pair instead of just an LF. Has no effect if NO_WRAP is specified as well.
URL_SAFE Encoder/decoder flag bit to indicate using the "URL and filename safe" variant of Base64 (see RFC 3548 section 4) where - and _ are used in place of + and*.
NO_CLOSE Flag to pass to Base64OutputStream to indicate that it should not close the output stream it is wrapping when it itself is closed.

Base64編碼提供以下幾種flag:

flag meaning
DEFAULT Encoder/Decoder flag,RFC 2045
NO_PADDING Encoder flag bit to omit the padding '=' characters at the end of the output (if any).
NO_WRAP Encoder flag bit to omit all line terminators(i.e., the output will be on one long line).
CRLF Encoder flag bit to indicate lines should be terminated with a CRLF pair instead of just an LF. Has no effect if NO_WRAP is specified as well.
URL_SAFE Encoder/decoder flag bit to indicate using the "URL and filename safe" variant of Base64 (see RFC 3548 section 4) where - and _ are used in place of + and*.
NO_CLOSE Flag to pass to Base64OutputStream to indicate that it should not close the output stream it is wrapping when it itself is closed.

在Base64中的可打印字符包括字母A-Z、a-z、數(shù)字0-9,這樣共有62個(gè)字符,最后兩個(gè)可打印符號(hào)在不同的系統(tǒng)中而不同。

轉(zhuǎn)換的時(shí)候,將三個(gè)byte的數(shù)據(jù),先后放入一個(gè)24bit的緩沖區(qū)中,先來的byte占高位。數(shù)據(jù)不足3byte的話,于緩沖器中剩下的bit用0補(bǔ)足。然后,每次取出6(因?yàn)?6=64)個(gè)bit,按照其值選擇ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作為編碼后的輸出。不斷進(jìn)行,直到全部輸入數(shù)據(jù)轉(zhuǎn)換完成。

當(dāng)原數(shù)據(jù)長度不是3的整數(shù)倍時(shí), 如果最后剩下一個(gè)輸入數(shù)據(jù),在編碼結(jié)果后加2個(gè)“=”;如果最后剩下兩個(gè)輸入數(shù)據(jù),編碼結(jié)果后加1個(gè)“=”;如果沒有剩下任何數(shù)據(jù),就什么都不要加


base64.png

因?yàn)閁RL編碼器會(huì)把標(biāo)準(zhǔn)Base64中的“/”和“+”字符變?yōu)樾稳纭?XX”的形式,而這些“%”號(hào)在存入數(shù)據(jù)庫時(shí)還需要再進(jìn)行轉(zhuǎn)換,因?yàn)锳NSI SQL中已將“%”號(hào)用作通配符。

為解決此問題,可采用一種用于URL的改進(jìn)Base64編碼,它不在末尾填充'='號(hào),并將標(biāo)準(zhǔn)Base64中的“+”和“/”分別改成了“-”和“_”,這樣就免去了在URL編解碼和數(shù)據(jù)庫存儲(chǔ)時(shí)所要作的轉(zhuǎn)換,避免了編碼信息長度在此過程中的增加,并統(tǒng)一了數(shù)據(jù)庫、表單等處對(duì)象標(biāo)識(shí)符的格式。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Base64編碼由來 Base64最早是用來解決電子郵件的傳輸問題。 傳統(tǒng)的電子郵件是1982年定下技術(shù)規(guī)范的,詳...
    Ashton閱讀 2,700評(píng)論 0 6
  • 前言:前面學(xué)習(xí)總結(jié)了平時(shí)開發(fā)中遇見的各種數(shù)據(jù)加密方式,最終都會(huì)對(duì)加密后的二進(jìn)制數(shù)據(jù)進(jìn)行Base64編碼,起到一種二...
    博格體閱讀 1,321評(píng)論 0 1
  • 最近學(xué)Python的時(shí)候遇到了編碼問題,順帶了解了一下base64編碼,首先閱讀了咱CSDN上的一篇文章Base6...
    連命都給你了閱讀 949評(píng)論 0 3
  • 1、Base64編碼原理 下圖為Base64編碼索引表: 字符選用了"A-Z、a-z、0-9、+、/" 64個(gè)可打...
    M_JCs閱讀 1,876評(píng)論 1 9
  • 一天一天的變冷了 早上把自己在被窩縮成一小團(tuán) 昨晚說好的早餐就這么泡湯了 學(xué)校通知 網(wǎng)上選課于上午十點(diǎn)開始 根據(jù)以...
    奈良艾木閱讀 240評(píng)論 0 0

友情鏈接更多精彩內(nèi)容