简单替换密码的测试

在本章中,我们将重点介绍如何使用各种方法测试替换密码,这有助于生成随机字符串,如下面所示 :

import random, string, substitution
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 random_string(size = 5000, chars = string.ascii_letters + string.digits):
   return ''.join(random.choice(chars) for _ in range(size))
if __name__ == '__main__':
   main()

输出

您可以随机观察输出生成的字符串有助于生成随机纯文本消息,如下所示 :

Strings

测试成功完成后,我们可以观察输出消息替换测试通过!.

替换

因此,您可以系统地破解替换密码.