Skip to content Skip to sidebar Skip to footer

Python Decode Fernet Key

I have generated few fernet keys and stored in str format for reference. Now, I need to encode these fernet keys in str format to 32 url-safe base64-encoded bytes to decrypt my dat

Solution 1:

Try your code without base64 encoding your key ie:

from cryptography.fernet import Fernet as frt

key=frt.generate_key()
s = "message"
print('input string: {0}'.format(s))
#key=base64.b64encode(key) #no need to do this
print('key: {0}, type: {1}'.format(key, type(key)))
f=frt(key)
token = f.encrypt(s.encode('utf-8')) #need to convert the string to bytes
print ('encrypted: {0}'.format(token))
output = f.decrypt(token)
output_decoded = output.decode('utf-8')
print ('decrypted: {0}'.format(output_decoded))

Post a Comment for "Python Decode Fernet Key"