def main():
    random.seed(43)

    for i in range(19):
        message = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk'
            'lmnopqrstuvwxyz1234567890 !?.,`~@#$%^&*()_-+=[]{}|;:<>/' 
        message = message * random.randint(2, 23)

        # convert message str to list
        message = list(message)
        random.shuffle(message)
        #convert back to str
        message = ''.join(message)

        # check all possible keys for each message
        for key in range(2, int(len(message) / 2)):
            encrypted = transposition_cipher.encryptMessage(key, message, \
                len(message))
            decrypted = transposition_cipher.decryptMessage(key, encrypted)

            # if results don't match then display error and quit
            if message != decrypted:
                print(f'{key} is not the correct key for message: {message}.')
                print(f'Decrypted as: {decrypted}')
                sys.exit()

        print(f'Test #{i+1} passed on message: "{message[:50]}..."')
示例#2
0
def main():
    random.seed(42)

    for i in range(20):
        message = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' * random.randint(4, 40)

        message = list(message)

        random.shuffle(message)
        message = ''.join(message)

        key = 8

        print('Test %s: "%s..."' % (i + 1, message[:50]))

        for key in range(1, int(len(message) / 2)):
            encrypted = transposition_cipher.decryptMessage(key, message)
            #print('Encrypted Message: %s' % encrypted)
            decrypted = transposition_cipher.encryptMessage(key, encrypted)
            #print('Decrypted Message: %s' % decrypted)

            if message != decrypted:
                print('Mismatch with key %s and message %s.' % (key, message))
                print('Decrypted as: ' + decrypted)
                sys.exit()
    print('Transposition cipher test passed')
示例#3
0
def main():
    inputFile = 'Prehistoric Men.txt'
    outputFile = 'Output.txt'
    key = int(input('Enter key: '))
    mode = input('Encrypt/Decrypt [e/d]: ')

    if not os.path.exists(inputFile):
        print('File %s does not exist. Quitting...' % inputFile)
        sys.exit()
    if os.path.exists(outputFile):
        print('Overwrite %s? [y/n]' % outputFile)
        response = input('> ')
        if not response.lower().startswith('y'):
            sys.exit()

    startTime = time.time()
    if mode.lower().startswith('e'):
        with open(inputFile) as f:
            content = f.read()
        translated = transCipher.encryptMessage(key, content)
    elif mode.lower().startswith('d'):
        with open(outputFile) as f:
            content = f.read()
        translated = transCipher.decryptMessage(key, content)

    with open(outputFile, 'w') as outputObj:
        outputObj.write(translated)

    totalTime = round(time.time() - startTime, 2)
    print(('Done (', totalTime, 'seconds )'))
示例#4
0
def encrypt():
	message, key = file_to_encrypt()
	print("####Starting to encrypt")
	startTime = time.time()
	encrypted_message = cipher.encryptMessage(key, message)
	print("The time of encryptyng is: {:.3f}".format(time.time() - startTime))
	save_encrypted_File(encrypted_message)
def main():
    inputFile = 'Prehistoric Men.txt'
    outputFile = 'Output.txt'
    key = int(input('Enter key: '))
    mode = input('Encrypt/Decrypt [e/d]: ')

    if not os.path.exists(inputFile):
        print('File %s does not exist. Quitting...' % inputFile)
        sys.exit()
    if os.path.exists(outputFile):
        print('Overwrite %s? [y/n]' % outputFile)
        response = input('> ')
        if not response.lower().startswith('y'):
            sys.exit()
            
    startTime = time.time()
    if mode.lower().startswith('e'):
        content = open(inputFile).read()
        translated = transCipher.encryptMessage(key, content)
    elif mode.lower().startswith('d'):
        content = open(outputFile).read()
        translated =transCipher .decryptMessage(key, content)

    outputObj = open(outputFile, 'w')
    outputObj.write(translated)
    outputObj.close()
    
    totalTime = round(time.time() - startTime, 2)
    print(('Done (', totalTime, 'seconds )'))
示例#6
0
def main():
    inputFile = "Prehistoric Men.txt"
    outputFile = "Output.txt"
    key = int(input("Enter key: "))
    mode = input("Encrypt/Decrypt [e/d]: ")

    if not os.path.exists(inputFile):
        print("File %s does not exist. Quitting..." % inputFile)
        sys.exit()
    if os.path.exists(outputFile):
        print("Overwrite %s? [y/n]" % outputFile)
        response = input("> ")
        if not response.lower().startswith("y"):
            sys.exit()

    startTime = time.time()
    if mode.lower().startswith("e"):
        with open(inputFile) as f:
            content = f.read()
        translated = transCipher.encryptMessage(key, content)
    elif mode.lower().startswith("d"):
        with open(outputFile) as f:
            content = f.read()
        translated = transCipher.decryptMessage(key, content)

    with open(outputFile, "w") as outputObj:
        outputObj.write(translated)

    totalTime = round(time.time() - startTime, 2)
    print(("Done (", totalTime, "seconds )"))
示例#7
0
def main():
    random.seed(43)

    for i in range(19):
        message = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.,`~@#$%^&*()_-+=[]{}|;:<>/' * random.randint(2, 23)

        # convert message str to list
        message = list(message)
        random.shuffle(message)
        #convert back to str
        message = ''.join(message)

        # check all possible keys for each message
        for key in range(2, int(len(message) / 2)):
            encrypted = transposition_cipher.encryptMessage(key, message, len(message))
            decrypted = transposition_brute_force.decryptMessage(key, encrypted)

            # if results don't match then display error and quit
            if message != decrypted:
                print('%s is not the correct key for message: %s.' % (key, message))
                print('Decrypted as: ' + decrypted)
                sys.exit()

            print('Test #%s passed with key %s: "%s..."' % (i+1, key, message[:50]))