前言
很久没写点东西了,想着随便写点,大早醒来就整理了下。看了本帖,肯定会有人问,"有现成的base64模块为什么不直接拿来用,巴拉巴拉巴拉..."。本帖只是在偶然的一次爬虫经历中碰到了base64的反爬虫问题,然后顺便学习了下 Base64 的算法,根据自己的理解写了这么个玩意,拿来分享下。
废话不多说了,早上时间也不多,直接丢源码上来吧,看得懂的骚年自然看得懂,看不懂的骚年还是先把python 基础语法学习下也能看懂了:
_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def A_J(a):
i = 0
b = ""
a = A_C(a)
k = ""
while (i < len(a)):
c = ord(a[i])
i += 1
try:
chr2 = ord(a[i])
i += 1
except IndexError:
k = int(1)
i += 1
try:
chr3 = ord(a[i])
i += 1
except IndexError:
k = int(2)
i += 1
enc1 = c >> 2
enc2 = ((c & 3) << 4) | (chr2 >>4)
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
enc4 = chr3 & 63
if k == 1:
chr2 = 'none'
chr3 = 'none'
elif k == 2:
chr3 = 'none'
if not is_number(chr2):
enc3 = enc4 = 64
elif not is_number(chr3):
enc4 = 64
b = b + _keyStr[enc1] + _keyStr[enc2] + _keyStr[enc3] + _keyStr[enc4]
return b
def A_C(a):
a = a.replace('\r\n','\n')
b = ""
for n in range(0,len(a)):
c = ord(a[n])
if c < 128:
b += chr(c)
elif c > 127 and c < 2048:
b += chr((c >> 6) | 192)
b += chr((c & 63) | 128)
else:
b += chr((c >> 12) | 224)
b += chr(((c >> 6) & 63) | 128)
b += chr((c & 63) | 128)
return b
print A_J('ljljkljlajsgaslkgjslakg')
(先写这么多吧,不早了,要赶去搬砖了,暂未测试,各位有问题可以留言)
网友评论