首页 > 编程语言 > 详细

Security and Cryptography in Python - Caesar Cipher

时间:2021-01-31 14:25:12      阅读:22      评论:0      收藏:0      [点我收藏+]

Security and Cryptography in Python - Caesar Cipher

Coding in Python

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 encrypt(key, message):
    cipher = ""
    for c in message:
        if c in key:
            cipher += key[c]
        else:
            cipher += c
    return cipher

key = generate_key(3)
print(key)
message = "YOU ARE AWESOME"
cipher = encrypt(key, message)
print(cipher)

Running result:

技术分享图片

Security and Cryptography in Python - Caesar Cipher

原文:https://www.cnblogs.com/keepmoving1113/p/14352385.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!