DES Encryption in python error(TypeError: Object type <class 'str'> cannot be passed to C code)

DES Encryption in python error(TypeError: Object type <class 'str'> cannot be passed to C code)

我建立了 des encryption_gui。我收到错误:

TypeError: Object type <class 'str'> cannot be passed to C code
    from Crypto.Cipher import DES
    from tkinter import *
    gui = Tk()
    
    def click_btn(event):
        print("btn click")
    
        def pad(text):
           n = len(text) % 8
           return text + (b' ' * n)
    
        t_p = textfield_e.get()
        text1 =  t_p
        t_key = textfield_key.get()
        key =  t_key
        
        
        des = DES.new(key, DES.MODE_ECB)
    
        padded_text = pad(text1)
        encrypted_text = des.encrypt(padded_text)
    
        textfield_d.insert(0,encrypted_text)
    
    
        def pad(text):
           n = len(text) % 8
           return text + (b' ' * n)
        
    
    gui.geometry('540x600')
    gui.title('PyCryptor')
    
    title_h = Label(gui,text='Encryption',font=('kanit',22))
    title_h.pack(pady=10)
    
    plant_box = Label(gui,text='Plain Text')
    plant_box.pack()
    
    textfield_e = Entry(gui,justify=CENTER,font=('font',15),relief='solid')
    textfield_e.pack(side=TOP,fill=X,padx=30,pady=10,ipady=10)
    
    key_box = Label(gui,text='Encryption Key')
    key_box.pack()
    
    textfield_key = Entry(gui,justify=CENTER,font=('font',10),relief='solid')
    textfield_key.pack(side=TOP,fill=X,padx=50,pady=10,ipady=10)
    
    btn_en = Button(gui,text='Encrypt',width=7,height=1,relief='solid',activebackground='orange',activeforeground='#fff')
    btn_en.pack(ipadx=7)
    btn_en.bind('<Button-1>',click_btn)
    
    title_h = Label(gui,text='Cipher Text',font=('kanit',22))
    title_h.pack(pady=10)
    
    textfield_d = Entry(gui,justify=CENTER,font=('font',15),relief='solid')
    textfield_d.pack(side=TOP,fill=X,padx=30,pady=20,ipady=50)
    
    
    
    gui.mainloop()

输出: 按钮点击

Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Users\PAssWORD\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
        return self.func(*args)
      File "c:\Users\PAssWORD\Music\encryt.py", line 19, in click_btn
        des = DES.new(key, DES.MODE_ECB)
      File "C:\Users\PAssWORD\AppData\Local\Programs\Python\Python39\lib\site-packages\Crypto\Cipher\DES.py", line 145, in new
        return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
      File "C:\Users\PAssWORD\AppData\Local\Programs\Python\Python39\lib\site-packages\Crypto\Cipher\__init__.py", line 79, in _create_cipher
        return modes[mode](factory, **kwargs)
      File "C:\Users\PAssWORD\AppData\Local\Programs\Python\Python39\lib\site-packages\Crypto\Cipher\_mode_ecb.py", line 216, in _create_ecb_cipher
        cipher_state = factory._create_base_cipher(kwargs)
      File "C:\Users\PAssWORD\AppData\Local\Programs\Python\Python39\lib\site-packages\Crypto\Cipher\DES.py", line 76, in _create_base_cipher
        result = start_operation(c_uint8_ptr(key),
      File "C:\Users\PAssWORD\AppData\Local\Programs\Python\Python39\lib\site-packages\Crypto\Util\_raw_api.py", line 232, in c_uint8_ptr
        raise TypeError("Object type %s cannot be passed to C code" % type(data))
    TypeError: Object type <class 'str'> cannot be passed to C code

您需要将 bytes 而不是 str 传递给 DES.new()pad(),因此更改以下行:

text1 = t_p
...
key = t_key

text1 = t_p.encode()
...
key = t_key.encode()

另外pad()函数的逻辑也不对,应该是这样的:

def pad(text):
    n = len(text) % 8
    if n:
        n = 8 - n
    return text + (b' ' * n)

你也定义了两次pad()


请注意,应使用 AES 而不是 DES