From 586206d4407b14b7c2844d7c4dd5d63bc8bc6d1b Mon Sep 17 00:00:00 2001 From: Pat Wick Date: Thu, 25 Apr 2024 21:54:11 -0400 Subject: [PATCH] completed encryption lab completed numerify, encrypt, decrypt, and wordify functions, in addition to an additional decryption method that attempts to crack the caesarian cipher when missing the shift number. --- ccipher.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/ccipher.py b/ccipher.py index 4434c1c..4843c36 100644 --- a/ccipher.py +++ b/ccipher.py @@ -16,4 +16,64 @@ hex_lst=[] for item in encoded_lst: hex_lst.append(list(item)[0]) print("The hex values associated with those bytes is:") -print(hex_lst) \ No newline at end of file +print(hex_lst) + +def numerify(message): + """Converts the characters in a String to their hex values, returns a list of hex values. + """ + letterBytes = [item.encode() for item in message] + hexResult = [list(letterByte)[0] for letterByte in letterBytes] + return hexResult + +def encrypt(numeric_message): + secretNumber = 1 + newMessage = [] + for value in numeric_message: + newMessage.append((value + secretNumber) % 256) + return newMessage + +def decrypt(encrypted_message): + secretNumber = 78 + newMessage = [] + for value in encrypted_message: + newMessage.append((value + (256 - secretNumber)) % 256) + return newMessage + +def wordify(decrypted_message): + message = [chr(item) for item in decrypted_message] + finalMessage = ''.join(message) + return finalMessage + +import statistics +def decrypt_unknown(encrypted_message): + """Makes the assumption that the space character is going to be the most common character in the message. This obviously wouldn't work with a shorter message consisting of just a word or even handful of words, where I would swap my strategy to something like the letter "e" instead. + """ + space = statistics.mode(encrypted_message) + cipher = space - 32 + newMessage = [] + for value in encrypted_message: + newMessage.append((value + (256 - cipher)) % 256) + return newMessage + +file1 = open("xfile.txt", "r") +file2 = open("personal_poetry.txt", "r") +file3 = open("answers19.txt", "r") + +def parseFile(file): + data = file.read() + #print(data) + data = data.split(', ') + #print(data) + data[0] = data[0][1:] + data[len(data)-1] = data[len(data)-1][:-1] + #print(data) + newData = [] + for datum in data: + newData.append(int(datum)) + #print(newData) + return newData + +message = parseFile(file3) +print(wordify(decrypt_unknown(message))) + +#print(wordify(decrypt(encrypt(numerify('abc'))))) \ No newline at end of file