它的意思是通过使用加密来保护python脚本。它应该使用os.urandom(40)的十六进制输出作为盐。kdf (cryptography.io)使用的是Cryptography.io(氪星)密码输入,使用的是getpass.getpass。它在my GitHub:(GitHub)上,需要同时使用Python2和3。
#!/usr/bin/env python
import sys
def exitmsg(msg):
print(msg)
input("Press ENTER to exit the script")
sys.exit()
if sys.version_info<(3,0,0):
def input(string):
return raw_input(string)
import base64
import binascii
import os
import getpass
try:
from cryptography.fernet import Fernet
except ImportError:
exitmsg("cryptography not installed install it with pip install cryptography via cmd or powershell (On Windows)")
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
print("PyLock beta v1.0.3 https://github.com/OfficialNoob/Python-Script-Locker")
salt = binascii.hexlify(os.urandom(40))
kdf = Scrypt(salt=salt,length=32,n=2**14,r=8,p=1,backend=default_backend())
loc = input("Script to use: ")
try:
fscript = open(loc)
except IOError:
exitmsg("Unable to read file")
script = fscript.read()
fscript.close
print("Can be used to overwrite your script")
sloc = input("Save as: ")
nc = '''#!/usr/bin/env python
#Made using PyLock https://github.com/OfficialNoob/Python-Script-Locker
import sys
def exitmsg(msg):
print(msg)
input("Press ENTER to exit")
sys.exit()
if sys.version_info<(3,0,0):
def input(string):
return raw_input(string)
import getpass
import base64
try:
from cryptography.fernet import Fernet
except ImportError:
exitmsg("cryptography not installed install it with pip install cryptography via cmd or powershell (On Windows)")
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
kdf = Scrypt(salt=%s,length=32,n=2**14,r=8,p=1,backend=default_backend())
try:
exec(Fernet(base64.urlsafe_b64encode(kdf.derive(getpass.getpass("Password to use: ").encode()))).decrypt(%s))
except Exception as ex:
if(type(ex).__name__ == "InvalidToken"):
exitmsg("Wrong password (-:")
print(ex)''' % (salt, Fernet(base64.urlsafe_b64encode(kdf.derive(getpass.getpass("Password to use: ").encode()))).encrypt(script.encode()))
try:
f = open(sloc,"w+")
f.write(nc)
except IOError:
exitmsg("Unable to write to file")
f.close
exitmsg("Your file has been created")发布于 2018-07-01 21:52:19
好消息是,据我所知,密码学是正确的(基于密码的密钥派生函数,随机盐)。坏消息是您的代码很难阅读,所以很难确保它是正确的。
请阅读PEP 8。这是个好建议。第一件事是看到62行,而不是一个空白行。在函数定义之间使用空行。在相关的说明中,使用带有明确定义的任务的辅助函数.避免很长的行(最多80列是个好主意),并将像Fernet(base64.urlsafe_b64encode(kdf.derive(getpass.getpass("Password to use: ").encode()))).encrypt(script.encode()))这样的长链分解成可管理的部分。因为代码很难阅读,所以不要期望得到完整的评论。
def exitmsg(msg):print(msg)输入(“按ENTER to exit") sys.exit()
不要要求用户按下键退出程序。当你没有话要说的时候,不要打印任何东西。标准输出用于程序输出,标准错误用于有用的消息。这个函数没有做任何有用的事情。将exitmsg(msg)替换为sys.exit()。
def input(string):
不要覆盖标准函数。
exitmsg("cryptography not installed install it with pip install cryptography via cmd or powershell (On Windows)")
标点符号是很重要的,你的信息是不可理解的,语言不仅仅是一堆单词。
exitmsg("Wrong password (-:")
将错误消息打印为标准错误,而不是标准输出。当出现错误时,以非零状态退出。
loc =输入(“要使用的脚本:")
不要从标准输入中读取脚本参数。从sys.argv上读出来。这就是它的目的。
尝试:主管(…)例外情况为ex: if(type(ex).__name__ == "InvalidToken"):exitmsg(“错误密码(-:")打印(Ex))
您正在报告某一类错误的“错误密码”错误。我不清楚你想用InvalidToken做什么。与其希望错误的键会导致特定类型的解析错误,不如使用已知值启动密文并检查该已知值。因为您知道密文是Python代码,所以可以使用Python注释行启动密文。
plaintext = fernet.decrypt(ciphertext)
if not ciphertext.startswith('# OfficialNoob PasswordScriptLocker ciphertext version 000000001\n'):
raise IncorrectPassword再一次,错误消息发生在标准错误上,脚本必须在错误状态为非零的情况下退出。
当然,整件事都是毫无意义的。在每次使用脚本时输入一个密码将是很烦人的。使用gpg一劳永逸地解密脚本将更加灵活(您可以使用密钥而不是密码),更方便,更安全(减少实现错误的风险)。
发布于 2019-07-12 04:00:36
fscript.close什么也不做。也许你是想打电话给fscript.close()。无论如何,打开文件的首选方法是使用with块,这样Python总是负责为您关闭文件:
try:
with open("Script to use: ") as fscript:
script = fscript.read()
except IOError:
exitmsg("Unable to read file")https://codereview.stackexchange.com/questions/197563
复制相似问题