Skip to the content.

Safe Computing HW Hack

import random

def caesar_cipher(text, shift, mode):
    result = ""
    for char in text:
        if char.isalpha():  # This part of the code only encrypts letters
            shift_amount = shift if mode == "encrypt" else -shift
            new_char = chr(((ord(char.lower()) - 97 + shift_amount) % 26) + 97)
            result += new_char.upper() if char.isupper() else new_char
        else:
            result += char  # This keeps the spaces and position unchanged
    return result

# Generate a random shift value
shift = random.randint(1, 25)
print(f"Random shift value chosen: {shift}")

# Get user input
mode = input("Do you want to encrypt or decrypt? ").strip().lower()
message = input("Enter your message: ")

# Perform encryption/decryption
output = caesar_cipher(message, shift, mode)
print(f"Result: {output}")
Random shift value chosen: 1
Result: Gh! Lx mzld hr Zuhjz