Security and Cryptography in Python - Attack on Caesar Cipher

2021-03-04 13:26

阅读:424

标签:rck   att   lse   lan   bre   security   shang   letter   this   

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:

技术图片

Security and Cryptography in Python - Attack on Caesar Cipher

标签:rck   att   lse   lan   bre   security   shang   letter   this   

原文地址:https://www.cnblogs.com/keepmoving1113/p/14352639.html


评论


亲,登录后才可以留言!