def main(): random.seed(50) #set random seed to static value # Generate 20 random test for i in range(20): # Genereate random message to test # Message will have random length (betwewen 4 and 40) message = 'ABCDEFGHIJKLMNOPQRSTUVWYXZ' * random.randint(4,40) #Convert the message string to a list to shuffle it: message = list(message) random.shuffle(message) #Convert list back to string message = ''.join(message) print('Test #%s: "%s..."' % (i + 1, message[:50])) # check all possible keys for each message for key in range (1, int(len(message)/2)): encrypted = transpositionEncrypt.encryptMessage(key, message) decrypted = decryptTransposition.decryptMessage(key, encrypted) # If the decryption doesn't match the original message, display an error message and quit if message != decrypted: print('There is something wrong with the key %s and message %s' % (key,message)) print('Decrypted as: ' + decrypted) sys.exit() print('Transposition cipher test passed')
def hackTransposition(message): print 'Hacking...' print '(Press Ctrl-C or Ctrl-D to quit at any time.)' # brute-force by looping through every possible key for key in range(1, len(message)): print 'Trying key #%s...' % (key) decryptedText = decryptTransposition.decryptMessage(key, message) if detectEnglish.isEnglish(decryptedText): # Check with user to see if the decrypted key has been found. print print 'Possible encryption hack:' print 'Key %s: %s' % (key, decryptedText[:100]) print #accounts for false positives print 'Enter D for done, or just press Enter to continue hacking:' response = raw_input('> ') if response.strip().upper().startswith('D'): return decryptedText return None
def main(): #inputFilename = 'frankenstein.txt' # to decrypt, next line must run inputFilename = 'frankenstein.encrypted.txt' outputFilename = 'frankenstein.decrypted.txt' myKey = 10 myMode = 'decrypt' # if input file does not exist program leaves early. if not os.path.exists(inputFilename): print "the file %s does not exist. Qutting..." % (inputFilename) sys.exit() # if output file exists, give user a chance to continue if os.path.exists(outputFilename): print "this will overwrite the file %s. (C)ontinue or (Q)uit?" % (outputFilename) response = raw_input('> ') if not response.lower().startswith('c'): sys.exit() # read in message from input file fileObj = open(inputFilename) content = fileObj.read() fileObj.close() print '%sing...' % (myMode.title()) # converts lowercase to first letter capitalized startTime = time.time() if myMode == 'encrypt': translated = TranspositionCipher.encryptMessage(myKey, content) elif myMode == 'decrypt': translated = decryptTransposition.decryptMessage(myKey, content) totalTime = round(time.time() - startTime, 2) print '%sion time: %s seconds' % (myMode.title(), totalTime) # write out translated message to a file outputFileObj = open(outputFilename, 'w') outputFileObj.write(translated) outputFileObj.close() print 'Done %sing %s (%s characters).' % (myMode, inputFilename, len(content)) print '%sed file is %s.' % (myMode.title(), outputFilename)
def main(): fileName = 'helloworld.txt' outputFileName = 'encryptedFile.txt' myKey = 10 encryptMode = 'encrypt' if not os.path.exists(fileName): print('The file %s does not exist.' % (fileName)) sys.exit() # If output file already exist the user can quit or continue to encrypt new file if os.path.exists(fileName): print('This will overwrite the file %s. (C)ontinue or (Quit)' % (outputFileName)) response = input('> ') # if response does not start with c exit the system if not response.lower().startswith('c'): sys.exit() # Read the message from the input file fileObj = open(fileName) content = fileObj.read() fileObj.close() print('%sing...' % (encryptMode.title())) # Measure how long the encryption and decryption takes startTime = time.time() if encryptMode == 'encrypt': translated = transpositionEncrypt.encryptMessage(myKey, content) elif encryptMode == 'decrypt': translated = decryptTransposition.decryptMessage(myKey, content) totalTime = round(time.time() - startTime, 2) print('%sion time:%s seconds' % (encryptMode.title(), totalTime)) # Write the translated message to the textfile outputFileObj = open(outputFileName, 'w') outputFileObj.write(translated) outputFileObj.close() print('Done %sing %s (%s characters).' % (encryptMode, fileName, len(content))) print('%sed file is %s.' % (encryptMode.title(), fileName))