Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import hashlib
- class User:
- def __init__(self, username, password_hash, profile):
- self.username = username
- self.password_hash = password_hash
- self.profile = profile
- self.friends = []
- self.direct_messages = []
- class Message:
- def __init__(self, sender, receiver, content):
- self.sender = sender
- self.receiver = receiver
- self.content = content
- users = {}
- messages = []
- def generate_password_hash(password):
- hash_object = hashlib.sha256(password.encode())
- return hash_object.hexdigest()
- def create_account():
- new_user = User("", "", "")
- new_user.username = input("Enter your desired username: ")
- if new_user.username in users:
- print("Username already exists. Please choose another username.")
- return
- new_user.password_hash = generate_password_hash(input("Enter your password: "))
- new_user.profile = input("Create your profile: ")
- users[new_user.username] = new_user
- print("Account created successfully!")
- def authenticate_user(username, password):
- if username in users:
- entered_password_hash = generate_password_hash(password)
- if entered_password_hash == users[username].password_hash:
- print("Authentication successful!")
- else :
- print("Authentication failed. Incorrect password.")
- else :
- print("User not found.")
- def delete_account(username):
- if username in users:
- del users[username]
- print("Account deleted successfully!")
- else :
- print("User not found.")
- def encrypt_message(message, key):
- encrypted_message = ""
- for char in message:
- if char.isalpha():
- encrypted_message += chr((ord(char) + key - ord('A')) % 26 + ord('A'))
- else :
- encrypted_message += char
- return encrypted_message
- def secure_send_message(sender, receiver, content):
- new_message = Message(sender, receiver, content)
- messages.append(new_message)
- if receiver in users:
- encrypted_content = encrypt_message(content, 3) # Example Caesar cipher with key 3
- users[receiver].direct_messages.append(encrypted_content)
- def rotate_password(username):
- if username in users:
- new_password = input("Enter your new password: ")
- users[username].password_hash = generate_password_hash(new_password)
- print("Password rotated successfully!")
- else :
- print("User not found.")
- def display_messages(username):
- if username in users:
- print(f"Displaying messages for {
- username
- }:")
- for message in messages:
- if message.receiver == username or message.sender == username:
- print(f" {
- message.sender
- } -> {
- message.receiver
- }: {
- message.content
- }")
- else :
- print("User not found.")
- def display_direct_messages(username):
- if username in users:
- print(f"Displaying direct messages for {
- username
- }:")
- for encrypted_message in users[username].direct_messages:
- decrypted_message = encrypt_message(encrypted_message, -3) # Decrypt with key -3
- print(f" {
- username
- } received: {
- decrypted_message
- }")
- else :
- print("User not found.")
- def main():
- choice = None
- while choice != 0:
- print("\n--- Secure Messaging Tool ---")
- print("1. Create Account")
- print("2. Authenticate User")
- print("3. Delete Account")
- print("4. Send Message")
- print("5. Rotate Password")
- print("6. Display Messages")
- print("7. Display Direct Messages")
- print("0. Exit")
- choice = int(input("Enter your choice: "))
- if choice == 1:
- create_account()
- elif choice == 2:
- username = input("Enter your username: ")
- password = input("Enter your password: ")
- authenticate_user(username, password)
- elif choice == 3:
- username = input("Enter your username to delete your account: ")
- delete_account(username)
- elif choice == 4:
- sender = input("Enter sender username: ")
- receiver = input("Enter receiver username: ")
- content = input("Enter message content: ")
- secure_send_message(sender, receiver, content)
- elif choice == 5:
- username = input("Enter your username: ")
- rotate_password(username)
- elif choice == 6:
- username = input("Enter username to display messages: ")
- display_messages(username)
- elif choice == 7:
- username = input("Enter your username: ")
- display_direct_messages(username)
- elif choice == 0:
- print("Exiting program.")
- else :
- print("Invalid choice. Try again.")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement