Base64是網路上最常見的用於傳輸8Bit位元組碼的編碼方式之一,Base64就是一種基於64個可列印字元來表示二進制數據的方法。可查看RFC2045~RFC2049,上面有MIME的詳細規範。
Base64編碼是從二進制到字元的過程,可用於在HTTP環境下傳遞較長的標識信息。例如,在Java Persistence系統Hibernate中,就採用了Base64來將一個較長的唯一標識符(一般為128-bit的UUID)編碼為一個字元串,用作HTTP表單和HTTP GET URL中的參數。在其他應用程式中,也常常需要把二進制數據編碼為適合放在URL(包括隱藏表單域)中的形式。此時,採用Base64編碼具有不可讀性,需要解碼後才能閱讀。
Base64由於以上優點被廣泛套用於計算機的各個領域,然而由於輸出內容中包括兩個以上“符號類”字元(+, /, =),不同的套用場景又分別研製了Base64的各種“變種”。為統一和規範化Base64的輸出,Base62x被視為無符號化的改進版本。
基本介紹
原理
先以“迅雷下載”為例: 很多下載類網站都提供“迅雷下載”的連結,其地址通常是加密的迅雷專用下載地址。
套用
簡介
規則
例子(1)
索引 | 對應字元 | 索引 | 對應字元 | 索引 | 對應字元 | 索引 | 對應字元 |
0 | A | 17 | R | 34 | i | 51 | z |
1 | B | 18 | S | 35 | j | 52 | 0 |
2 | C | 19 | T | 36 | k | 53 | 1 |
3 | D | 20 | U | 37 | l | 54 | 2 |
4 | E | 21 | V | 38 | m | 55 | 3 |
5 | F | 22 | W | 39 | n | 56 | 4 |
6 | G | 23 | X | 40 | o | 57 | 5 |
7 | H | 24 | Y | 41 | p | 58 | 6 |
8 | I | 25 | Z | 42 | q | 59 | 7 |
9 | J | 26 | a | 43 | r | 60 | 8 |
10 | K | 27 | b | 44 | s | 61 | 9 |
11 | L | 28 | c | 45 | t | 62 | + |
12 | M | 29 | d | 46 | u | 63 | / |
13 | N | 30 | e | 47 | v | ||
14 | O | 31 | f | 48 | w | ||
15 | P | 32 | g | 49 | x | ||
16 | Q | 33 | h | 50 | y |
例子(2)
代碼實現
JavaScript版
if (!Shotgun) var Shotgun = {};if (!Shotgun.Js) Shotgun.Js = {};Shotgun.Js.Base64 = { _table: [ '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', '+', '/' ], encode: function (bin) { var codes = []; var un = 0; un = bin.length % 3; if (un == 1) bin.push(0, 0); else if (un == 2) bin.push(0); for (var i = 2; i < bin.length; i += 3) { var c = bin[i - 2] << 16; c |= bin[i - 1] << 8; c |= bin[i]; codes.push(this._table[c >> 18 & 0x3f]); codes.push(this._table[c >> 12 & 0x3f]); codes.push(this._table[c >> 6 & 0x3f]); codes.push(this._table[c & 0x3f]); } if (un >= 1) { codes[codes.length - 1] = "="; bin.pop(); } if (un == 1) { codes[codes.length - 2] = "="; bin.pop(); } return codes.join(""); }, decode: function (base64Str) { var i = 0; var bin = []; var x = 0, code = 0, eq = 0; while (i < base64Str.length) { var c = base64Str.charAt(i++); var idx = this._table.indexOf(c); if (idx == -1) { switch (c) { case '=': idx = 0; eq++; break; case ' ': case '\n': case "\r": case '\t': continue; default: throw { "message": "\u0062\u0061\u0073\u0065\u0036\u0034\u002E\u0074\u0068\u0065\u002D\u0078\u002E\u0063\u006E\u0020\u0045\u0072\u0072\u006F\u0072\u003A\u65E0\u6548\u7F16\u7801\uFF1A" + c }; } } if (eq > 0 && idx != 0) throw { "message": "\u0062\u0061\u0073\u0065\u0036\u0034\u002E\u0074\u0068\u0065\u002D\u0078\u002E\u0063\u006E\u0020\u0045\u0072\u0072\u006F\u0072\u003A\u7F16\u7801\u683C\u5F0F\u9519\u8BEF\uFF01" }; code = code << 6 | idx; if (++x != 4) continue; bin.push(code >> 16); bin.push(code >> 8 & 0xff); bin.push(code & 0xff) code = x = 0; } if (code != 0) throw { "message": "\u0062\u0061\u0073\u0065\u0036\u0034\u002E\u0074\u0068\u0065\u002D\u0078\u002E\u0063\u006E\u0020\u0045\u0072\u0072\u006F\u0072\u003A\u7F16\u7801\u6570\u636E\u957F\u5EA6\u9519\u8BEF" }; if (eq == 1) bin.pop(); else if (eq == 2) { bin.pop(); bin.pop(); } else if (eq > 2) throw { "message": "\u0062\u0061\u0073\u0065\u0036\u0034\u002E\u0074\u0068\u0065\u002D\u0078\u002E\u0063\u006E\u0020\u0045\u0072\u0072\u006F\u0072\u003A\u7F16\u7801\u683C\u5F0F\u9519\u8BEF\uFF01" }; return bin; }};
BASH版
base64Table=(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 + /);function str2binary() { idx=0; for((i=0; i<${#str}; i++)); do dividend=$(printf "%d" "'${str:i:1}"); for((j=0;j<8;j++)); do let idx=8*i+7-j; let bin[$idx]=$dividend%2; dividend=$dividend/2; done; done; let idx=${#str}*8; for((i=0; i<appendEqualCnt*2; i++)); do let bin[$idx]=0; let idx++; done;}function calcBase64() { for((i=0; i<${#bin[*]}/6; i++)); do sum=0; for((j=0; j<6; j++)); do let idx=i*6+j; let n=6-1-j; let sum=sum+${bin[$idx]}*2**n; done; echo -n ${base64Table[$sum]}; done}declare -a binfunction base64Encode() { read -p "please enter ASCII string:" str; let appendZero=${#str}*8%6; let bits=${#str}*8; appendEqualCnt=0; if [[ $appendZero -ne 0 ]]; then let appendEqualCnt=(6-$appendZero)/2; fi str2binary; calcBase64; if [[ $appendEqualCnt -eq 2 ]]; then echo -n "=="; elif [[ $appendEqualCnt -eq 1 ]]; then echo -n "="; fi echo; }
Java版
import java.util.Base64;對於標準的Base64:加密為字元串使用Base64.getEncoder().encodeToString();加密為位元組數組使用Base64.getEncoder().encode();解密使用Base64.getDecoder().decode();對於URL安全或MIME的Base64,只需將上述getEncoder()getDecoder()更換為getUrlEncoder()getUrlDecoder()或getMimeEncoder()和getMimeDecoder()即可。
PHP版
<?php$txt1=trim($_POST['text1']);$txt2=trim($_POST['text2']);$txt3=trim($_POST['text3']);$button=$_POST['button'];?><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN"><html><head><title>迅雷和FlashGet,QQ旋風地址地址轉換工具</title><metahttp-equiv="Content-Type"content="text/html;charset=gb2312"><metacontent="迅雷,FlashGet,地址轉換,"name="keywords"></head><body><formname="form1"method="post"action=""><hrsize="1"><h3>迅雷轉換</h3><P>轉換地址:<inputname="text1"value="<?phpecho$txt1;?>"type="text"style="width:516px;"/></P><P>轉換後的:<inputtype="text"value="<?phpif($button=="普通地址->迅雷地址")echo"thunder://".base64_encode("AA".$txt1."ZZ");if($button=="迅雷地址->普通地址")echosubstr(base64_decode(str_ireplace("thunder://","",$txt1)),2,-2);?>"style="width:516px;"/></P><P><inputtype="submit"name="button"value="普通地址->迅雷地址"/><inputtype="submit"name="button"value="迅雷地址->普通地址"/></P><h3>FlashGet轉換</h3><P>FlashGet地址:<inputname="text2"value="<?phpecho$txt2;?>"type="text"style="width:516px;"/></P><P>轉換後地址:<inputtype="text"value="<?phpif($button=="普通地址->FlashGet地址")echo"flashget://".base64_encode($txt2);if($button=="FlashGet地址->普通地址")echostr_ireplace("[FLASHGET]","",base64_decode(str_ireplace("flashget://","",$txt2)));?>"style="width:516px;"/></P><P><inputtype="submit"value="普通地址->FlashGet地址"name="button"/><inputtype="submit"value="FlashGet地址->普通地址"name="button"/></P><h3>QQ旋風轉換</h3><P>QQ旋風地址:<inputname="text3"value="<?phpecho$txt3;?>"type="text"style="width:516px;"/></P><P>轉換後地址:<inputtype="text"value="<?phpif($button=="普通地址->QQ旋風")echo"qqdl://".base64_encode($txt3);if($button=="QQ旋風->普通地址")echobase64_decode(str_ireplace("qqdl://","",$txt3));?>"style="width:516px;"/></P><P><inputtype="submit"value="普通地址->QQ旋風"name="button"/><inputtype="submit"value="QQ旋風->普通地址"name="button"/></P></form></body></html>
VB版
Option Explicit'B as e64Encoding/DecodingAlgorithm'By:DavidMidkif f([email protected])''Thisalgorithmsencodes and decodesdatain to B as e64'for mat.Thisfor matisextremelymoreefficientthan'Hexadecimalencoding.Private m_bytIndex(0 To 63) As BytePrivate m_bytReverseIndex(0 To 255) As BytePrivate Const k_bytEqualSign As Byte = 61Private Const k_bytmask1 As Byte = 3Private Const k_bytmask2 As Byte = 15Private Const k_bytmask3 As Byte = 63Private Const k_bytmask4 As Byte = 192Private Const k_bytmask5 As Byte = 240Private Const k_bytmask6 As Byte = 252Private Const k_bytShift2 As Byte = 4Private Const k_bytShift4 As Byte = 16Private Const k_bytShift6 As Byte = 64Private Const k_lMaxBytesPerLine As Long = 152Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)Public Function Decode64(sInput As String) As StringIf sInput = "" Then Exit FunctionDecode64 = StrConv(DecodeArray64(sInput), vbUnicode)End FunctionPrivate Function DecodeArray64(sInput As String) As Byte()Dim bytInput() As ByteDim bytWorkspace() As ByteDim bytResult() As ByteDim lInputCounter As LongDim lWorkspaceCounter As LongbytInput = Replace(Replace(sInput, vbCrLf, ""), "=", "")ReDim bytWorkspace(LBound(bytInput) To (UBound(bytInput) * 2)) As BytelWorkspaceCounter = LBound(bytWorkspace)For lInputCounter = LBound(bytInput) To UBound(bytInput) bytInput(lInputCounter) = m_bytReverseIndex(bytInput(lInputCounter))Next lInputCounterFor lInputCounter = LBound(bytInput) To (UBound(bytInput) - ((UBound(bytInput) Mod 8) + 8)) Step 8 bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) \ k_bytShift4) bytWorkspace(lWorkspaceCounter + 1) = ((bytInput(lInputCounter + 2) And k_bytmask2) * k_bytShift4) + (bytInput(lInputCounter + 4) \ k_bytShift2) bytWorkspace(lWorkspaceCounter + 2) = ((bytInput(lInputCounter + 4) And k_bytmask1) * k_bytShift6) + bytInput(lInputCounter + 6) lWorkspaceCounter = lWorkspaceCounter + 3Next lInputCounterSelect Case (UBound(bytInput) Mod 8): Case 3: bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) \ k_bytShift4) Case 5: bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) \ k_bytShift4) bytWorkspace(lWorkspaceCounter + 1) = ((bytInput(lInputCounter + 2) And k_bytmask2) * k_bytShift4) + (bytInput(lInputCounter + 4) \ k_bytShift2) lWorkspaceCounter = lWorkspaceCounter + 1 Case 7: bytWorkspace(lWorkspaceCounter) = (bytInput(lInputCounter) * k_bytShift2) + (bytInput(lInputCounter + 2) \ k_bytShift4) bytWorkspace(lWorkspaceCounter + 1) = ((bytInput(lInputCounter + 2) And k_bytmask2) * k_bytShift4) + (bytInput(lInputCounter + 4) \ k_bytShift2) bytWorkspace(lWorkspaceCounter + 2) = ((bytInput(lInputCounter + 4) And k_bytmask1) * k_bytShift6) + bytInput(lInputCounter + 6) lWorkspaceCounter = lWorkspaceCounter + 2End SelectReDim bytResult(LBound(bytWorkspace) To lWorkspaceCounter) As ByteIf LBound(bytWorkspace) = 0 Then lWorkspaceCounter = lWorkspaceCounter + 1 CopyMemoryVarPtr (bytResult(LBound(bytResult))), VarPtr(bytWorkspace(LBound(bytWorkspace))), lWorkspaceCounter DecodeArray64 = bytResultEnd FunctionPublic Function Encode64(ByRefsInput As String) As StringIf sInput = "" Then Exit FunctionDim bytTemp() As BytebytTemp = StrConv(sInput, vbFromUnicode)Encode64 = EncodeArray64(bytTemp)End FunctionPrivate Function EncodeArray64(ByRefbytInput() As Byte) As StringOn Error GoTo ErrorHandlerDim bytWorkspace() As Byte, bytResult() As ByteDim bytCrLf(0 To 3) As Byte, lCounter As LongDim lWorkspaceCounter As Long, lLineCounter As LongDim lCompleteLines As Long, lBytesRemaining As LongDim lpWorkSpace As Long, lpResult As LongDim lpCrLf As LongIf UBound(bytInput) < 1024 Then ReDim bytWorkspace(LBound(bytInput) To (LBound(bytInput) + 4096)) As ByteElse ReDim bytWorkspace(LBound(bytInput) To (UBound(bytInput) * 4)) As ByteEnd IflWorkspaceCounter = LBound(bytWorkspace)For lCounter = LBound(bytInput) To (UBound(bytInput) - ((UBound(bytInput) Mod 3) + 3)) Step 3 bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) \ k_bytShift2)) bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex(((bytInput(lCounter) And k_bytmask1) * k_bytShift4) + ((bytInput(lCounter + 1)) \ k_bytShift4)) bytWorkspace(lWorkspaceCounter + 4) = m_bytIndex(((bytInput(lCounter + 1) And k_bytmask2) * k_bytShift2) + (bytInput(lCounter + 2) \ k_bytShift6)) bytWorkspace(lWorkspaceCounter + 6) = m_bytIndex(bytInput(lCounter + 2) And k_bytmask3) lWorkspaceCounter = lWorkspaceCounter + 8Next lCounterSelect Case (UBound(bytInput) Mod 3): Case 0: bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) \ k_bytShift2)) bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex((bytInput(lCounter) And k_bytmask1) * k_bytShift4) bytWorkspace(lWorkspaceCounter + 4) = k_bytEqualSign bytWorkspace(lWorkspaceCounter + 6) = k_bytEqualSign Case 1: bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) \ k_bytShift2)) bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex(((bytInput(lCounter) And k_bytmask1) * k_bytShift4) + ((bytInput(lCounter + 1)) \ k_bytShift4)) bytWorkspace(lWorkspaceCounter + 4) = m_bytIndex((bytInput(lCounter + 1) And k_bytmask2) * k_bytShift2) bytWorkspace(lWorkspaceCounter + 6) = k_bytEqualSign Case 2: bytWorkspace(lWorkspaceCounter) = m_bytIndex((bytInput(lCounter) \ k_bytShift2)) bytWorkspace(lWorkspaceCounter + 2) = m_bytIndex(((bytInput(lCounter) And k_bytmask1) * k_bytShift4) + ((bytInput(lCounter + 1)) \ k_bytShift4)) bytWorkspace(lWorkspaceCounter + 4) = m_bytIndex(((bytInput(lCounter + 1) And k_bytmask2) * k_bytShift2) + ((bytInput(lCounter + 2)) \ k_bytShift6)) bytWorkspace(lWorkspaceCounter + 6) = m_bytIndex(bytInput(lCounter + 2) And k_bytmask3)End SelectlWorkspaceCounter = lWorkspaceCounter + 8If lWorkspaceCounter <= k_lMaxBytesPerLine Then EncodeArray64 = Left$(bytWorkspace, InStr(1, bytWorkspace, Chr$(0)) - 1)Else bytCrLf(0) = 13 bytCrLf(1) = 0 bytCrLf(2) = 10 bytCrLf(3) = 0ReDim bytResult(LBound(bytWorkspace) To UBound(bytWorkspace))lpWorkSpace = VarPtr(bytWorkspace(LBound(bytWorkspace)))lpResult = VarPtr(bytResult(LBound(bytResult)))lpCrLf = VarPtr(bytCrLf(LBound(bytCrLf)))lCompleteLines = Fix(lWorkspaceCounter / k_lMaxBytesPerLine)For lLineCounter = 0 To lCompleteLines CopyMemorylpResult , lpWorkSpace, k_lMaxBytesPerLine lpWorkSpace = lpWorkSpace + k_lMaxBytesPerLine lpResult = lpResult + k_lMaxBytesPerLine CopyMemorylpResult , lpCrLf, 4& lpResult = lpResult + 4&Next lLineCounterlBytesRemaining = lWorkspaceCounter - (lCompleteLines * k_lMaxBytesPerLine)If lBytesRemaining > 0 Then CopyMemorylpResult , lpWorkSpace, lBytesRemaining EncodeArray64 = Left$(bytResult, InStr(1, bytResult, Chr$(0)) - 1)End IfExit FunctionErrorHandler: Er As ebytResult EncodeArray64 = bytResultEnd Function
C#版
直接使用.NET中的的庫類函式方法:///<summary>///Base64加密///</summary>///<paramname="Message"></param>///<returns></returns>publicstringBase64Code(stringMessage){byte[]bytes=Encoding.Default.GetBytes(Message);returnConvert.ToBase64String(bytes);}///<summary>///Base64解密///</summary>///<paramname="Message"></param>///<returns></returns>publicstringBase64Decode(stringMessage){byte[]bytes=Convert.FromBase64String(Message);returnEncoding.Default.GetString(bytes);}
python版
def base(string:str)->str: oldstr = '' newstr = [] base = '' base64_list = ['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', '+', '/'] #把原始字元串轉換為二進制,用bin轉換後是0b開頭的,所以把b替換了,首位補0補齊8位 for i in string: oldstr += '{:08}'.format(int(str(bin(ord(i))).replace('0b', ''))) #把轉換好的二進制按照6位一組分好,最後一組不足6位的後面補0 for j in range(0, len(oldstr), 6): newstr.append('{:<06}'.format(oldstr[j:j + 6])) #在base_list中找到對應的字元,拼接 for l in range(len(newstr)): base += base64_list[int(newstr[l], 2)] #判斷base字元結尾補幾個‘=’ if len(string) % 3 == 1: base += '==' elif len(string) % 3 == 2: base += '=' return base