副本(我没有在其中找到答案):https://stackoverflow.com/questions/4066361/how-to-obfuscate-python-code How do I protect Python code?
因此,我已经查看了^^上面的这两个链接,并且没有发现任何对实际加密python脚本和/或混淆python代码有用的东西。因此,我对C是新手,但在python方面有经验,如果我想开发商业python项目,我最好的想法是:
创建c脚本和加密编译的python脚本C脚本只需提供一个字符串加密密钥并对其进行解密。我从来没有尝试过加密,我知道这并不完美。但我不需要完美。我只想让我的python源代码更难解压缩,因为我意识到这仍然很容易,但并不像那么简单。
我现在已经查看了Cython,我可以轻松地生成一个*.c文件,现在我如何将它编译成二进制文件呢?(与视听演播室)
那么,我如何加密我的python代码并从一个C脚本(我可以编译成二进制代码,这大大增加了编辑难度)解密它?
发布于 2014-02-18 21:17:37
我会这样做:
1)创建C脚本,生成密钥并将其存储到文本文件中
2)取起键,在运行Python时立即删除文本文件
3)使用密钥解密Python代码中非常重要的部分(确保没有这些比特会破坏脚本),然后全部导入
4)立即重新加密重要的Python位,并删除.pyc文件。
这是可以战胜的,但你可以接受。
要加密和重新加密您的python位,请尝试以下代码:
from hashlib import md5
from Crypto.Cipher import AES
from Crypto import Random
def encrypt(in_file, out_file, password, key_length=32):
bs = AES.block_size
salt = Random.new().read(bs - len('Salted__'))
key, iv = derive_key_and_iv(password, salt, key_length, bs)
cipher = AES.new(key, AES.MODE_CBC, iv)
out_file.write('Salted__' + salt)
finished = False
while not finished:
chunk = in_file.read(1024 * bs)
if len(chunk) == 0 or len(chunk) % bs != 0:
padding_length = (bs - len(chunk) % bs) or bs
chunk += padding_length * chr(padding_length)
finished = True
out_file.write(cipher.encrypt(chunk))
def decrypt(in_file, out_file, password, key_length=32):
bs = AES.block_size
salt = in_file.read(bs)[len('Salted__'):]
key, iv = derive_key_and_iv(password, salt, key_length, bs)
cipher = AES.new(key, AES.MODE_CBC, iv)
next_chunk = ''
finished = False
while not finished:
chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs))
if len(next_chunk) == 0:
padding_length = ord(chunk[-1])
chunk = chunk[:-padding_length]
finished = True
out_file.write(chunk)总之,下面是一些伪代码:
def main():
os.system("C_Executable.exe")
with open("key.txt",'r') as f:
key = f.read()
os.remove("key.txt")
#Calls to decrpyt files which look like this:
with open("Encrypted file name"), 'rb') as in_file, open("unecrypted file name"), 'wb') as out_file:
decrypt(in_file, out_file, key)
os.remove("encrypted file name")
import fileA, fileB, fileC, etc
global fileA, fileB, fileC, etc
#Calls to re-encrypt files and remove unencrypted versions along with .pyc files using a similar scheme to decryption calls
#Whatever else you want但为了强调和重点,
Python不是为这个而设计的!它意味着开放和自由!
如果你发现自己在这个关头没有其他选择,你也许应该使用另一种语言。
发布于 2014-02-18 20:47:16
看一下努伊卡项目。它是一个python编译器,它将您的python脚本编译成使用libpython运行的本地可执行代码。
http://nuitka.net/
发布于 2014-02-19 16:11:20
你没有解释为什么你觉得需要加密/解密。答案可能会实质性地改变所提出的任何建议。
例如,让我们假设您试图保护知识产权,但喜欢在python中编写代码的方便性。如果这是您的动机,请考虑cython--http://cython.org。
但是,假设您更关心安全性(即:防止某人未经用户许可修改您的代码)。在这种情况下,您可以考虑某种嵌入式加载程序,在调用嵌入式python解释器之前检查您的python源代码。
我相信你可能还需要加密的其他原因有1/2。
https://stackoverflow.com/questions/21864682
复制相似问题