Date: 2018.6.17 端午
1、參考
https://my.oschina.net/wizardpisces/blog/107445
https://www.cnblogs.com/kaituorensheng/p/4501128.html
http://help.sense.com.cn/?p=165
http://www.cnblogs.com/haq5201314/p/8437201.html
2、python下編譯py成pyc和pyo (文件加密)
將python文件.py編譯成pyc二進制文件:
python -m py_file.py
或者通過腳本運行:
import py_compile ##單個文件編譯
import compileall ##多個文件編譯
py_compile.compile('path') ##path是包括.py文件名的路徑
將python文件編譯成pyo二進制文件:
python -O -m py_file.py
什么是pyc文件?
pyc是一種二進制文件,是由py文件經過編譯后,生成的文件,是一種byte code,py文件變成pyc文件后,加載的速度有所提高,而且pyc是一種跨平台的字節碼,是由python的虛擬機來執行的,這個是類似於JAVA或者.NET的虛擬機的概念。
注意事項:pyc的內容,是跟python的版本相關的,不同版本編譯后的pyc文件是不同的,2.5編譯的pyc文件,2.4版本的 python是無法執行的。
什么是pyo文件?
pyo是優化編譯后的程序 python -O 源文件即可將源程序編譯為pyo文件
什么是pyd文件?
pyd是python的動態鏈接庫。
3、將Python文件轉成exe封裝(文件加密)
參考:https://blog.csdn.net/SoaringLee_fighting/article/details/78892876
4、采用hashlib或pycrypto模塊進行文本加密
hashlib模塊:
import hashlib
sha1 = hashlib.sha1('文本內容') #加密
osv=sha1.hexdigest()
print(osv)
bx=bytes(osv,encoding='utf-8') #轉換類型
with open('1.txt','wb') as f: #以二進制寫類型打開
f.write(bx) #寫入文件
get_sha1('')
pycrypto模塊:
from Crypto.Cipher import AES
obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
message = "The answer is no"
ciphertext = obj.encrypt(message)
>>> ciphertext
'\xd6\x83\x8dd!VT\x92\xaa`A\x05\xe0\x9b\x8b\xf1'
>>> obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
>>> obj2.decrypt(ciphertext)
'The answer is no'