加密/解密Python脚本用C [英] Encrypt/UnEncrypt Python Scripts in C

查看:444
本文介绍了加密/解密Python脚本用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

复制(即我还没有发现在回答):
如何进行模糊处理的Python code
如何保护的Python code?

duplicates (that I havent found answers in): How to obfuscate Python code How do I protect Python code?

所以,我已经看过上述两种^^的链接,我没有发现任何的实际加密Python脚本和/或混淆蟒蛇code有用。所以,我是新来C,但在python经历,如果我想开发商业Python项目我最好的想法是这样的:

So I have looked at both of the links above ^^ and I have found nothing useful for actually encrypting python scripts and/or obfuscating python code. So I am new to C but experienced in python, and if I wanted to develop commercial python projects my best idea was this:

创建一个C脚本,加密和放大器;编译python脚本 C脚本需要简单地提供一个字符串加密密钥和解密。仅供参考我从来没有真的试图加密的,我知道这不会是完美的。但我不需要完美的。我只是想使其更难反编译我的Python源$ C ​​$ C,意识到这将是仍然很容易但不是轻松。

create a c script and an encrypted&compiled python script The C script would need to simply supply a string encryption key and decrypt it. Just FYI ive never actually tried encryption, and I know this wouldnt be perfect. But I dont need perfect. I just want to make it harder to decompile my python source code, realizing this would be still easy but not AS easy.

我目前看用Cython,我可以很容易地生成* .c文件,现在我怎么能编译这个二进制? (与Visual Studio)

I have looked currently at Cython, and I can easily generate a *.c file, now how can I compile this to binary? (with visual studio)

所以,我怎么能加密我的Python code和C脚本进行解密(我可以编译为二进制使其显著更难编辑)?

So how could I encrypt my python code and decrypt it from a C script (which I could compile to binary making it significantly harder to edit)?

推荐答案

下面是我会做:

1)创建一个C脚本产生的关键,并将其存储到一个文本文件

1) Create a C script which produces a key and stores it to a text file

2)拿起钥匙,一旦运行的Python立即删除文本文件

2) Pick up the key and immediately delete the text file upon running Python

3)使用密钥来解密你的Python code的真正重要的部分(确保没有这些位​​会打破你的脚本)的然后将它导入所有

3) Use the key to decrypt the really important parts of your Python code (make sure that not having these bits will break your script) then import it all

4)立即重新加密重要的Python位,并删除 .pyc文件文件

4) Immediately re-encrypt the important Python bits, and delete the .pyc file

这将是不可战胜的,但你确定这一点。

This will be beatable, but you're ok with that.

要加密和重新加密你的Python位,试试这个code:

To encrypt and re-encrypt your python bits, try this code:

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)

总结一下,这里的一些伪code:

So to summarize, here's some pseudo-code:

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

但只是压力和重要的一点,

But just to stress and important point,

如果您在这个节骨眼上,没有其他替代发现自己,你应该只是用不同的语言

If you find yourself at this juncture with no other alternative, you probably should just use a different language

这篇关于加密/解密Python脚本用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆