Cryptography-with-python-testing-of-simple-substitution-cipher

提供:Dev Guides
移動先:案内検索

単純な置換暗号のテスト

この章では、以下に示すようにランダム文字列を生成するのに役立つさまざまな方法を使用して置換暗号をテストすることに焦点を当てます-

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 test pass !! を確認できます。

置換

したがって、体系的な方法で置換暗号をハッキングできます。