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()

 

相关文章:

  • 2021-06-01
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2022-01-22
  • 2021-07-12
  • 2021-07-10
猜你喜欢
  • 2021-10-09
  • 2022-12-23
  • 2022-02-16
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2021-05-30
相关资源
相似解决方案