0

i have this piece of code for encryption.

from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key) 
token = f.encrypt(b"something cool")
k = f.decrypt(token)
print(k) `

This is the output

b'something cool'

According to the example on the website, that "b" should've gone. I'm very new at this and would like to know or understand how exactly the solution works.

Thanks

2 Answers2

1

That ‘b’ means bytes. So instead of working with strings encryption algorythms are actually using bytes. My experience is that what you give a library (str/bytes/array) it should give you back, which Fernet is doing. I would simply convert the bytes back to a string k.decode(“utf-8”)

rayepps
  • 2,072
  • 1
  • 12
  • 22
0

The encryption functions are doing what they should: bytes in and bytes out.

Cryptography and encryption work with bytes, not strings or other encoding, decrypt returns bytes. The actual low level decrypt has no idea of encodings, it can't the decryption could be a string, it could be an image, etc.

It is up to the caller to provide encodings in and out that are appropriate to the data being encrypted/decrypted.

As the caller wrap the encryption in a function you write that provides the correct encodings, in this case a string to bytes on encryption and bytes back to a string on decryption.

zaph
  • 111,848
  • 21
  • 189
  • 228