The following code works for me :
Python 3.8.3 (default, May 19 2020, 06:50:17) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cryptography.fernet as cpf
>>> key = cpf.Fernet.generate_key()
>>> f = cpf.Fernet(key)
>>> m = f.encrypt("1234, abcd, '123CE'".encode())
>>> m
b'gAAAAABgbsGYBZjqDDilTYl5zmBfENlPkEbEdnK242_K1IBovuL-oiGrvvZmUZZMUoMQ2jYUdJ32GaI834U_Oe5Bdxhf6r-hIG5KwFawfmN45ewjSlgmzx4='
However, you problem can happen when generating your strings:
>>> s = "my string" + b"my bytestring"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "bytes") to str
>>> s = "my string" + b"my bytestring".decode()
>>> s
'my stringmy bytestring'
Make sure you convert all you strings to either str
or bytestring
format before trying to concatenate them. For that, use :
s.decode()
to convert s
to str
if s
is a bytestring
s.encode()
to convert s
to bytestring
if s
is of str
type.