Remote User Authentication Using Asymmetric Encryption with Blocking
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA256
# Create RSA key pair
server_key = RSA.generate(2048)
server_public_key = server_key.publickey()
# Store correct hashed credentials (on server)
correct_username = "gecuser"
correct_password = "secure123"
# Create hash digest
def hash_credential(username, password):
combined = f"{username}:{password}".encode()
hash_obj = SHA256.new(combined)
return hash_obj.digest()
# Store only the hashed version on server
stored_hash = hash_credential(correct_username, correct_password)
# Track login attempts
failed_attempts = 0
blocked = False
# Simulate multiple login attempts
while not blocked:
# --- User Input ---
username = input("Enter username: ")
password = input("Enter password: ")
# --- Encrypt login data using public key and send to Server ---
login_data = f"{username}:{password}".encode()
cipher = PKCS1_OAEP.new(server_public_key)
encrypted_data = cipher.encrypt(login_data)
# --- Server decrypt with private key ---
decipher = PKCS1_OAEP.new(server_key)
decrypted_data = decipher.decrypt(encrypted_data).decode()
user, passwd = decrypted_data.split(":", 1)
# --- Hash received credentials and compare ---
attempt_hash = hash_credential(user, passwd)
if attempt_hash == stored_hash:
print("Access Granted")
break
else:
failed_attempts += 1
print(f"Access Denied (Attempt {failed_attempts})")
if failed_attempts >= 3:
blocked = True
print("User has been blocked after 3 failed attempts.")
Credentials are encrypted using RSA public key, preventing interception during transit
Server decrypts credentials using private key, ensuring only authorized servers can read login data
System blocks user after three failed attempts, preventing repeated login attacks
Credentials are never transmitted in plaintext during communication
Original passwords are never saved; only hash values are stored for comparison
Your credentials are securely encrypted before being sent to the bank's server.
Uses RSA to securely exchange session keys between browser and server.
WhatsApp, Signal use RSA to securely exchange keys for end-to-end encryption.
RSA ensures authenticity of documents via digital signatures in e-governance.
| Component | Explanation |
|---|---|
| Key Generation | The server generates an RSA key pair: public for encryption, private for decryption. |
| Client Simulation | User enters username and password → encrypted using server's public key. |
| Secure Transmission | Data is encrypted → cannot be intercepted/read by others. |
| Server Verification | Server decrypts data using private key → compares with stored credentials. |
| Secure Storage | Server stores SHA-256 hash of credentials instead of plaintext passwords. |
| Login Logic | If match → access granted; else → failed attempt is logged. |
| Blocking Mechanism | After 3 failed attempts, user is blocked for security. |
After completing this practical, you will be able to:
Apply public-private key cryptography to secure user login data and understand RSA implementation.
Implement a secure login system in Python using PyCryptodome library effectively.
Understand and implement login attempt tracking and blocking after repeated failures.
Apply secure storage principles using cryptographic hash functions (SHA-256) instead of plaintext.