python中的caesar,Python中的split

发布时间:2022-11-18

本文目录一览:

1、求python 凯撒密码 加码和解码 2、如何用python编写凯撒密码 ? 3、python编写凯撒密码!!!求大神帮助!!!

求python 凯撒密码 加码和解码

输入:CAT 输出:DBU

import string
def caesar_shift(s):
    # Write your code here
    # To print results to the standard output you can use print
    # Example: print "Hello world!"
    table = string.maketrans(string.ascii_uppercase, string.ascii_uppercase[1:] + string.ascii_uppercase[:1]) #1表示加密时右移1位
    print s.translate(table)
caesar_shift("CAT")

str.maketrans()是创建一个字符翻译表,而str.translate()就是根据这个翻译表,翻译这个字符串。(比按asc码求模更简洁) string.translate(table [, deletechars]) string.maketrans(intab, outtal) 如果解决了您的问题请采纳! 如果未解决请继续追问

如何用python编写凯撒密码 ?

凯撒密码是对字母表整体进行偏移的一种变换加密。因此,建立一个字母表,对明文中每个字母,在这个字母表中偏移固定的长度即可得到对应的密文字母。 最基本的实现如下:

def caesarcipher(s: str, rot: int=3) -> str:
    _ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    encode = ''
    i = 0
    for c in s:
        try:
            encode += _[(_.index(c.upper()) + rot) % len(_)]
        except (Exception,) as e:
            encode += c
    return encode
print(caesarcipher('hellow'))
print(caesarcipher('KHOORZ', -3))

如果要求解密后保持大小写,那么,字母表_还需要包含所有小写字母并且index时不对c做upper处理. 同样的,也可以在字母表中追加数字,各种符号,空格等.

python编写凯撒密码!!!求大神帮助!!!

exec('moveLength = int(raw_input("Input raw_input amount, expect a int number"))\nprint "".join([dict(zip(list("abcdefghijklmnopqrstuvwxyz"), [list("abcdefghijklmnopqrstuvwxyz")[(i + moveLength) % 26] for i in range(26)]))[x] for x in list(raw_input("String to change:"))])')

楼主分没给够, 所以只能看到这样的代码喽……o((≧▽≦o) ......