zoukankan      html  css  js  c++  java
  • Security and Cryptography in Python

    Security and Cryptography in Python - Attack on Caesar Cipher

    Crypto Rule #1(Kerckhoffs' Principle)

    Eve should not be able to break the ciphers even when she knows the cipher.

    def generate_key(n):
        letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        key = {}
        cnt = 0
        for c in letters:
            key[c] = letters[(cnt + n) % len(letters)]
            cnt += 1
        return key
    
    def get_decryption_key(key):
        dkey = {}
        for c in key:
            dkey[key[c]] = c
        return dkey
    
    def encrypt(key, message):
        cipher = ""
        for c in message:
            if c in key:
                cipher += key[c]
            else:
                cipher += c
        return cipher
    
    # this is done by your enemy
    key = generate_key(3)
    print(key)
    message = "YOU ARE AWESOME"
    cipher = encrypt(key, message)
    
    # this is us breaking the cipher
    print(cipher)
    for i in range(26):
        dkey = generate_key(i)
        message = encrypt(dkey,cipher)
        print(message)
    

    Running result:

    image-20210131150033127

    相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
  • 相关阅读:
    杭电2042
    杭电2041
    杭电2040
    杭电2046
    SPOJ
    SPOJ
    SPOJ
    HDU
    HDU
    HDU
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/14352639.html
Copyright © 2011-2022 走看看