示例#1
0
    def DecryptTxt (self):
        # take inputs
        tkProg.ClearScreen()
        path = input ("input path of txt to decrypt ")
        tkProg.ClearScreen()
        outPath = input ("input path to folder output to, leave as blank if you want to output to the program directory ")
        tkProg.ClearScreen()
        outFileName = input ("What should the name of the file be, leave blank for default name, do not include file extensions ")
        alphabet = self.AlphabetContainer.TakeAlphabets ()  # take the working alphabet
        tkProg.ClearScreen()
        print ("Choose a decryption method")
        print ("1) Find all possible values for all possible keys")
        print ("2) Decrypt with a known key")
        print ("3) Find the most likely key")
        in1 = input()

        msg = []

        # read the file
        if (exists (path)):
            with open (path) as f:
                msg = f.readlines()

            # remove all whitespace characters
            msg = [x.strip() for x in msg]
            
        msgStr = "".join (msg)            
        value = "" 

        if (in1 == "1"):                # brute force
            value = CaesarDecrypt (msgStr, alphabet)
        elif (in1 == "2"):              # decrypt with known key
            workingKey = input ("what key should be used: ")
            value = CaesarDecryptKey (msgStr, alphabet, int(workingKey)) + " - Encrypted using key: " + workingKey
        else:                           # chi squared
            language = input ("which language pack should be chosen, leave blank for English ")
            if (language == ""):
                language = "English"

            topX = input ("How many top results should be returned, leave as 1 for only the most likely result ")

            value = SmartCaesarDecrypt (msgStr, alphabet, language, int(topX) - 1)             

        # write file
        print ("writing...")
        # output the file
        T = ""
        if (outFileName == ""):
            T = "Single layer encryption at " + str(datetime.datetime.now())
        else:
            T = outFileName
        filepath = os.path.join (outPath, T) + ".txt"
        f = open (filepath, "w")
        for i in value:
            f.write (str(i[0]) + " with key " + str(i[1]) + "\n")
        f.close()

        print (value)
        return
    def EncryptTxt(self):
        # take inputs
        tkProg.ClearScreen()
        path = input("input path of txt to encrypt")
        tkProg.ClearScreen()
        outPath = input(
            "input path to folder output to, leave as blank if you want to output to the program directory"
        )
        tkProg.ClearScreen()
        outFileName = input(
            "What should the name of the file be, leave blank for default name, do not include file extensions"
        )
        key = input(
            "input the value of the key to encrypt with, leave as blank for a randomly generated key"
        )
        alphabet = self.AlphabetContainer.TakeAlphabets(
        )  # take the working alphabet

        value = ""

        # read the file and encrypt
        if (exists(path)):  # verify that the file exists
            f = open(path)
            msgs = f.readlines()

            v = []

            if (key == ""):
                key = random.randrange(len(alphabet))

            p = 0
            for msg in msgs:
                v.append(CaesarEncryptKey(msg, int(key), alphabet))
                p += 1
                print(str(round((p / len(msgs)) * 100, 2)) + "%")
                tkProg.ClearScreen()
        else:
            print("The file does not exist, please try again")
            return

        # output the file
        T = ""
        if (outFileName == ""):
            T = ("Encryption with key " + key)
        else:
            T = outFileName
        filepath = os.path.join(outPath, T) + ".txt"
        f = open(filepath, "w")
        for l in v:
            f.writelines(l)
        f.close()

        # letting the user know
        print("The encrypted message is " + value)
        print("Key: " + key)
        print("The file has been written to " + filepath)

        return 1
    def MultiDecryptTxt(self):
        # take inputs
        tkProg.ClearScreen()
        path = input("input path of txt to decrypt ")
        tkProg.ClearScreen()
        outPath = input(
            "input path to folder output to, leave as blank if you want to output to the program directory "
        )
        tkProg.ClearScreen()
        outFileName = input(
            "What should the name of the file be, leave blank for default name, do not include file extensions "
        )
        alphabet = self.AlphabetContainer.TakeAlphabets(
        )  # take the working alphabet
        tkProg.ClearScreen()
        print("working...")

        v = []

        # read the file and encrypt
        if (os.path.exists(path)):  # verify that the file exists
            f = open(path, "r")
            msgs = []
            msgs = f.readlines()

            for msg in msgs:
                v.append(MultiDecrypt(msg, alphabet))
        else:
            print("The file does not exist, please try again")
            return

        # write file
        T = ""
        if (outFileName == ""):
            T = "Multi decryption done at " + str(datetime.now())
        else:
            T = outFileName
        filepath = os.path.join(outPath, T) + ".txt"

        f = open(filepath, "w")
        for i in v:
            f.writelines(str(i[0]) + " " + str(i[1]) + " " + str(i[2]))
        f.close()

        print("done!")
        print(v)
        input()
    def ImportAlphabets (self):
        tkProg.ClearScreen()
        print ("Import new alphabet")
        print ()
        impPath = input ("Path of alphabet txt: ")
        print ("importing alphabet at " + impPath + "...")

        # check if path exists
        if (not os.path.exists (impPath)):
            print ("not a valid import path")
            return

        n : str  # name of alphabet
        s : str  # store the string that contains the alphabet

        f = open (impPath)
        
        n = None
        s = None

        for _ in f:
            wL = f.readline()
            if (wL[0] == "#"):      # hash indicates the name of the alphabet
                n = wL.strip()        
            elif (wL[0] == "$"):    # $ indicates the actual alphabet
                s = wL.strip()
        f.close()

        # check if the alphabet exists
        if (s == None):
            print ("Error, this alphabet is empty, please ensure the alphabet has been marked with the $ indicator")
            return

        # name the alphabet if it hasn't been named already
        if (n == None):
            n = input ("This alphabet is unnamed, please input a name: ")

        #convert the string into an array 
        v=[]
        for c in s:
            v.append (c)

        v.pop (0)   # remove the dollar sign

        # store it all in the container
        self.Container.Alphabets.append ((v, n))
        self.SaveAlphabets ()

        print ("the alphabet has been stored successfully and stored accordingly")
        Pause()
        return
    def ManageAlphabets (self):
        tkProg.ClearScreen()

        l = len (self.Container.Alphabets)
        if (l == 0):
            print ("there are no alphabets, add some using the import menu")
            Pause()
            return
        else:
            for a in range (l):
                print (str (a + 1) + ") " + self.Container.Alphabets[a][1])

        inp = input("Which would you like to delete?")
        self.Container.Alphabets.pop (int (inp) - 1)
        self.SaveAlphabets ()
        print ("successfully deleted")
        Pause()
        return
示例#6
0
ModulePaths = [f for f in os.listdir(os.getcwd()) if os.path.isfile(os.path.join(os.getcwd(), f))]
ModulePaths.remove ("program.py") 

for module in ModulePaths:
    __import__ (os.path.splitext(os.path.basename(module))[0])
    Modules.append ()
    '''

def Args ():
    """Handles argument parsing"""
    parser = argparse.ArgumentParser(description="Directly run a process from the run command")
    parser.add_argument("ProcessName", metavar="N", type=str, nargs='+', help="The name of the process to run")
    args = parser.parse_args()
    return args

tkProg.ClearScreen()
print ("Caesar encryption and decryption")
print ()

# display all module actions but also log them into a mega list
n = 1
for mod in Modules:
    for action in mod.Actions:
        print (str (n) + ") " + action)
        Actions.append ((mod.Actions[action], mod))
        n += 1

# TODO add input checking
userin = int (input (""))

# Call the method