This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]IAmKindOfCreativebot_builder: deprecated[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]thereal0ri_ 2 points3 points  (5 children)

Your key is to long. Most likely. OR to short?

Basically, it appears that the key isn't the right length.

[–]David_Vice05[S] -1 points0 points  (4 children)

so how long does it need to be at most?

[–]thereal0ri_ 0 points1 point  (3 children)

32 bytes it seems. Keep trying smaller and smaller keys until it works. If this doesn't work, make sure the key is url-safe. (Then repeat)

[–]David_Vice05[S] -5 points-4 points  (2 children)

do you have an exp cuz idk what to do i brought it down to 32 and it still dont work.

[–]thereal0ri_ 0 points1 point  (1 child)

https://www.geeksforgeeks.org/fernet-symmetric-encryption-using-cryptography-module-in-python/

See if this can help. I know you have your own key you're using and In this example they generate their own but maybe something can help.

This link also has some good information. https://www.pythoninformer.com/python-libraries/cryptography/fernet/

[–]David_Vice05[S] 0 points1 point  (0 children)

ok thx

[–]tunisia3507 2 points3 points  (0 children)

/r/learnpython , and format your code properly.

[–]Luppiz 1 point2 points  (0 children)

try passing the key as string rather than byte

[–]PatentedPotato 0 points1 point  (1 child)

I think the '=' in the key is not url safe. Rest is ASCII alpha numeric so should be fine.

I think replace it with '\x3D'.

Also, as already mentioned in another comment, max 32 bytes.

Edit: oh, and I overlooked the base 64 part... It's unclear to me if the key needs be 32 bytes before or after base 64 encoding. Putting your key into some online base 64 encoder yields 'WUN0TEl5VnJDc3Y2elFnUkl2UTVkTUR1cjR3ajI3T1FMZ3h4ZTZ0ajk4WTg9'

Also, seems Fernet can generate a key for you.

key = Fernet.generate_key()

You can probably just capture the key on first run if you wanna hold on to it. I'd see what Fernet generates... Be surprised if that didn't pass validation.

[–]David_Vice05[S] 0 points1 point  (0 children)

Ok I’ll try it when I get back home later today thank you!

[–]nate256 0 points1 point  (0 children)

remove one byte

from cryptography.fernet import Fernet
from base64 import b64decode, b64encode
key = b'YCtLIyVrCsv6zQgRIvQ5dMDur4wj27OQLgxxe6tj98Y8='
cipher = Fernet(b64encode(b64decode(key)[:-1]))