base編碼是Binary-to-text encoding的一種實現方法,它能夠把二進制數據(含不可打印的字符)編碼成可打印字符序列。php
本文會不定時收錄「base全家桶」:base6四、base3二、base1六、base5八、base9一、base9二、base3六、base6二、base8五、base128等。html
------------------python
0x01 base64git
安裝:python2.7自帶github
alphabet:windows
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
padding:composer
=
使用:python2.7
import base64 c=base64.b64encode('pcat.cc') m=base64.b64decode(c) print c,m
0x02 base32函數
安裝:python2.7自帶ui
alphabet:
ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
padding:
=
使用:
import base64 c=base64.b32encode('pcat.cc') m=base64.b32decode(c) print c,m
0x03 base16
安裝:python2.7自帶
alphabet:
0123456789ABCDEF
使用:
import base64 c=base64.b16encode('pcat.cc') m=base64.b16decode(c) print c,m
注意:
當b16decode的參數含有小寫字母時,須要傳入第二個參數True
base64.b16decode('706361742e6363',True)
或者使用python2.7的.decode('hex')則無須考慮大小寫。
'706361742e6363'.decode('hex')
0x04 base58
github項目:https://github.com/keis/base58
安裝:
pip install base58
alphabet:
123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
使用:
import base58 c=base58.b58encode('pcat.cc') m=base58.b58decode(c) print c,m
0x05 base91
網址:http://base91.sourceforge.net/
github項目:https://github.com/aberaud/base91-python
安裝:
pip install base91
alphabet:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"
使用:
import base91 c=base91.encode('pcat.cc') m=base91.decode(c) print c,m
0x06 base92
github項目:https://github.com/thenoviceoof/base92
安裝:
pip install base92
alphabet:
!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}
a special denotation (an empty string):
~
使用:
import base92 c=base92.encode('pcat.cc') m=base92.decode(c) print c,m
注意:
encode,b92encode,base92_encode是同樣的,
decode,b92decode,base92_decode是同樣的。
0x07 base36
github項目:https://github.com/tonyseek/python-base36
安裝:
pip install base36
alphabet:
0123456789abcdefghijklmnopqrstuvwxyz
使用:
import base36 c=base36.loads('pcat') assert type(c)==int m=base36.dumps(c) print c,m
注意:
dumps函數接收的參數類型爲int
0x08 base62
github項目:https://github.com/suminb/base62
安裝:
pip install pybase62
alphabet:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
使用:
import base62 m=base62.decode('pcat') assert type(m)==int c=base62.encode(m) print c,m c=base62.encodebytes('pcat.cc') m=base62.decodebytes(c) print c,m
注意:
encode函數接收的參數類型爲int
0x09 base85
base85種類:
(1) github項目:https://github.com/tuupola/base85
安裝:
請先安裝PHP的Composer,再
composer require tuupola/base85
使用:
(下面代碼分別對應上面4種類型)
<?php require __DIR__ . '/vendor/autoload.php'; use Tuupola\Base85; $m='pcat.cc'; $ascii85 = new Base85([ "characters" => Base85::ASCII85, "compress.spaces" => false, "compress.zeroes" => true ]); print $ascii85->encode($m) . PHP_EOL; $adobe85 = new Base85([ "characters" => Base85::ASCII85, "compress.spaces" => false, "compress.zeroes" => true, "prefix" => "<~", "suffix" => "~>" ]); print $adobe85->encode($m) . PHP_EOL; $z85 = new Base85([ "characters" => Base85::Z85, "compress.spaces" => false, "compress.zeroes" => false ]); print $z85->encode($m) . PHP_EOL; $rfc1924 = new Base85([ "characters" => Base85::RFC1924, "compress.spaces" => false, "compress.zeroes" => false ]); print $rfc1924->encode($m) . PHP_EOL;
(2) python3中base64模塊自帶
#!/usr/bin/env python3 import base64 c=base64.a85encode(b'pcat.cc') m=base64.a85decode(c) print(c,m) c=base64.b85encode(b'pcat.cc') m=base64.b85decode(c) print(c,m)
注意:
python3中的a85encode和b85encode分別對應上面4種類型的第1和第4種。
0x0a base128
github項目:https://github.com/rpuntaie/base128
安裝:
git clone https://github.com/rpuntaie/base128 cd base128 python3 setup.py install
另外須要先安裝bitarray(在windows裏安裝麻煩)
pip install bitarray
alphabet:
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\xb5\xb6\xb7\xbc\xbd\xbe\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff
使用:
#!/usr/bin/env python3 import base128 b128=base128.base128(chars=None, chunksize=7) c=list(b128.encode(b'pcat.cc')) m=b''.join(b128.decode(c)) print(c,m)
注意:chars至少128字節,若是爲None,則使用base128.base128.defaultcharschunksize必須是7的倍數,且小於128。