0

These are the decryption functions.

def decrypt(info,key):
    msg=info
    PAD="%"
    decipher=AES.new(new_pwd,AES.MODE_ECB)
    pt=decipher.decrypt(msg).decode('utf-8')  # ERROR OCCURS HERE
    pad_index=pt.find(PAD)
    result=pt[:pad_index]
    return result

def decrypt_file(filename,new_pwd):
    with open(filename, 'r') as f:
        ciphertext=f.read()
    dec=decrypt(ciphertext, new_pwd)
    with open(filename[:-4], 'w') as f:
         f.write(dec)

This is the error I got when I run the code:

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "first.py", line 85, in decrypt_text_file
    decrypt_file(fname,new_pwd)
  File "first.py", line 63, in decrypt_file
    dec=decrypt(ciphertext, new_pwd)
  File "first.py", line 29, in decrypt
    pt=decipher.decrypt(msg).decode('utf-8')
  File "C:\Users\aravi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\Crypto\Cipher\_mode_ecb.py", line 190, in decrypt
    c_uint8_ptr(ciphertext),
  File "C:\Users\aravi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\Crypto\Util\_raw_api.py", line 243, 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

I don't have clue about fixing this.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • The ciphertext seems to be loaded as string (`r`), `decrypt` expects a bytes-like object. How is the ciphertext stored? If it's stored in binary format, it must be loaded with `rb`, see [here](https://stackoverflow.com/a/9644285/9014097). – Topaco Oct 24 '20 at 19:02

1 Answers1

0

.decrypt() expects bytes instead of string.

Change the error line to:

pt=decipher.decrypt(msg.encode()).decode('utf-8')

PGR
  • 116
  • 6