重复(我没有找到答案):
https://*.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文件,现在如何将其编译为二进制文件? (使用Visual Studio)
那么,如何加密我的python代码并从C脚本解密(我可以将其编译为二进制文件,使其很难编辑)?
解决方法:
这就是我要做的:
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不是为此设计的!它的意思是开放和免费的!
如果您在此关头没有其他选择,则可能应该使用其他语言