key_lime_pi_6.3_key_typing.py
|
global key_type
def generate_key():
# key type 1, AKA Pi Mode:
if True:
import mpmath
import re
key_type = 1
mpmath.mp.dps = 1050
un_scrubbed_pi = str(mpmath.pi)
pi_str = re.sub(r'[^0-9]', '', un_scrubbed_pi)
#print(pi_str)
return pi_str, key_type
# key type 2: Try replacing the text "Try replacing me!"
# Be sure to leave the quotation marks or it won't work!
if False:
key_type = 2
new_key_generated = ""
new_key_str = "Try replacing me!"
for i in range(len(new_key_str)):
char_to_ascii = ord(new_key_str[i])
#print(f"{new_key_str[i]} converted to ascii: {char_to_ascii}")
new_key_generated += str(char_to_ascii)
return new_key_generated, key_type
#key type 3
#use alternate keys
if False:
key_type = 3
new_key_generated = ""
with open('key3.txt', 'r') as file:
content = file.read()
splitted_data = content.splitlines()
clean_content = ''.join(splitted_data)
print(clean_content)
new_key_str = clean_content
for i in range(len(new_key_str)):
char_to_ascii = ord(new_key_str[i])
#print(f"{new_key_str[i]} converted to ascii: {char_to_ascii}")
new_key_generated += str(char_to_ascii)
return new_key_generated, key_type
# key type 4, Math.Pi Mode for demonstrating limitations:
if False:
import re
import math
key_type = 4
pi = math.pi
un_scrubbed_pi = str(pi)
pi_str = re.sub(r'[^0-9]', '', un_scrubbed_pi)
#print(pi_str)
return pi_str, key_type
def encrypt_phrase(phrase, key):
# FIXME re-write all of this to use vague language FIXED
encrypted_phrase = ""
for i in range(len(phrase)):
#print(phrase[i])
ascii_char = int(str(ord(phrase[i])))
plain_char = chr(ascii_char)
print(f"ascii_char {ascii_char}\nplain_char: {plain_char}")
key_char_plus_ascii = int(ascii_char) + int(key[i])
new_plain_char = chr(key_char_plus_ascii)
if key_char_plus_ascii > 126:
mod_loop = key_char_plus_ascii % 126
key_char_plus_ascii = mod_loop + 32
print(f"mod loop {mod_loop} mod loop {key_char_plus_ascii}")
character_encrypted = chr(key_char_plus_ascii)
digit_key_added = int(key[i])
print(f"digit_key_added: {digit_key_added}")
print(f"key_char_plus_ascii {key_char_plus_ascii}\n new char: {new_plain_char}")
encrypted_phrase += character_encrypted
return encrypted_phrase
def de_encrypt_phrase(phrase, key):
de_encrypted_phrase = ""
for i in range(len(phrase)):
#print(phrase[i])
ascii_char = int(str(ord(phrase[i])))
print(f"ascii_char {ascii_char}")
if ascii_char <= 32:
mod_loop = ascii_char % 126
ascii_char = mod_loop + 32
print(f"mod: {mod_loop} and ascii char {ascii_char}")
key_char_minus_ascii = int(ascii_char) - int(key[i])
character_de_encrypted = chr(key_char_minus_ascii)
digit_key_subtracted = int(key[i])
print(f"digit_key_subtracted: {digit_key_subtracted}")
print(f"key_char_minus_ascii {key_char_minus_ascii}\n")
de_encrypted_phrase += character_de_encrypted
print()
return de_encrypted_phrase
def main():
key_str, key_type = generate_key()
print(f"Key type: {key_type}")
print("Generated Key:", key_str)
print(f"key length {len(key_str)}")
while True:
your_phrase = input(
"What is the phrase you want to encode? Q to quit. D to decode. ")
if your_phrase == 'Q':
break
elif your_phrase == 'D':
while your_phrase != 'E':
your_phrase = input(
"What do you want to decode? Q to quit. E to encrypt. ")
de_encrypted_phrase = de_encrypt_phrase(your_phrase, key_str)
if your_phrase != 'D' and your_phrase != 'E':
print(f"Decoded phrase: {de_encrypted_phrase}")
elif your_phrase == 'Q':
break
elif your_phrase == 'E':
continue
encrypted_phrase = encrypt_phrase(your_phrase, key_str)
if your_phrase != 'D' and your_phrase != 'E':
print("Encrypted Phrase:", encrypted_phrase)
if __name__ == "__main__":
main()