Last active
October 26, 2022 05:41
-
-
Save GrantSchiller/6552176 to your computer and use it in GitHub Desktop.
Includes basic Caesar cipher encode/decode and an assisted brute force decode. (Python 3.3.2)
This file contains 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
def init_(): # Initializes the program | |
startup = input("Encode or decode with key or decode without key? (ed/dwok)\n> ").lower() | |
if startup == "dwok": # "dwok" means decode without key. | |
caesar_cipher_hiddenkey_decode() | |
elif startup == "ed": # "ed" means encode/decode. | |
caesar_cipher() | |
else: | |
print("It looks like you didn't input ed or dwok. Try again please.") | |
startup_() | |
def isInt_(x): | |
try: | |
int(x) | |
return True | |
except ValueError: | |
return False | |
def caesar_cipher(): # Encodes or decodes with a key | |
text = input("\nInput your message.\n> ") | |
key = int(input("\nInput your integer key.\n> ")) | |
mode = input("\nEncode or decode?\n> ") | |
alphabet = "abcdefghijlmnopqrstuvwxyz" | |
newText = "" # text = list(text) is unnecessary here | |
if mode == "decode": # Reverses the direction of the inputted shift | |
key = -key | |
print("\nDecoding...\n") | |
else: | |
print("\nEncoding...\n") | |
''' | |
The following loop iterates through the index values up to the length of the inputted text. | |
For each index value plus the shift value of the letter in the inputted text, | |
the loop stores the corresponding letter in newLetter. | |
The modulus is to correct for values outside of the alphabet range. | |
Finally, the newLetter is added to the newText string. | |
Along the way, some corrections are made for special cases. | |
Really, it's easiest to tell what's going on using the PythonTutor.com Visualizer. | |
''' | |
for char in range(len(text)): | |
if text[char].isupper(): | |
alphabet = alphabet.upper() | |
elif text[char].islower(): | |
alphabet = alphabet.lower() | |
if text[char].isalpha() == False: # Exception for non-letters | |
if isInt_(text[char]): | |
newLetter = str((int(text[char]) + key)%10) | |
else: | |
newLetter = text[char] | |
else: | |
newLetter = alphabet[((alphabet.index(text[char])+key)%25)] | |
newText += newLetter | |
print("Result:", newText) | |
def caesar_cipher_hiddenkey_decode(): # Decodes one step at a time with user input | |
text = input("\nInput your message to decode.\n> ") | |
alphabet = "abcdefghijlmnopqrstuvwxyz" | |
key = 1 | |
not_done = "" | |
while not_done == "": | |
newText = "" | |
print("\nDecoding...\n") | |
for char in range(len(text)): | |
if text[char].isupper(): | |
alphabet = alphabet.upper() | |
elif text[char].islower(): | |
alphabet = alphabet.lower() | |
if text[char].isalpha() == False: | |
if isInt_(text[char]): | |
newLetter = str((int(text[char]) - key)%10) | |
else: | |
newLetter = text[char] | |
else: | |
newLetter = alphabet[((alphabet.index(text[char])-key)%25)] | |
newText += newLetter | |
key += 1 # Iterate through all possible key values one step at a time. | |
print("Result: " + newText) | |
not_done = input("\nInput something if done.\n> ") # User can tell the script when to stop cycling. | |
print("\nDone!\n\nThe final result is: " + newText) | |
init_() # Begins the script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there any error here I put:
message: hello there
key: 5675
encode
result: hello there
any error? I missing?