base64decode(), base64.b64encode()用法
每日一个 2021-03-30
Base64编码是一种“防君子不防小人”的编码方式。广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符。
优点:速度快,ascii字符,肉眼不可理解
缺点:编码比较长,非常容易被破解,仅适用于加密非关键信息的场合
python2中进行Base64编码和解码import base64
s = ‘我是字符串’a = base64.b64encode(s)
print a
ztLKx9fWt/u0rg==
print base64.b64decode(a)
我是字符串
python3不太一样:因为3.x中字符都为unicode编码,而b64encode函数的参数为byte类型,所以必须先转码。
import base64
encodestr = base64.b64encode(‘abcr34r344r’.encode(‘utf-8’))
print(encodestr)
打印结果为
b’YWJjcjM0cjM0NHI=’结果和我们预想的有点区别,我们只想要获得YWJjcjM0cjM0NHI=,而字符串被b’'包围了。
这时肯定有人说了,用正则取出来就好了。。。别急。。。
b 表示 byte的意思,我们只要再将byte转换回去就好了。。。源码如下
import base64encodestr = base64.b64encode(‘abcr34r344r’.encode(‘utf-8’))
print(str(encodestr,‘utf-8’))
打印结果为
YWJjcjM0cjM0NHI=
例子如下:
echo '
# -*- coding: UTF-8 -*-target = "/ng8w/lib/www/_lk/foot.html"
import re
xr=open(target).read()
comp = re.compile("(.*)©.*(\n.*)")
open(target,"w").write(re.sub(comp,"\\1© Copyright 2013 - 2020 安天实验室\n\\2",xr)
)
'|pythonecho "
# -*- coding: UTF-8 -*-import json
version_file='/ng8w/conf/version.json'
data = json.load(open(version_file))
data['version']='antiy'
json.dump(data,open(version_file,'w'))"|python
/ng8w/bin/yara_analyzer.py install
echo '
files = ["/ng8w/lib/www/_lk/static/img/logo.png","/ng8w/lib/www/_lk/static/img/leadsec.png"]PNG_BASE64_DATA=""" iVBORw0KGgoAAAANSUhEUgAAAUAAAAA8CAYAAAAXB2OkAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAADQhJREFUeNrsnW+MI2Udx38Dd+CBcHN4pweKzHJwyl1ke8i/N7hdE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wLIe6ug9vnkNgLYI1Fr5A9ON54jzS5W2Ia8SPAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP8XYABNCPXGQtlV6wAAAABJRU5ErkJggg== """
import base64
png_data=base64.b64decode(PNG_BASE64_DATA)
for each in files:open(each,"w").write(png_data)
'|python