简体   繁体   中英

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

I build des encryption_gui. I got error:

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()

Output: btn click

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

You need to pass bytes instead of str to DES.new() and pad() , so change the following lines:

text1 = t_p
...
key = t_key

to

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

Also the logic of pad() function is incorrect, it should be like below:

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

Also you have defined pad() twice.


Note that AES should be used instead of DES .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM