這裏是我會做:
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不是爲此而製作的!它意味着開放和自由!
如果在這個節骨眼上,沒有其他替代發現自己,你應該只使用不同的語言
這種慾望只會導致痛苦。解放它吧。 – zwol
如果您打算使用C腳本解密它,反正用戶最終不會通過C腳本獲得未加密的副本? –
由於您只想給好奇的讀者帶來不便,我建議使用最簡單的加密方法:將所有零置換爲零,將零置零。按0xFF對每個字節進行異或應該做到這一點。作爲額外的好處,加密方案與解密方案相同,與其他方案相比節省了50%的工作量。 – Kevin