diff --git a/encryptDecrypt.py b/encryptDecrypt.py new file mode 100644 index 0000000000000000000000000000000000000000..833c10c42f9820d8ea86cddef466fa05e0c9419f --- /dev/null +++ b/encryptDecrypt.py @@ -0,0 +1,186 @@ +#Student Name Mateusz Apanel +#Student ID 18012102 +#this coursework is based on a project i did in school a few years back in c# and now re done in python. + + +def offset(chars): + s1=sum(chars) + #Adds all chars ASCII numbers together + s2=s1/8 + #Divides result by 8 + s3=round(s2) + #Rounds result + offsetFactor=s3-32 + print("Offset Factor:", offsetFactor) + return offsetFactor + #Sets the offset factor as the result -32 before printing and returning it + + +#ENCRYPTION +def Encrypt(message, offsetFactor): + #Arguments are the plainFile text and the offset factor + plainFile=message.readline() + print("Original text:",plainFile) + #Reads the line from the file and prints it + plainFile2=[] + cipher=[] + #Initialises the above arrays + i=0 + for i in range(0,len(plainFile)): + if plainFile[i]!=" ": + plainFile2.append(plainFile[i]) + #Adds all characters from text file to array except spaces. + for i in range(0, len(plainFile2)): + if i%5==0: + plainFile2.insert(i," ") + #If i is a multiple of 5, inserts a space in the array at that position + for i in range(0,len(plainFile2)): + if plainFile2[i]!=" ": + char=ord(plainFile2[i])+offsetFactor + #Encrypt the letter for the length of the text file + if char>126: + char-=94 + #If it is above the ASCII range, minus 94 + cipher.append(char) + #Adds the char to the cipher text array + else: + cipher.append(ord(" ")) + print(cipher) + cipherText="" + #Initialises the cipherText variable + for i in range(0,len(cipher)): + cipherText=cipherText+chr(cipher[i]) + #Adds all of the chars of the cipher text to the string + return cipherText + #Returns the cipher text string to the function + + +def EncryptMessage(): + fileValid=False + while fileValid==False: + fileName=input("Please enter the name of your file\n") #sample.txt + if os.path.isfile(fileName): + fileValid=True + file=open(fileName, "r") + #Opens the file to be encrypted if it exists + else: + print("This file does not exist") + i=0 + chars=[0,1,2,3,4,5,6,7] + nChars=[0,1,2,3,4,5,6,7] + #Initialises the above arrays + for i in chars: + nChars[i]=random.randint(33,126) + #Generates a random number within the ASCII range + chars[i]=chr(nChars[i]) + #Converts the random number to a character before storing in the array + i+=1 + key="'" + for i in range(0,8): + key+=chars[i] + key+="'" + print("\nKeep this key safe for decryption:", key) + #Sets the key to the array and prints it as a string + offsetFactor=offset(nChars) + #Carries out the offset factor calculation + cipherText=Encrypt(file, offsetFactor) + #Begins encryption process + print("Encryption Complete:", cipherText,"\n") + outputFileName=input("Please enter the name of the output file\n") + outputFile=open(outputFileName, "w") + outputFile.write(cipherText) + print("File written") + + + + +#DECRYPT MESSAGE +def decrypt(message, offsetFactor): + #Arguments are the cipher text and the offset factor + cipher=message.readline() + print("Encrypted text:",cipher) + #Reads the line from the file and prints it + plainFile=[] + #Initialises plainFile text array + for i in range(0,len(cipher)): + if cipher[i]!=" ": + char=ord(cipher[i])-offsetFactor + #For the length of the text, if char isn't space, decrypt letter + if char<32: + char+=94 + #If it is below the ASCII range, plus 94 + plainFile.append(char) + #Adds the char to the plainFile text array + else: + plainFile.append(ord(cipher[i])) + #Adds the space to the plainFile text array + plainText=chr(plainFile[0]) + #Adds the 1st letter of the plainFile text to the string + for i in range(1,len(plainFile)): + plainText=plainText+chr(plainFile[i]) + #Adds the rest of the chars of the plainFile text to the string + return plainText + #Returns the plainFile text string to the function + + +def DecryptMessage(): + fileValid=False + keyValid=False + while fileValid==False: + fileName=input("Please enter the name of your file\n") + if os.path.isfile(fileName): + fileValid=True + file=open(fileName, "r") + #Opens the file to be decrypted if it exists + else: + print("This file does not exist") + while keyValid==False: + sKey=input("Please input your 8 character key") + if len(sKey)==8: + keyValid=True + else: + print("Invalid key detected") + #Asks user to input key as string + i=0 + key=[0,1,2,3,4,5,6,7] + nKey=[0,1,2,3,4,5,6,7] + #Initialises above arrays + for i in key: + nKey[i]=ord(sKey[i]) + #Converts string into ASCII number and stores in array + key[i]=chr(nKey[i]) + #Converts ASCII number to character and stores in array + i+=1 + offsetFactor=offset(nKey) + #Carries out offset factor calculation + plainText=decrypt(file, offsetFactor) + #Begins decryption process + print("\nDecryption Complete:", plainText,"\n") + #Prints decrypted file + +#MAIN MENU +import time +import random +import os +#Imports the random and time modules for later use. +offsetFactor=0 +cipherText="" +#Initialises the above variables +def menu(): + choice=0 + while choice != 1|2: + print("Option 1: Encrypt Message\n") + print("Option 2: Decrypt Message\n") + print("Option 3: Exit\n") + #Displays the valid menu options + choice = int(input("Please type in 1, 2, 3 to choose an option.\n")) + #Gets the user to choose an option + if choice == 1: + EncryptMessage() + elif choice == 2: + DecryptMessage() + #Redirects the user to the correct function + time.sleep(1) + exit() + #Closes the program if the user chooses option 3 or other +menu() \ No newline at end of file