我在Python 2.7中用PyCrypto编写了一个加密脚本。但是,在尝试转换为可执行文件时,Crypto不会加载,程序会立即关闭。我试过PY2EXE,Pyinstaller和CX_Freeze。我想知道如何使这项工作。谁能告诉我我的script.py文件应该如何查找Py2exe和Pyinstaller?谢谢。我的剧本:
import shutil
from Crypto import Random
from Crypto.Cipher import AES
from tkinter.filedialog import askopenfilename
from tkinter import *
import os
def pad(s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
def encrypt(message, key, key_size=256):
print("Your key for decryption is: ", key)
message = pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(message)
def decrypt(ciphertext, key):
iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
return plaintext.rstrip(b"\0")
def encrypt_file(file_name, key):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
enc = encrypt(plaintext, key)
with open(file_name + '.enc', 'wb') as fo:
fo.write(enc)
def decrypt_file(file_name, key):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
dec = decrypt(ciphertext, key)
with open(file_name[:-4], 'wb') as fo:
fo.write(dec)
key = b'\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18'
filename = askopenfilename()
file_name, file_extension = os.path.splitext(filename)
dec = "_dec"
newfile = file_name + dec
copied = ''.join((newfile, file_extension))
shutil.copy(filename, copied )
encrypt_file(filename, key)
master = Tk()
w = Message(master, text="Encrypted!", width = 100)
w.pack()
mainloop()