Skip to content

4. Explanation of the Pencrypt Code

Pengu edited this page Dec 4, 2024 · 2 revisions

Explanation of the Pencrypt Code

Pencrypt is a Python program designed to encrypt and decrypt messages using the Caesar Encryption method. This method involves shifting each letter of the text by a fixed number of positions down the alphabet. Below, we'll walk through the code step by step, explaining how it works based on basic Python knowledge. Also we will make many chapters, for each version one, because the code is changing every version.

Version 1.0 - Surfing Penguin, Terminal Version

Importing Modules

python import random import time import os

The program begins by importing necessary modules. random is used to generate random numbers, time is used for creating delays, and os is used to clear the console screen.

Starting the Program

print("starting program...")
time.sleep(0.5)
print("started successfully")
time.sleep(0.1)
print("")
os.system('cls' if os.name == 'nt' else 'clear')

Here, the program prints a start-up message, pauses briefly using time.sleep(), and then clears the console screen with os.system().

Version Information

print("program version: 1.0 / surfing penguin")
print("an encrypting software made by Pengu")
print("")

The program prints its version information and a brief description.

Main Function Definitions

first_decision()
def first_decision():
    decision_made = False
    while decision_made == False:
        print("Please press E to encrypt and U to unscramble messages.")
        answer = input("-> ")
        if answer in ["E", "e", "encrypt", "encrypting mode"]:
            print("-> encrypting mode, activated")
            print("")
            decision_made = True
            encrypting_entrance()
        elif answer in ["U", "u", "unscrambling", "unscrambling mode"]:
            print("-> unscrambling mode, activated")
            print("")
            decision_made = True
            unscramble()
        else:
            print(" -> Sorry please press E or U thank you. ")
            print("")
encrypt_or_unscramble()
def encrypt_or_unscramble():
    os.system('cls' if os.name == 'nt' else 'clear')
    decision_made = False
    while decision_made == False:
        print("Please press E to encrypt and U to unscramble messages.")
        answer = input("-> ")
        if answer in ["E", "e", "encrypt", "encrypting mode"]:
            print("-> encrypting mode, activated")
            print("")
            decision_made = True
            encrypting_entrance()
        elif answer in ["U", "u", "unscrambling", "unscrambling mode"]:
            print("-> unscrambling mode, activated")
            print("")
            decision_made = True
            unscramble()
        else:
            print(" -> Sorry please press E or U thank you. ")
            print("")
encrypting_entrance()
def encrypting_entrance():
    os.system('cls' if os.name == 'nt' else 'clear')
    print("Lets encrypt your text message!")
    print("-------------------------------------------->")
    print("Firstly we need an Pencrypting-Code, you can choose your")
    print("own code between 1 and 26 or can use a random Code.")
    encrypt()
encrypt()
def encrypt():
    code = 1
    owncode = 1
    decision_made = False
    print("-> Please press O to use your own Code and R to get an random Code,")
    print("   you also can exit encrypting mode with X.")

    while decision_made == False:
        answer = input("-> ")
        print("")
        if answer in ["O", "o", "own", "own code", "Own", "Own Code", "0"]:
            print("")
            print("-> Please enter your own code from 1 to 26 or exit with X")
            while True:
                owncode = input("-> ")
                if owncode in ["X", "x", "Exit", "exit"]:
                    print("-> Exiting self-choosing mode")
                    decision_made = True
                    encrypt()
                    break
                elif owncode.isdigit():
                    if 1 <= int(owncode) <= 25:
                        code = int(owncode)
                        decision_made = True
                        break
                    else:
                        print("-> Sorry, please enter a number between 1 and 26 or X to exit.")
                        print("")
                else:
                    print(" -> Sorry, please enter a number or X to exit.")
                    print("")
        elif answer in ["R", "r", "random", "random code"]:
            code = random.randint(1,25)
            decision_made = True
        elif answer in ["X", "x", "Exit", "exit"]:
            print("-> Exiting encrypting mode")
            print("")
            decision_made = True
            encrypt_or_unscramble()
        else:
            print(" -> Sorry please press O, R or X thank you. ")
            print("")

    print(f"Nice, your Pencrypting-Code is {code}, now we can start encrypting your text message.")
    message = input("Please enter it here: ")
    # encrypting #
    encrypted_message = ""
    for char in message:
        if char.isalpha():
            ascii_offset = 65 if char.isupper() else 97
            encrypted_char = chr((ord(char) - ascii_offset + code) % 26 + ascii_offset)
            encrypted_message += encrypted_char
        else:
            encrypted_message += char
    print(f"Your encrypted message: {encrypted_message}")
    print(f"The message got created with the Pencrypt-Code {code}")

Caesar Cipher Encryption Logic

The code above selects the encryption code, prompts the user for a message, and then encrypts it using the Caesar Cipher. It shifts each character in the message by the code's value, wrapping around the alphabet.

unscramble()
def unscramble():
    os.system('cls' if os.name == 'nt' else 'clear')
    print("-> Lets unscramble your text message!")
    print("")

    decision_made = False
    while decision_made == False:
        print("-> Please enter your Pencrypt-Code from 1 to 26")
        print("   or exit unscrambling mode with X.")
        answer = input("-> ")
        if answer in ["X", "x", "Exit", "exit"]:
            print("-> Exiting unscrambling mode")
            print("")
            decision_made = True
            encrypt_or_unscramble()
        elif answer.isdigit():
            if 1 <= int(answer) <= 25:
                code = int(answer)
                decision_made = True
            else:
                print(" -> Sorry, please enter a number between 1 and 26 or X to exit.")
                print("")
        else:
            print(" -> Sorry, please enter your Pencrypt-Code or press X to exit.")
            print("")

    os.system('cls' if os.name == 'nt' else 'clear')
    print("Nice, now we can start unscrambling your text message.")
    message = input("Please enter it here: ")
    # unscrambling #
    unscrambled_message = ""
    for char in message:
        if char.isalpha():
            ascii_offset = 65 if char.isupper() else 97
            unscrambled_char = chr((ord(char) - ascii_offset - code) % 26 + ascii_offset)
            unscrambled_message += unscrambled_char
        else:
            unscrambled_message += char
    print(f"Your unscrambled message: {unscrambled_message}")
    print(f"The message got unscrambled with the Pencrypt-Code {code}")

Welcome Dialog

print("Welcome to Pencrypt, your nice and easy software to encrypt and")
print("unscramble text messages with the Caesar encryption technique.")
print("")

The program greets the user with a welcome message.

Program Execution

first_decision()
input("Press Enter to exit...")

Finally, the program starts by calling first_decision() and waits for the user to press Enter to exit.

Summary

Pencrypt is a simple yet effective program for encrypting and decrypting messages using the Caesar Cipher technique. It provides an easy-to-use interface, allowing users to securely encode their messages with either a chosen or random code. The program's design ensures that it is lightweight and fast, making it a practical tool for protecting your communications.