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.
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)....
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.
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?
Python - No libraries - No count() function allowed You need to travel 100 miles via...
Python - No libraries - No count() function allowed You need to travel 100 miles via rental car. There are several cars on the lot to choose from, each with their own MPG (miles per gallon) rating. Some cars have a manual transmission, while others do not (they're automatic). The price for gas in the area is $3 per gallon. Cars that have a manual transmission get a 10% discount at the pump. To streamline your selection, the car rental...
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use...
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use “re” or “Regex” to denote regular expressions. Write the Python Code to return matching analytics word in the following given text. Write the Python Code to return how many times analytics word is provided in this text. Definitions are useful to the extent they serve a purpose. So, is defining analytics important? Yes and no. It’s not likely that we’ll ever arrive at a...
Explain how we can use phage in daily life
Explain how we can use phage in daily life
If there are functional limit to the size of primes we can use, but how and...
If there are functional limit to the size of primes we can use, but how and why is there a functional limit? more detail on how primes are used in RSA.please type
How can we use Excel to Control and track a budget.
How can we use Excel to Control and track a budget.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT