凯撒密码
class CaesarCipher:
def __init__(self, alphabet, shift):
self.alphabet = alphabet
self.asize = len(alphabet)
self.shift = shift
self._dict = {}
for i, c in enumerate(ALPHABET):
self._dict[c] = i
def _encode(self, c):
return self.alphabet[(self._dict[c]+self.shift)%self.asize]
def encode(self, s):
o = ''
for c in s:
o += self._encode(c)
return o
if __name__ == '__main__':
ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
cipher = CaesarCipher(ALPHABET, 3)
code = cipher.encode('hello')
print(code)
ROT13
即把 凯撒密码 的偏置参数设成 13,由于字母表的长度为 26,故 ROT13 既是加密算法,也是解密算法:
ROT13(ROT13(s))=s
ROT13(ROT13( s )) = s
ROT13(ROT13(s))=s
本文介绍了凯撒密码的实现,包括其Python实现类`CaesarCipher`,该类支持自定义字母表和偏移量。通过`encode`方法,可以对输入的字符串进行加密。特别地,ROT13是一种特殊的凯撒密码,偏移量为13,由于字母表长度为26,ROT13具有自我解密性质:ROT13(ROT13(s))=s。
2562

被折叠的 条评论
为什么被折叠?



