凯撒密码(Caesar)

简介: 凯撒密码(Caesar)

凯撒密码(Caesar)

  • 加密对象: 大小写字母
  • 原理:
  • 是一种移位加密方法,给每个字母排个索引,字母a-z索引依次为0-25
  • 偏移量是多少就向后面移动多少
  • 如:密文"Abc",偏移量为3,加密后为"Def"。移位量即是密钥.
  • 代码
# write by 2021/6/26
UPPER_DIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LOWER_DIC = "abcdefghijklmnopqrstuvwxyz"
def encrypt_caesar(string, offset):
    ciphertext = ""
    for i in string:
        if i in UPPER_DIC:
            new_index = (UPPER_DIC.index(i) + offset) % 26
            ciphertext += UPPER_DIC[new_index]
        elif i in LOWER_DIC:
            new_index = (LOWER_DIC.index(i) + offset) % 26
            ciphertext += LOWER_DIC[new_index]
        else:
            ciphertext += i
    return ciphertext
def decrypt_caesar(string, offset):
    return encrypt_caesar(string=string, offset=26-offset)
if __name__ == '__main__':
    ciphertext = encrypt_caesar("Caesar is good!", 3)
    plaintext = decrypt_caesar(ciphertext, 3)
    print(f"{plaintext}: {ciphertext}")


目录
相关文章
|
3月前
|
存储 安全 网络安全
如何取安全的密码?
如何取安全的密码?
33 0
|
3月前
|
数据安全/隐私保护 C++
【C++】凯撒密码 实现加密与解密
【C++】凯撒密码 实现加密与解密
|
3月前
|
算法 安全 程序员
详解 DES加密技术 | 凯撒密码 | 栅栏密码
详解 DES加密技术 | 凯撒密码 | 栅栏密码
167 0
|
11月前
|
数据安全/隐私保护
【密码学】密码棒密码
【密码学】密码棒密码
250 0
|
数据安全/隐私保护
7-137 凯撒密码
7-137 凯撒密码
101 0
|
Linux 数据安全/隐私保护
普莱费尔密码(playfair)
普莱费尔密码(playfair)
139 0
|
机器学习/深度学习 Linux 数据安全/隐私保护
维吉尼亚密码(Vigenere)
维吉尼亚密码(Vigenere)
174 0
|
安全 算法 数据安全/隐私保护
C/C++编程题之简单密码
密码是我们生活中非常重要的东东,我们的那么一点不能说的秘密就全靠它了。哇哈哈. 接下来渊子要在密码之上再加一套密码,虽然简单但也安全。
|
数据安全/隐私保护
凯撒密码 (20 分)
凯撒密码 (20 分)
336 0
|
算法 数据安全/隐私保护 Python
Python编程:实现凯撒密码加密解密
Python编程:实现凯撒密码加密解密
646 0
Python编程:实现凯撒密码加密解密