最基础的加密方法,将一个字符串中的所有字符按照其在字母表中的顺序向前或后移动shift位,得到密码
def shift_char(c, shift): if c.islower(): return chr(((ord(c)-97+shift)%26)+97) elif c.isupper(): return chr(((ord(c)-65+shift)%26)+65) else: return c # don not shift non-letters
def caesar_cipher(s, shift): m = "" for c in s: m += shift_char(c, shift) return m
text = "Thank You!" # the die has been cast print text, "becomes", caesar_cipher(text, 6)
|
阅读(977) | 评论(0) | 转发(0) |