5

The pycryptodome is working fine, but I'm getting an error message.

Here's my code:

from Crypto.Cipher import DES key = 'hello123' def pad(text): while len(text) % 8 != 0: text += '' return text des = DES.new(key, DES.MODE_ECB) text1 = 'Python is the Best Language!' padded_text = pad(text1) encrypted_text = des.encrypt(padded_text) print(encrypted_text) print(des.decrypt(encrypted_text)) 

This is my error message:

Traceback (most recent call last): File "C:\Users\Raj_7\Desktop\des.py", line 10, in des = DES.new(key, DES.MODE_ECB)

File "D:\Python\lib\site-packages\Crypto\Cipher\DES.py", line 145, in new return _create_cipher(sys.modules[name], key, mode, *args, **kwargs)

File "D:\Python\lib\site-packages\Crypto\Cipher__init__.py", line 79, in _create_cipher return modes[mode](factory, **kwargs)

File "D:\Python\lib\site-packages\Crypto\Cipher_mode_ecb.py", line 215, in _create_ecb_cipher cipher_state = factory._create_base_cipher(kwargs)

File "D:\Python\lib\site-packages\Crypto\Cipher\DES.py", line 76, in _create_base_cipher result = start_operation(c_uint8_ptr(key),

File "D:\Python\lib\site-packages\Crypto\Util_raw_api.py", line 234, in c_uint8_ptr

raise TypeError("Object type %s cannot be passed to C code" % type(data)) TypeError: Object type cannot be passed to C code

5
  • 1
    Please type in your sample code and error code here. Don't post it with image. stackoverflow.com/help/how-to-ask Commented Jan 27, 2020 at 4:19
  • Added the code , the error is not formatting correctly so its in the pic Commented Jan 27, 2020 at 4:25
  • 1
    error message must by typed in, it cannot be entered as a pic, as per SO guidelines. It's too difficult to read text in images, especially on mobile devices. Also, the ability to copy-paste relevant parts of the error message into search engines, or even StackOverflow is often a good way to find the answers to problems. Volunteers will want this ability. Read How to ask. then edit accordingly. Preceed the error message with a > at the beginning of the line to format it as quote or error message. Commented Jan 27, 2020 at 4:47
  • 1
    even if you cannot get the formatting for the error message correct, you need to type it in. Someone else can help you with the formatting, then you can look to see how they did it. Basically preceed it with a >, and adding two spaces at the end of a line will produce a line break, but keep the next line formatted. Commented Jan 27, 2020 at 4:50
  • i added the spaces current one is fine ? Commented Jan 27, 2020 at 4:58

1 Answer 1

4

It works here if I use bytes instead of strings

from Crypto.Cipher import DES def pad(text): n = len(text) % 8 return text + (b' ' * n) key = b'hello123' text1 = b'Python is the Best Language!' des = DES.new(key, DES.MODE_ECB) padded_text = pad(text1) encrypted_text = des.encrypt(padded_text) print(encrypted_text) print(des.decrypt(encrypted_text)) 
Sign up to request clarification or add additional context in comments.

3 Comments

From pycryptodome.readthedocs.io/en/latest/src/cipher/cipher.html : Plaintexts and ciphertexts (input/output) can only be bytes, bytearray or memoryview. In Python 3, you cannot pass strings. In Python 2, you cannot pass Unicode strings.
you should use '\x00' for padding, not ' '.
It works for me if I use a while loop but this method does not work for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.