码迷,mamicode.com
首页 > 编程语言 > 详细

Security and Cryptography in Python - Attack on Caesar Cipher

时间:2021-02-01 12:50:36      阅读:0      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!