Caesar cipher
Description: https://en.wikipedia.org/wiki/Caesar_cipher
#!/usr/bin/python # -*- coding: UTF-8 -*- # 恺撒挪移式密码法 ## 密码字母移动三位 def caesar_crypt(plain): a, z = ord('a'), ord('z') cipher = [] for char in plain: c = char if c != ' ': asc = ord(c) - 3 if asc < a: asc = z - (a - asc) + 1 c = chr(asc) cipher.append(c) return "".join(cipher) def caesar_decrypt(cipher): a, z = ord('a'), ord('z') plain = [] for char in cipher: c = char if c != ' ': asc = ord(c) + 3 if asc > z: asc = a + (asc - z) - 1 c = chr(asc) plain.append(c) return "".join(plain) def main(): plain = "the quick brown fox jumps over the lazy dog" print(plain) cipher = caesar_crypt(plain) print(cipher) plain = caesar_decrypt(cipher) print(plain) if __name__ == '__main__': main()