Python密码学

简单替代密码测试

简单替代密码的测试详细操作教程
在本章中,我们将重点介绍使用各种方法测试替换密码,这有助于生成如下所示的随机字符串-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
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()

输出

您可以将输出观察为随机生成的字符串,这有助于生成随机的纯文本消息,如下所示-
字符串
测试成功完成后,我们可以观察到输出消息 替代测试通过!。
 Substitution
因此,您可以系统地破解替换密码。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4