To view your encrypted message, you must print it. encrypted_data = f.encrypt(b"This message is being encrypted and cannot be seen!")Īnd that’s it, the above sentence has been encrypted.
In order to encrypt data from the above key, you must use the encrypt method. print(key.decode()) Output Bq64GE−−93K1RVro4go1frN−8twBSvXdbCPSPLIKz9U= Encrypting Data Make sure to use the decode function while printing. We can print the key and store it, if needed. In the above lines, we generated a key using the generate_key() method and then assigned that key to a variable “f” in the next line.Īnd that’s it, you now have a key stored in a variable ready to be used. In order to start encrypting data, you must first create a fernet key. You are all set to start writing your script. Note − Make sure you get the capitalization’s correct. So, let us import it into the Python script. We will be using the fernet module to encrypt and decrypt data. Once you have the package downloaded and installed, you can import its modules.
To do so, launch your terminal and type in the code below.
HOW TO ENCODE A MESSAGE IN PYTHON INSTALL
The cryptography module does not come packaged with Python, which means you will have to install it using the pip package manager. Now that you know what we will be dealing with, let’s get started. While using the fernet module, a unique key is generated without which you cannot read or manipulate the encrypted data. We will be using the fernet module in the cryptography package to encrypt and decrypt data using Python. What is cryptography? Cryptography deals with the conversion of plain text into cipher text which is called encryption of data and cipher text back to plain text which is called decryption of data.