Created
December 9, 2024 11:26
-
-
Save Joseph413168/02ca70d5c5c8b5a346833ddaec510e55 to your computer and use it in GitHub Desktop.
Encryption Algorithm (Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Each letter is assigned a color and converted to HEX then converted to its decimal mathematical operation is preformed on it | |
| # Then the numbers are converted to letters | |
| import random | |
| def generate_random_color(): | |
| """Generates a random color in hex.""" | |
| return "#{:06x}".format(random.randint(0, 0xFFFFFF)) | |
| def color_to_decimal(hex_color): | |
| """Converts a hex color to a decimal value.""" | |
| return int(hex_color[1:], 16) | |
| def decimal_to_color(decimal_value): | |
| """Converts a decimal value back to a hex color.""" | |
| return "#{:06x}".format(decimal_value & 0xFFFFFF) # Ensures valid hex range | |
| def perform_operation(decimal_value, operation, operand): | |
| """Performs the selected mathematical operation.""" | |
| if operation == "addition": | |
| return decimal_value + operand | |
| elif operation == "subtraction": | |
| return decimal_value - operand | |
| elif operation == "multiplication": | |
| return decimal_value * operand | |
| elif operation == "division": | |
| return decimal_value // operand # Use floor division for integer results | |
| else: | |
| raise ValueError("Invalid operation selected.") | |
| def digits_to_letters(number): | |
| """Converts each digit of a number into a corresponding letter.""" | |
| digit_to_letter = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', | |
| 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J'} | |
| return ''.join(digit_to_letter[int(digit)] for digit in str(number)) | |
| def encrypt(text, operation, operand): | |
| """Encrypts the input text.""" | |
| letter_to_color = {} | |
| encrypted_values = {} | |
| operation_log = [] | |
| for letter in text: | |
| # Assign a random color to the letter if not already assigned | |
| if letter not in letter_to_color: | |
| random_color = generate_random_color() | |
| letter_to_color[letter] = random_color | |
| # Get the color and its decimal value | |
| hex_color = letter_to_color[letter] | |
| decimal_value = color_to_decimal(hex_color) | |
| # Perform the selected operation | |
| transformed_value = perform_operation(decimal_value, operation, operand) | |
| # Convert the result to letters | |
| transformed_letters = digits_to_letters(transformed_value) | |
| # Save the operation details | |
| operation_log.append( | |
| f"Letter '{letter}' (Color {hex_color}, Decimal {decimal_value}) {operation} {operand} = {transformed_value} -> {transformed_letters}" | |
| ) | |
| # Store the encrypted value | |
| encrypted_values[letter] = transformed_letters | |
| return encrypted_values, letter_to_color, operation_log | |
| # Input and encryption | |
| text = input("Enter the text to encrypt: ") | |
| operation = input("Enter the mathematical operation (addition, subtraction, multiplication, division): ").lower() | |
| operand = int(input("Enter the numerical operand (e.g., 2, 5): ")) | |
| encrypted_values, letter_to_color, operation_log = encrypt(text, operation, operand) | |
| # Display results | |
| print("\n--- Encryption Results ---") | |
| print("Encrypted Values:", encrypted_values) | |
| print("Letter to Color Mapping:", letter_to_color) | |
| print("\nMath Operations Performed:") | |
| for log in operation_log: | |
| print(log) | |
| input("\nPress Enter to exit...") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment