|
| 1 | +import hashlib |
| 2 | + |
| 3 | +def get_confidentiality_level(): |
| 4 | + while True: |
| 5 | + try: |
| 6 | + level = int(input("Enter the confidentiality level (1-10): ")) |
| 7 | + if 1 <= level <= 10: |
| 8 | + return level |
| 9 | + else: |
| 10 | + print("Confidentiality level must be between 1 and 10.") |
| 11 | + except ValueError: |
| 12 | + print("Please enter a valid integer between 1 and 10.") |
| 13 | + |
| 14 | +def caesar_cipher_encrypt(message, shift): |
| 15 | + encrypted_message = "" |
| 16 | + for char in message: |
| 17 | + if char.isalpha(): |
| 18 | + if char.islower(): |
| 19 | + encrypted_message += chr((ord(char) - ord('a') + shift) % 26 + ord('a')) |
| 20 | + elif char.isupper(): |
| 21 | + encrypted_message += chr((ord(char) - ord('A') + shift) % 26 + ord('A')) |
| 22 | + else: |
| 23 | + encrypted_message += char |
| 24 | + return encrypted_message, "Caesar Cipher" |
| 25 | + |
| 26 | +def encrypt_message(message, confidentiality_level): |
| 27 | + if 1 <= confidentiality_level <= 2: |
| 28 | + # Caesar cipher with a shift of 1 for low confidentiality |
| 29 | + shift = 1 |
| 30 | + encrypted_message, method = caesar_cipher_encrypt(message, shift) |
| 31 | + else: |
| 32 | + # Choose a suitable hashing algorithm based on the level |
| 33 | + if 2 <= confidentiality_level <= 4: |
| 34 | + hash_algorithm = hashlib.sha256 |
| 35 | + elif 4 < confidentiality_level <= 6: |
| 36 | + hash_algorithm = hashlib.sha384 |
| 37 | + elif 6 < confidentiality_level <= 8: |
| 38 | + hash_algorithm = hashlib.sha3_256 |
| 39 | + else: |
| 40 | + hash_algorithm = hashlib.sha512 |
| 41 | + |
| 42 | + # Hashing the message |
| 43 | + hashed_message = hash_algorithm(message.encode()).hexdigest() |
| 44 | + encrypted_message, method = hashed_message, hash_algorithm.__name__ |
| 45 | + |
| 46 | + return encrypted_message, method |
| 47 | + |
| 48 | +def main(): |
| 49 | + message = input("Enter the message to be encrypted: ") |
| 50 | + confidentiality_level = get_confidentiality_level() |
| 51 | + |
| 52 | + encrypted_message, method = encrypt_message(message, confidentiality_level) |
| 53 | + print(f"Encrypted message: {encrypted_message}") |
| 54 | + print(f"Encryption method used: {method}") |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + main() |
0 commit comments