b32&64encode详解
以前只是用这些函数,从没想过这些编码的原理;还是因为一道ctf题让我好好的了解了一下这些编码方式!
一、b32encode
我们知道每个字节是8比特,b32encode将5个字节分成8块,每块前三位补0,从而将5个字节扩展为8个字节;那么扩展后的每个字节的值不超过32,与32个字符"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"对应,有时我们发现编码的字符串中还有"="这个字符,但是它是填充字符,以上就是b32encode;用Python写一个脚本来帮助理解:
from string import uppercase,digits
from base64 import b32encode
def my_b32encode(s):
base=uppercase+digits[2:8]
assert(len(s))==5
bs=''.join([bin(ord(x))[2:].rjust(8,'0') for x in s])
assert len(bs)==40
sbs=[bs[5*ind:5*(ind+1)] for ind in range(8)]
re=[base[int(x,2)] for x in sbs]
return ''.join(re)
if __name__=='__main__':
print my_b32encode('BITSC')
print b32encode('BITSC')
二、b64encode
b64encode将3个字节分成4块,每块前两位补0,从而将3个字节扩展为4个字节;那么扩展后的每个字节的值不超过64,与64个字符 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"对应,同样地,"="作为填充字符,以上就是b64encode;用Python写一个脚本来帮助理解:
from string import uppercase,lowercase,digits
from base64 import b64encode
def my_b64encode(s):
base=uppercase+lowercase+digits+'+/'
assert len(s)==3
bs=''.join([bin(ord(x))[2:].rjust(8,'0') for x in s])
assert len(bs)==24
sbs=[bs[6*ind:6*(ind+1)] for ind in range(4)]
re=[base[int(x,2)] for x in sbs]
return ''.join(re)
if __name__=='__main__':
print my_b64encode('BIT')
print b64encode('BIT')