0

So currently I have been working on a encryption and decryption program for my school to use, for their database and storing student logon passwords. I am having trouble with this error though.

TypeError: a bytes-like object is required, not 'str'

If anyone can help me fix this error, it would be much appreciated.

Code Below:

from Crypto.Cipher import AES
import base64
import os

def en():
    message = input("Message to encrypt: ")
    BLOCK_SIZE = 32
    PADDING = '{'

    pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING

    EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))

    en_Key = b'-\xdd\xe3\x12\xa5\xc9\xfbB\xb7\xe1\xf7?l\xb0\x91"\xb8\\\xae\xa4\x8a\xbfo\x0bf\xcf\xeek\xb1k\x00d'
    print('Encryption key: ', en_Key)

    cipher = AES.new(en_Key)

    encoded = EncodeAES(cipher, message)
    print('Encrypted message: ', encoded)

def de(en_Message):

    PADDING = '{'
    DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)

    en_Key = b'-\xdd\xe3\x12\xa5\xc9\xfbB\xb7\xe1\xf7?l\xb0\x91"\xb8\\\xae\xa4\x8a\xbfo\x0bf\xcf\xeek\xb1k\x00d'

    cipher = AES.new(en_Key)

    decoded = DecodeAES(cipher, en_Message)
    print('Decoded message: ', decoded)
Thom-x
  • 841
  • 1
  • 6
  • 20
Delusive
  • 11
  • 1
  • On which line does that error appear? – Artjom B. Oct 24 '15 at 22:47
  • Not on a line, just when i launch the program, and select the decryption – Delusive Oct 31 '15 at 01:57
  • 1
    I suspect `en_Message` is a `str`, but pyCrypto expects a `bytes` object. You haven't shown the the code that's producing the input to those functions, so general advice should do: [Best way to convert string to bytes in Python 3?](http://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3) – Artjom B. Oct 31 '15 at 12:28

0 Answers0