Python 像素RGB元组列表中的truct图像 newim=Image.new(“RGB”,(int(newwidth),int(newheight))) newim.putdata(finaltextwo) newim.show() # --------------------- #GUI的东西从这里开始 # --------------------- #空密码应答器 def pass_alert(): showinfo(“密码警报”,“请输入有效密码”) def enc_成功(图像名称): showinfo(“成功”,“加密图像:”+imagename) #图像加密按钮事件 def image_open(): #现在没用,以后可能需要 全局文件路径 #检查密码输入是否为空。如果是,请提醒 enc_pass=passg.get() 如果enc_pass==“”: 通过警报() 其他: password=hashlib.sha256(enc_pass).digest() filename=askopenfilename() file\u path\u e=os.path.dirname(文件名) #加密图像 加密(文件名、密码) #图像解密按钮事件 def cipher_open(): #现在没用,以后可能需要 全局文件路径 #检查密码输入是否为空。如果是,请提醒 dec_pass=passg.get() 如果dec_pass==“”: 通过警报() 其他: password=hashlib.sha256(dec_pass).digest() filename=askopenfilename() file\u path\u d=os.path.dirname(文件名) #解密密文 解密(文件名、密码) #主gui应用程序从这里开始 类应用程序: 定义初始(自我,主): #使passg全局化以在函数中使用 全球通行证 #设置前端标题等等诸如此类的废话 title=“图像加密机” author=“” msgtitle=消息(主控,文本=标题,fg=“红色”) 配置(字体=(“Times”,“24”,“粗体斜体”),宽度=200) msgautor=消息(主控,文本=作者) msgautor.config(字体=(“helvetica”,“15”,“粗体”),宽度=200) #画画布 画布宽度=350 画布高度=100 w=画布(主, 宽度=画布宽度, 高度=画布(高度) #打包GUI,这是基本的,我们应该使用网格系统 msgtitle.pack() msgauthor.pack() w、 包() #按钮上方的密码字段 passlabel=Label(master,text=“输入加密/解密密码:”,font=30) passlabel.pack() passg=条目(主控,show=“*”,宽度=50) 密码包() #在此添加触发文件浏览器的两个加密/解密按钮 self.encrypt=按钮(主, text=“Encrypt”,fg=“red”,font=20, 命令=图像(打开,宽度=20,高度=2) self.encrypt.pack(侧面=左侧) self.decrypt=按钮(主, text=“Decrypt”,fg=“blue”,font=20, 命令=密码(打开,宽度=20,高度=2) self.decrypt.pack(侧=右) root=Tk() root.wm_标题(“图像加密机”) app=app(根目录) root.mainloop()

Python 像素RGB元组列表中的truct图像 newim=Image.new(“RGB”,(int(newwidth),int(newheight))) newim.putdata(finaltextwo) newim.show() # --------------------- #GUI的东西从这里开始 # --------------------- #空密码应答器 def pass_alert(): showinfo(“密码警报”,“请输入有效密码”) def enc_成功(图像名称): showinfo(“成功”,“加密图像:”+imagename) #图像加密按钮事件 def image_open(): #现在没用,以后可能需要 全局文件路径 #检查密码输入是否为空。如果是,请提醒 enc_pass=passg.get() 如果enc_pass==“”: 通过警报() 其他: password=hashlib.sha256(enc_pass).digest() filename=askopenfilename() file\u path\u e=os.path.dirname(文件名) #加密图像 加密(文件名、密码) #图像解密按钮事件 def cipher_open(): #现在没用,以后可能需要 全局文件路径 #检查密码输入是否为空。如果是,请提醒 dec_pass=passg.get() 如果dec_pass==“”: 通过警报() 其他: password=hashlib.sha256(dec_pass).digest() filename=askopenfilename() file\u path\u d=os.path.dirname(文件名) #解密密文 解密(文件名、密码) #主gui应用程序从这里开始 类应用程序: 定义初始(自我,主): #使passg全局化以在函数中使用 全球通行证 #设置前端标题等等诸如此类的废话 title=“图像加密机” author=“” msgtitle=消息(主控,文本=标题,fg=“红色”) 配置(字体=(“Times”,“24”,“粗体斜体”),宽度=200) msgautor=消息(主控,文本=作者) msgautor.config(字体=(“helvetica”,“15”,“粗体”),宽度=200) #画画布 画布宽度=350 画布高度=100 w=画布(主, 宽度=画布宽度, 高度=画布(高度) #打包GUI,这是基本的,我们应该使用网格系统 msgtitle.pack() msgauthor.pack() w、 包() #按钮上方的密码字段 passlabel=Label(master,text=“输入加密/解密密码:”,font=30) passlabel.pack() passg=条目(主控,show=“*”,宽度=50) 密码包() #在此添加触发文件浏览器的两个加密/解密按钮 self.encrypt=按钮(主, text=“Encrypt”,fg=“red”,font=20, 命令=图像(打开,宽度=20,高度=2) self.encrypt.pack(侧面=左侧) self.decrypt=按钮(主, text=“Decrypt”,fg=“blue”,font=20, 命令=密码(打开,宽度=20,高度=2) self.decrypt.pack(侧=右) root=Tk() root.wm_标题(“图像加密机”) app=app(根目录) root.mainloop(),python,user-interface,tkinter,encryption,pycrypto,Python,User Interface,Tkinter,Encryption,Pycrypto,它使用旧的python包。请告诉我问题出在哪里,谢谢。为了更好地理解,代码中有注释。如果几天前它运行顺利,但现在不是了,那么这与您的系统有关,而不是代码,您可能会运行更新或库更新,意外删除依赖项等…您需要知道平稳运行期间发生的事件,因此,您可以知道是哪个事件触发了代码无法像以前那样顺利运行,请检查您的系统事件日志您介意在您的系统中运行它并查看问题所在吗?我也在另一个系统中做过,但同样的问题仍然存在,不断地告诉程序在一两个小时内没有响应,最后创建了输出图像。实际上我在外面使用智能手机,啊,好的,请

它使用旧的python包。请告诉我问题出在哪里,谢谢。为了更好地理解,代码中有注释。

如果几天前它运行顺利,但现在不是了,那么这与您的系统有关,而不是代码,您可能会运行更新或库更新,意外删除依赖项等…您需要知道平稳运行期间发生的事件,因此,您可以知道是哪个事件触发了代码无法像以前那样顺利运行,请检查您的系统事件日志您介意在您的系统中运行它并查看问题所在吗?我也在另一个系统中做过,但同样的问题仍然存在,不断地告诉程序在一两个小时内没有响应,最后创建了输出图像。实际上我在外面使用智能手机,啊,好的,请在接下来的5个小时内随时帮我,我可以等待。需要这么多代码吗?你能展示一下触发行为的部分吗?
from Tkinter import *
from tkFileDialog import *
import tkMessageBox
import os
import PIL
import math
from PIL import Image
from Crypto.Cipher import AES
import hashlib
import binascii
import base64

global password  # make pass global var


# encryption method
# -----------------
def encrypt(imagename, password):
    # initialize variables
    plaintext = list()
    plaintextstr = ""

    # load the image
    im = Image.open(imagename)  # open target image
    pix = im.load()

    # print im.size   # print size of image (width,height)
    width = im.size[0]
    height = im.size[1]

    # break up the image into a list, each with pixel values and then append to a string
    for y in range(0, height):
        # print("Row: %d") %y  # print row number
        for x in range(0, width):
            # print pix[x,y]  # print each pixel RGB tuple
            plaintext.append(pix[x, y])

    # add 100 to each tuple value to make sure each are 3 digits long.  being able to do this is really just a PoC
    # that you'll be able to use a raw application of RSA to encrypt, rather than PyCrypto if you wanted.
    for i in range(0, len(plaintext)):
        for j in range(0, 3):
            plaintextstr = plaintextstr + "%d" % (int(plaintext[i][j]) + 100)

    # length save for encrypted image reconstruction
    relength = len(plaintext)

    # append dimensions of image for reconstruction after decryption
    plaintextstr += "h" + str(height) + "h" + "w" + str(width) + "w"

    # make sure that plantextstr length is a multiple of 16 for AES.  if not, append "n".  not safe in theory
    # and i should probably replace this with an initialization vector IV = 16 * '\x00' at some point.  In practice
    # this IV buffer should be random.
    while (len(plaintextstr) % 16 != 0):
        plaintextstr = plaintextstr + "n"

    # encrypt plaintext
    obj = AES.new(password, AES.MODE_CBC, 'This is an IV456')
    ciphertext = obj.encrypt(plaintextstr)

    # write ciphertext to file for analysis
    cipher_name = imagename + ".crypt"
    g = open(cipher_name, 'w')
    base64_ciphertext = base64.b64encode(ciphertext)
    g.write(base64_ciphertext)

    # -----------------
    # construct encrypted image (not currently using since Tkinter isn't very nice)
    # -----------------
    def construct_enc_image():
        # hexlify the ciphertext
        asciicipher = binascii.hexlify(ciphertext)

        # replace function
        def replace_all(text, dic):
            for i, j in dic.iteritems():
                text = text.replace(i, j)
            return text

        # use replace function to replace ascii cipher characters with numbers
        reps = {'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5', 'f': '6', 'g': '7', 'h': '8', 'i': '9', 'j': '10',
                'k': '11', 'l': '12', 'm': '13', 'n': '14', 'o': '15', 'p': '16', 'q': '17', 'r': '18', 's': '19',
                't': '20', 'u': '21', 'v': '22', 'w': '23', 'x': '24', 'y': '25', 'z': '26'}
        asciiciphertxt = replace_all(asciicipher, reps)

        # construct encrypted image
        step = 3
        encimageone = [asciiciphertxt[i:i + step] for i in range(0, len(asciiciphertxt), step)]
        # if the last pixel RGB value is less than 3-digits, add a digit a 1
        if int(encimageone[len(encimageone) - 1]) < 100:
            encimageone[len(encimageone) - 1] += "1"
        # check to see if we can divide the string into partitions of 3 digits.  if not, fill in with some garbage RGB values
        if len(encimageone) % 3 != 0:
            while (len(encimageone) % 3 != 0):
                encimageone.append("101")

        encimagetwo = [(int(encimageone[int(i)]), int(encimageone[int(i + 1)]), int(encimageone[int(i + 2)])) for i in
                       range(0, len(encimageone), step)]

        # make sizes of images equal
        while (int(relength) != len(encimagetwo)):
            encimagetwo.pop()

        # encrypted image
        encim = Image.new("RGB", (int(width), int(height)))
        encim.putdata(encimagetwo)

        encim.show()
        # alert success and path to image
        enc_success(cipher_name)

    construct_enc_image()
# decryption method
# -----------------
def decrypt(ciphername, password):
    # reach ciphertext into memory
    cipher = open(ciphername, 'r')
    ciphertext = cipher.read()
    denc = base64.b64decode(ciphertext)

    # decrypt ciphertext with password
    obj2 = AES.new(password, AES.MODE_CBC, 'This is an IV456')
    decrypted = obj2.decrypt(denc)

    # parse the decrypted text back into integer string
    decrypted = decrypted.replace("n", "")

    # extract dimensions of images
    newwidth = decrypted.split("w")[1]
    newheight = decrypted.split("h")[1]

    # replace height and width with emptyspace in decrypted plaintext
    heightr = "h" + str(newheight) + "h"
    widthr = "w" + str(newwidth) + "w"
    decrypted = decrypted.replace(heightr, "")
    decrypted = decrypted.replace(widthr, "")

    # reconstruct the list of RGB tuples from the decrypted plaintext
    step = 3
    finaltextone = [decrypted[i:i + step] for i in range(0, len(decrypted), step)]
    finaltexttwo = [
        (int(finaltextone[int(i)]) - 100, int(finaltextone[int(i + 1)]) - 100, int(finaltextone[int(i + 2)]) - 100) for
        i in range(0, len(finaltextone), step)]

    # reconstruct image from list of pixel RGB tuples
    newim = Image.new("RGB", (int(newwidth), int(newheight)))
    newim.putdata(finaltexttwo)
    newim.show()


# ---------------------
# GUI stuff starts here
# ---------------------

# empty password aler
def pass_alert():
    tkMessageBox.showinfo("Password Alert", "Please enter a Valid password.")


def enc_success(imagename):
    tkMessageBox.showinfo("Success", "Encrypted Image: " + imagename)


# image encrypt button event
def image_open():
    # useless for now, may need later
    global file_path_e

    # check to see if password entry is null.  if yes, alert
    enc_pass = passg.get()
    if enc_pass == "":
        pass_alert()
    else:
        password = hashlib.sha256(enc_pass).digest()
        filename = askopenfilename()
        file_path_e = os.path.dirname(filename)
        # encrypt the image
        encrypt(filename, password)


# image decrypt button event
def cipher_open():
    # useless for now, may need later
    global file_path_d

    # check to see if password entry is null.  if yes, alert
    dec_pass = passg.get()
    if dec_pass == "":
        pass_alert()
    else:
        password = hashlib.sha256(dec_pass).digest()
        filename = askopenfilename()
        file_path_d = os.path.dirname(filename)
        # decrypt the ciphertext
        decrypt(filename, password)


# main gui app starts here
class App:
    def __init__(self, master):
        # make passg global to use in functions
        global passg
        # setup frontend titles etc blah blah
        title = "   Image Encryptor"
        author = ""
        msgtitle = Message(master, text=title, fg="red")
        msgtitle.config(font=("Times", "24", "bold italic"), width=200)
        msgauthor = Message(master, text=author)
        msgauthor.config(font=("helvetica", "15", "bold"), width=200)

        # draw canvas
        canvas_width = 350
        canvas_height = 100
        w = Canvas(master,
                   width=canvas_width,
                   height=canvas_height)

        # pack the GUI, this is basic, we shold use a grid system
        msgtitle.pack()
        msgauthor.pack()
        w.pack()

        # password field here above buttons
        passlabel = Label(master, text="Enter Encryption/Decryption Password:", font=30)
        passlabel.pack()
        passg = Entry(master, show="*", width=50)
        passg.pack()

        # add both encrypt/decrypt buttons here which trigger file browsers
        self.encrypt = Button(master,
                              text="Encrypt", fg="red", font=20,
                              command=image_open, width=20, height=2)
        self.encrypt.pack(side=LEFT)
        self.decrypt = Button(master,
                              text="Decrypt", fg="blue", font=20,
                              command=cipher_open, width=20, height=2)
        self.decrypt.pack(side=RIGHT)


root = Tk()
root.wm_title("Image Encryptor")
app = App(root)
root.mainloop()