key = call_key() slogan = "Hello!! Welcome to AIM!!".encode() a = Fernet(key) coded_slogan = a.encrypt(slogan) print(coded_slogan)Īs you can see we have successfully encrypted the data. The next step would be passing the message you want to encrypt in the encode function, initializing the Fernet class, and encrypt the data using encrypt function. # Function to load the key def call_key(): return open("pass.key", "rb").read() The key generated above is a unique key and it will be used further for all encryption and decryption processes so In order to call this key, again and again, let us define a function to load the key whenever required. This function will create a pass.key file in your directory as shown in the image below. # Generating the key and writing it to a file def genwrite_key(): key = Fernet.generate_key() with open("pass.key", "wb") as key_file: key_file.write(key) This function will create a key file where our generated key will be stored. Let’s define a function to generate a key and write it to a file. from cryptography.fernet import FernetĬryptography works on authentication for which we will need to generate a key. Let us import the Fernet function from the library. We first need to install the library using pip install cryptography.įernet function is used for encryption and decryption in Cryptography. Let us explore Cryptography and see how to encrypt and decrypt data using it. It provides cryptographic recipes to python developers. Python has the following modules/libraries which are used for cryptography namely:Ĭryptography is a python package that is helpful in Encrypting and Decrypting the data in python. Generally, it uses a key that is known to both the sender and the receiver so that they can cipher and decipher the text. Encryption is converting the text into a secret message, technically known as converting the plaintext to ciphertext and Decryption is converting the ciphertext back to plaintext so that only the authorized users can decipher and use the data. The ciphertext is a data or text which is encrypted into a secret code using a mathematical algorithm, it can be deciphered using different mathematical Algorithms. If I need to send my personal details to someone over mail, I can convert the information using Encryption techniques and send it, on the other hand, the receiver will decrypt the information received using Decryption Techniques and there will be no data tampering in between. It involves the process of encrypting and decrypting the data, for eg. It works on different mathematical concepts and algorithms to transfer the encoded data into a secret code which is difficult to decode. Cryptography is a process which is mainly used for safe and secure communication.
0 Comments
Leave a Reply. |