Introduction to Cryptography
Cryptography ensures data security, integrity, and authentication using techniques like hashing, encryption, and digital signatures. Python provides powerful libraries like:
hashlib – For hashing data (SHA, MD5, etc.)
cryptography – For encryption (AES, RSA, etc.)
PyCrypto/PyCryptodome – For secure encryption & decryption
PyNaCl – For modern cryptographic operations
Installing Cryptography Libraries
pip install cryptography pycryptodome
Hashing with hashlib
Hash passwords securely using SHA-256.
import hashlib
def hash_data(data):
return hashlib.sha256(data.encode()).hexdigest()
# Example usage
print(hash_data("securepassword"))
Generates a secure one-way hash.
Symmetric Encryption with AES (cryptography Library)
AES (Advanced Encryption Standard) is used for secure data encryption.
from cryptography.fernet import Fernet
# Generate a key
key = Fernet.generate_key()
cipher = Fernet(key)
def encrypt_message(message):
return cipher.encrypt(message.encode())
def decrypt_message(encrypted_message):
return cipher.decrypt(encrypted_message).decode()
# Example usage
encrypted = encrypt_message("Secret Message")
print("Encrypted:", encrypted)
print("Decrypted:", decrypt_message(encrypted))
Fernet (AES-128) encryption ensures secure communication.
Asymmetric Encryption with RSA (PyCryptodome Library)
RSA uses a public key for encryption and a private key for decryption.
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64
# Generate RSA key pair
key = RSA.generate(2048)
public_key = key.publickey().export_key()
private_key = key.export_key()
def rsa_encrypt(message, pub_key):
recipient_key = RSA.import_key(pub_key)
cipher_rsa = PKCS1_OAEP.new(recipient_key)
encrypted = cipher_rsa.encrypt(message.encode())
return base64.b64encode(encrypted).decode()
def rsa_decrypt(encrypted_message, priv_key):
private_key = RSA.import_key(priv_key)
cipher_rsa = PKCS1_OAEP.new(private_key)
decrypted = cipher_rsa.decrypt(base64.b64decode(encrypted_message))
return decrypted.decode()
# Example usage
encrypted_msg = rsa_encrypt("Secure Data!", public_key)
print("Encrypted:", encrypted_msg)
print("Decrypted:", rsa_decrypt(encrypted_msg, private_key))
RSA encryption is used for secure authentication & key exchange.
Digital Signatures with RSA
Digital signatures verify data integrity and authenticity.
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
def sign_data(data, priv_key):
key = RSA.import_key(priv_key)
hashed_data = SHA256.new(data.encode())
signature = pkcs1_15.new(key).sign(hashed_data)
return base64.b64encode(signature).decode()
def verify_signature(data, signature, pub_key):
key = RSA.import_key(pub_key)
hashed_data = SHA256.new(data.encode())
try:
pkcs1_15.new(key).verify(hashed_data, base64.b64decode(signature))
return "Signature Verified ✅"
except:
return "Invalid Signature ❌"
# Example usage
signature = sign_data("Secure Message", private_key)
print("Signature:", signature)
print(verify_signature("Secure Message", signature, public_key))
Ensures data authenticity & prevents tampering.
When to Use Cryptography?
✔ Secure Password Storage – Hash passwords before storing them.
✔ Data Encryption – Encrypt sensitive data during transmission.
✔ Digital Signatures – Verify identity and prevent forgery.
✔ Authentication – Secure communication in web apps.