Table of Contents
Steps:
- Import Fernet
- Then, at that point, create an encryption key, that can be utilized for encryption and unscrambling.
- Convert the string to byte string, with the goal that it tends to be scrambled.
- Occurrence the Fernet class with the encryption key.
- Then, at that point, scramble the string with Fernet example.
- Then, at that point, it tends to be decoded with Fernet class occurrence and it ought to be instanced with a similar key utilized for encryption.
from cryptography.fernet import Fernet # we will be encryting the below string. message = "hello geeks" # generate a key for encryptio and decryption # You can use fernet to generate # the key or use random key generator # here I'm using fernet to generate key key = Fernet.generate_key() # Instance the Fernet class with the key fernet = Fernet(key) # then use the Fernet class instance # to encrypt the string string must must # be encoded to byte string before encryption encMessage = fernet.encrypt(message.encode()) print ( "original string: " , message) print ( "encrypted string: " , encMessage) # decrypt the encrypted string with the # Fernet instance of the key, # that was used for encrypting the string # encoded byte string is returned by decrypt method, # so decode it to string with decode methods decMessage = fernet.decrypt(encMessage).decode() print ( "decrypted string: " , decMessage) |
Output:
Awry key Encryption:
In Asymmetric-key Encryption, we utilize two keys a public key and private key. The public key is utilized to encode the information and the private key is utilized to unscramble the information. By the name, the public key can be public (can be shipped off any individual who needs to send information). Nobody has your private key, so nobody the center can peruse your information.
Example:
Install the python rsa library with the following command.
pip install rsa
import rsa # generate public and private keys with # rsa.newkeys method,this method accepts # key length as its parameter # key length should be atleast 16 publicKey, privateKey = rsa.newkeys( 512 ) # this is the string that we will be encrypting message = "hello geeks" # rsa.encrypt method is used to encrypt # string with public key string should be # encode to byte string before encryption # with encode method encMessage = rsa.encrypt(message.encode(), publicKey) print ( "original string: " , message) print ( "encrypted string: " , encMessage) # the encrypted message can be decrypted # with ras.decrypt method and private key # decrypt method returns encoded byte string, # use decode method to convert it to string # public key cannot be used for decryption decMessage = rsa.decrypt(encMessage, privateKey).decode() print ( "decrypted string: " , decMessage) |
Output:
Also Read: Reading an Excel File using Python