- Hacking RSA Cipher
- RSA Cipher Decryption
- RSA Cipher Encryption
- Creating RSA Keys
- Understanding RSA Algorithm
- Symmetric & Asymmetric Cryptography
- Implementation of One Time Pad Cipher
- One Time Pad Cipher
- Implementing Vignere Cipher
- Understanding Vignere Cipher
- Python Modules of Cryptography
- Decryption of Simple Substitution Cipher
- Testing of Simple Substitution Cipher
- Simple Substitution Cipher
- Hacking Monoalphabetic Cipher
- Affine Ciphers
- Multiplicative Cipher
- XOR Process
- Base64 Encoding & Decoding
- Decryption of files
- Encryption of files
- Decryption of Transposition Cipher
- Encryption of Transposition Cipher
- Transposition Cipher
- ROT13 Algorithm
- Caesar Cipher
- Reverse Cipher
- Python Overview and Installation
- Double Strength Encryption
- Overview
- Home
Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Modules of Cryptography
In this chapter, you will learn in detail about various modules of cryptography in Python.
Cryptography Module
It includes all the recipes and primitives, and provides a high level interface of coding in Python. You can install cryptography module using the following command −
pip install cryptography
Code
You can use the following code to implement the cryptography module −
from cryptography.fernet import Fernet key = Fernet.generate_key() cipher_suite = Fernet(key) cipher_text = cipher_suite.encrypt("This example is used to demonstrate cryptography module") plain_text = cipher_suite.decrypt(cipher_text)
Output
The code given above produces the following output −
The code given here is used to verify the password and creating its hash. It also includes logic for verifying the password for authentication purpose.
import uuid import hashpb def hash_password(password): # uuid is used to generate a random number of the specified password salt = uuid.uuid4().hex return hashpb.sha256(salt.encode() + password.encode()).hexdigest() + : + salt def check_password(hashed_password, user_password): password, salt = hashed_password.sppt( : ) return password == hashpb.sha256(salt.encode() + user_password.encode()).hexdigest() new_pass = input( Please enter a password: ) hashed_password = hash_password(new_pass) print( The string to store in the db is: + hashed_password) old_pass = input( Now please enter the password again to check: ) if check_password(hashed_password, old_pass): print( You entered the right password ) else: print( Passwords do not match )
Output
Scenario 1 − If you have entered a correct password, you can find the following output −
Scenario 2 − If we enter wrong password, you can find the following output −
Explanation
Hashpb package is used for storing passwords in a database. In this program, salt is used which adds a random sequence to the password string before implementing the hash function.
Advertisements