python中的DES加密错误(TypeError:对象类型<class'str'>不能传递给C代码)

Posted

技术标签:

【中文标题】python中的DES加密错误(TypeError:对象类型<class\'str\'>不能传递给C代码)【英文标题】:DES Encryption in python error(TypeError: Object type <class 'str'> cannot be passed to C code)python中的DES加密错误(TypeError:对象类型<class'str'>不能传递给C代码) 【发布时间】:2021-03-16 17:08:17 【问题描述】:

我构建了 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()

输出: btn 点击

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

【问题讨论】:

尝试重新安装Crypto 【参考方案1】:

您需要将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

【讨论】:

以上是关于python中的DES加密错误(TypeError:对象类型<class'str'>不能传递给C代码)的主要内容,如果未能解决你的问题,请参考以下文章

Python实现DES加密算法的文本加密器

DES加密

python des加密

PYTHON实现DES加密源码

des加密算法(c/c++)

通俗易懂,十分钟读懂DES,详解DES加密算法原理,DES攻击手段以及3DES原理。Python DES实现源码