🔐Practical 12: RSA Login Simulation

Remote User Authentication Using Asymmetric Encryption with Blocking

Simulate Secure Login
Using RSA asymmetric encryption
Understand Cryptography
Public-private key authentication
Enhance Security
Blocking after failed attempts

🎓 Related Theories

1. Asymmetric Encryption

  • Uses a key pair system
  • Public Key: Used to encrypt data
  • Private Key: Used to decrypt data
  • RSA is the most widely used algorithm
  • Ensures confidentiality

2. User Authentication

  • Process of verifying user identity
  • Credentials encrypted and transmitted
  • Server decrypts and validates
  • Secure verification process

3. Login Attempt Blocking

  • Protection against brute-force attacks
  • Blocks after 3 failed attempts
  • Enhances system security
  • Prevents repeated attack attempts

💻 Implementation Code

rsa_login_simulation.py
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.")

📊 Program Output

✅ For Authorized Access

Enter username: gecuser
Enter password: secure123
Access Granted
❌ For Unauthorized Access
Enter username: gecuser
Enter password: secure
Access Denied (Attempt 1)

Enter username: gecuser
Enter password: secure12
Access Denied (Attempt 2)

Enter username: gec_user
Enter password: secure123
Access Denied (Attempt 3)
User has been blocked after 3 failed attempts.

🛡️ Security Aspects

🔐 Confidentiality

Credentials are encrypted using RSA public key, preventing interception during transit

🎫 Authentication

Server decrypts credentials using private key, ensuring only authorized servers can read login data

🚫 Brute-force Protection

System blocks user after three failed attempts, preventing repeated login attacks

🔒 No Plaintext Exposure

Credentials are never transmitted in plaintext during communication

🗄️ Secure Storage

Original passwords are never saved; only hash values are stored for comparison

🌍 Real-World Relevance

🏦 Online Banking/Login

Your credentials are securely encrypted before being sent to the bank's server.

🌐 SSL/TLS Websites (https)

Uses RSA to securely exchange session keys between browser and server.

💬 Secure Messaging

WhatsApp, Signal use RSA to securely exchange keys for end-to-end encryption.

📄 Digital Signatures

RSA ensures authenticity of documents via digital signatures in e-governance.

📋 Summary of the Practical

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.

💡 Important Notes

🎯 Learning Outcomes

After completing this practical, you will be able to:

🔑 Cryptography Application

Apply public-private key cryptography to secure user login data and understand RSA implementation.

🐍 Python Implementation

Implement a secure login system in Python using PyCryptodome library effectively.

🛡️ Security Mechanisms

Understand and implement login attempt tracking and blocking after repeated failures.

🔐 Secure Storage

Apply secure storage principles using cryptographic hash functions (SHA-256) instead of plaintext.