Search code examples
pythonencodingcryptographydecoding

Error- encrypt() .... positional argument : `data`


So i been trying to make a encoder and decoder just for fun. Since i am new to python i usually sit and try to debug the code bymyself but seems like this is pretty hard. Would Appreciate some help :).

Error =
ValueError: Fernet key must be 32 url-safe base64-encoded bytes.

from cryptography.fernet import Fernet as dpsisbad


question1 = input("Do You Have A Key? (yes - y and no - n)\n")
if question1 == "y":
    key = input("Enter Your Key Here \n")
    print(key)
else:
    key = dpsisbad.generate_key() 
    print("Please Keep this Key Safely.\n")
    print(key)
dpsstalks = dpsisbad(key)
question2 = input("Do You Want to Encode Or Decode The Message? (Encode = e and Decode = d)\n")


if question2 == "e":
    gg = input("Enter the text you want to encode. \n")
    encoded_text = gg.encode()
    token = dpsstalks.encrypt(encoded_text)
    print(token)
else:
    text1 = input("Enter the the encrypted text. \n")
    token = dpsstalks.decrypt(text1)
    print(token)



Solution

  • I've run into a similar problem some time ago. Your code is almost correct, the problem is that you haven't considered the fact that in python the method input() accept only string as input(so when you write b'abcd' in the input it will take the whole String as a String and it will ignore the b'', because it will think that it is part of the string). For this reason both the key and the token must be decoded when you are printing them out and you must encode them in the input() method, the code should look like this:

    from cryptography.fernet import Fernet as dpsisbad
    
    
    question1 = input("Do You Have A Key? (yes - y and no - n)\n")
    if question1 == "y":
        key = input("Enter Your Key Here \n").encode()
        print(key.decode())
    else:
        key = dpsisbad.generate_key() 
        print("Please Keep this Key Safely.\n")
        print(key.decode())
    dpsstalks = dpsisbad(key)
    
    
    while True:
        question2 = input("Do You Want to Encode Or Decode The Message? (Encode = e, Decode = d, quit = q)\n")
        if question2 == "e":
            gg = input("Enter the text you want to encode. \n")
            encoded_text = gg.encode()
            token = dpsstalks.encrypt(encoded_text)
            print(token.decode())
        elif question2 == "d":
            text1 = input("Enter the the encrypted text. \n").encode()
            token = dpsstalks.decrypt(text1)
            print(token.decode())
        elif question2 in ("q", "exit", "quit"):
            break
    

    The while and the last elif aren't really needed, I just added them to make it easyer to test and don't have to restart the program each time. Sorry for my poor english, but I'm not native speaker. If I have not been clear.

    Edit: In the output you could do also a thingh like this:

    str(token)[2 : -1]
    

    What it is actually doing it's trasforming the token in a string and removing the first two(b') and the last one character('), but I would not do it since for how much it works it is not the correct way, I am illustrating it to you anyway because maybe it could be useful for you (if only to modify the strings).