def main(): for i in range(1000): key = substitution.getRandomKey() message = random_string() print('Test %s: String: "%s.."' % (i + 1, message[:50])) print("Key: " + key) encrypted = substitution.translateMessage(message, key, 'E') decrypted = substitution.translateMessage(encrypted, key, 'D') if decrypted != message: print('ERROR: Decrypted: "%s" Key: %s' % (decrypted, key)) sys.exit() print('Substutition test passed!')
def decryptWithCipherletterMapping(ciphertext, letterMapping): # Return a string of the ciphertext decrypted with the letter mapping, # with any ambiguous decrypted letters replaced with an _ underscore. # First create a simple sub key from the letterMapping mapping. key = ['x'] * len(LETTERS) for cipherletter in LETTERS: if len(letterMapping[cipherletter]) == 1: # If there's only one letter, add it to the key. keyIndex = LETTERS.find(letterMapping[cipherletter][0]) key[keyIndex] = cipherletter else: ciphertext = ciphertext.replace(cipherletter.lower(), '_') ciphertext = ciphertext.replace(cipherletter.upper(), '_') key = ''.join(key) # With the key we've created, decrypt the ciphertext. return substitution.translateMessage(ciphertext, key, 'D')