凯撒加解密脚本

发布时间 2023-11-10 22:04:29作者: LC静一

# chr()接受一个整数,,返回该整数对应的Unicode字符,chr('A')=65,chr('中')=2013
# ord()接受一个字符,,返回该字符对应的Unicode编码,ord('65')=A,ord('2013')=中

 明文:

str = 'cdef'

加密

for j in range(0, 26):
    print(j, end=' ')
    for i in range(len(str)):
        if ord('a') <= ord(str[i]) <= ord('z'):
            print(chr((ord(str[i]) + j - ord('a')) % 26 + ord('a')), end='')
        else:
            print(chr((ord(str[i]) + j - ord('A')) % 26 + ord('A')), end='')
    print(end='\n')

解密

for j in range(0, 26):
    print(j, end=' ')
    for i in range(len(str)):
        if ord('a') <= ord(str[i]) <= ord('z'):
            print(chr((ord(str[i]) - j - ord('a')) % 26 + ord('a')), end='')
        else:
            print(chr((ord(str[i]) - j - ord('A')) % 26 + ord('A')), end='')
    print(end='\n')