Question

In: Computer Science

how can we use cryptography libraries in Python

how can we use cryptography libraries in Python

Solutions

Expert Solution

For using cryptographic modules, we need to install it first. It includes all the recipes and primitives, and provides a high level interface of coding in Python. We can install cryptography module using the following command at the command prompt. Make sure that the command prompt should be be opened as "Run as administrator". Go to the folder in which pip tool resides which comes along with the basic installation of Python.

For me it is C:\Python37-32\Scripts\ - Here type the command pip install cryptography and press enter.

The screenshot for the installation is pasted below.

Following shows an example Python program which encrypts a string if it contains only digits,letters, comma and spaces.

from cryptography.fernet import Fernet
key = Fernet.generate_key()
#Reading a string for encryption
str=input("Enter a string for encryption:")
#Checking whether string contains only digits,letters,comma and spaces
for char in str:
if char in "0123456789 .,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
flag=0
else:
flag=1
break
if flag==1:
print("Invalid string.Try again")
else:
#Generating key for encryption
message = str.encode()
f = Fernet(key)
#Encryption
encrypted = f.encrypt(message)
#printing encrypted message
print("Encrypted message:",encrypted)
#Decryption
decrypted = f.decrypt(encrypted)
#printing decrypted message
print("Decrypted message:",decrypted)

Python code in IDLE pasted for better understanding of the indent

Output


Related Solutions

Find out how can we use cryptography libraries in Python. Write down the steps to install...
Find out how can we use cryptography libraries in Python. Write down the steps to install cryptography library in Python.
Writ a Python Program that illustrate the DES algorithm 1-Not allowed to use any crypto libraries...
Writ a Python Program that illustrate the DES algorithm 1-Not allowed to use any crypto libraries 2- The whole algorithm must be implemented from scratch 2- The code must be properly commented Please don’t use librae’s or any code on the internet please do the code for me i have posted this many times
Writ a Python Program that illustrate the DES algorithm 1-Not allowed to use any crypto libraries...
Writ a Python Program that illustrate the DES algorithm 1-Not allowed to use any crypto libraries 2- The whole algorithm must be implemented from scratch 2- The code must be properly commented Please don’t use librae’s or any code on the internet
How do we encrypt? Explain modern symmetric cryptography. Explain the digital signature scheme. Explain the cryptography...
How do we encrypt? Explain modern symmetric cryptography. Explain the digital signature scheme. Explain the cryptography tools. What is digital signature? What is steganography? Explain the protocol for secure communications.
Cryptography - please use Python to complete the following task. You don't have to implement AES...
Cryptography - please use Python to complete the following task. You don't have to implement AES just import the necessary libraries. Can you fix my code? Thanks! Read in a 128 bit key from the user (16 characters). If the user enters a key of any other size it's fine to terminate the program with an error. Read in a string of arbitary length from the user. (ie, the string could be 1 character, or 50,000, or anywhere in between)....
Python programming ***************************************************************************************************** If we load the json files then how could we use the objects...
Python programming ***************************************************************************************************** If we load the json files then how could we use the objects to extract particular values from those files. eg: fi1 = open("king.json", "rb") obj1 = yaml.safe_load(fi1.read()) fi1.close() fi2 = open("queen.json", "rb") obj2 = yaml.safe_load(fi2.read()) fi2.close() Now if the JSON file queen.json had elements like name, rule year, children, etc. and we wanted the name of the queen who ruled between particular years, how would we do it? second would be getting a value common for...
in python: (can use ntlk) Given the following documents: • Can we go to Disney??!!!!!! Let's...
in python: (can use ntlk) Given the following documents: • Can we go to Disney??!!!!!! Let's go on a plane! • The New England Patriots won the Super Bowl.. • I HATE going to school so early • When will I be considered an adult? • I want to go to A&M, Baylor, or the University of Texas. Conduct punctuation removal, stop word removal, casefolding, lemmatization, stemming on the documents.
Widespread use of cryptography can mean trouble for law enforcement and security personnel when crooks and spies use cryptography to secure their communications from wiretapping.
Widespread use of cryptography can mean trouble for law enforcement and security personnel when crooks and spies use cryptography to secure their communications from wiretapping. Under U.S. law, communications utilities are required to provide law enforcement access to communication links when proper judicial process has been observed. Such access is useless if the messages they provide access to are encrypted with powerful security. For years the U.S. and other countries have tried to control the spread of cryptographic technologies, with...
How can we use game theory to improve strategic decision-making? How can we use game theory...
How can we use game theory to improve strategic decision-making? How can we use game theory in the construction and/or design of a strategy?
Explain how we can use phage in daily life
Explain how we can use phage in daily life
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT