totools.siteBase64 在線編碼解碼工具(totools 最好用的在線工具集合)
- 編碼解碼
- 加密解密
- 哈希算法
- 代碼格式化
- 語言處理
- URLEncode
- Base64
Base64 編碼或解碼的結果:
也可以選擇圖片文件來獲取它的 Base64 編碼的 DataURI 形式:
日誌記錄:
設置(為了使瀏覽器能夠記住設置,請開啟 Cookie)
什麼是base64
Base64編碼實現原理,將待編碼數據轉換成二進制數據,其中6bit為一個編碼單元,故該6bit能編碼的容量為2**6=64,這也是base64名稱來由。
Base64是網絡上最常見的用於傳輸8Bit字節碼的編碼方式之一,Base64就是一種基於64個可打印字符來表示二進制數據的方法。可查看RFC2045~RFC2049,上面有MIME的詳細規範。
Base64編碼在應用層數據傳輸應用廣泛。
Base64編碼是從二進制到字符的過程,可用於在HTTP環境下傳遞較長的標識信息。採用Base64編碼具有不可讀性,需要解碼後才能閱讀。同時Base64編碼廣泛用於加密解密領域,由於加密都是對二進制數據進行操作,所以加密結果往往都是二進制數據,無法被直視,當不做任何編碼處理,加密結果呈現在我們眼前的都是一團亂碼,Base64編碼剛好能很好的解決這個問題。
以編碼"Man"為例
文本 | M | a | n |
---|
ASCII編碼 | 77 | 97 | 110 |
---|
二進制位 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 |
---|
索引 | 19 | 22 | 5 | 46 |
---|
Base64編碼 | T | W | F | u |
---|
Base64索引表:
數值 | 字符 | | 數值 | 字符 | | 數值 | 字符 | | 數值 | 字符 |
---|
0 | A | 16 | Q | 32 | g | 48 | w |
1 | B | 17 | R | 33 | h | 49 | x |
2 | C | 18 | S | 34 | i | 50 | y |
3 | D | 19 | T | 35 | j | 51 | z |
4 | E | 20 | U | 36 | k | 52 | 0 |
5 | F | 21 | V | 37 | l | 53 | 1 |
6 | G | 22 | W | 38 | m | 54 | 2 |
7 | H | 23 | X | 39 | n | 55 | 3 |
8 | I | 24 | Y | 40 | o | 56 | 4 |
9 | J | 25 | Z | 41 | p | 57 | 5 |
10 | K | 26 | a | 42 | q | 58 | 6 |
11 | L | 27 | b | 43 | r | 59 | 7 |
12 | M | 28 | c | 44 | s | 60 | 8 |
13 | N | 29 | d | 45 | t | 61 | 9 |
14 | O | 30 | e | 46 | u | 62 | + |
15 | P | 31 | f | 47 | v | 63 | / |
語言 |
Base64 編碼 |
Base64 解碼 |
Java |
base64 = new BASE64Encoder().encode(str.getBytes()); |
str = new String(new BASE64Decoder().decodeBuffer(base64)); |
JavaScript |
base64 = btoa(str);
或
var s = CryptoJS.enc.Utf8.parse(str); base64 = CryptoJS.enc.Base64.stringify(s);
|
str = atob(base64);
或
var s = CryptoJS.enc.Base64.parse(base64);
str = s.toString(CryptoJS.enc.Utf8);
|
PHP |
$base64 = base64_encode($str); |
$str = base64_decode($base64); |
C#/.NET |
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str); base64 = System.Convert.ToBase64String(bytes); |
byte[] bytes = System.Convert.FromBase64String(base64); str = System.Text.Encoding.UTF8.GetString(bytes); |
Python |
import base64 base64 = base64.b64encode(str) |
import base64 str = base64.b64decode(base64) |
Perl |
use MIME::Base64; $base64 = encode_base64($str); |
use MIME::Base64; $str = decode_base64($base64); |
Golang |
import b64 "encoding/base64" ... base64 := b64.StdEncoding.EncodeToString([]byte(str)) |
import b64 "encoding/base64" ... str := b64.StdEncoding.DecodeString(base64) |
Ruby |
require "base64" base64 = Base64.encode64(str) |
require "base64" str = Base64.decode64(base64) |
MySQL/MariaDB |
SELECT TO_BASE64(str); |
SELECT FROM_BASE64(base64); |
PostgreSQL |
SELECT encode(str, 'base64'); |
SELECT decode(base64, 'base64'); |
Linux Shell (以 test 為例) |
$ echo test | base64 |
$ echo dGVzdAo= | base64 -d |