谁有PYTHON编写的凯撒密码的加密和解密代码?

给你写了一个.def convert(c, key, start = 'a', n = 26):a = ord(start)offset = ((ord(c) - a + key)%n)return chr(a + offset)def caesarEncode(s, key):o = ""for c in s:if c.islower():o+= convert(c, key, 'a')elif c.isupper():o+= convert(c, key, ...
谁有PYTHON编写的凯撒密码的加密和解密代码?
for x in range(0,len(C)):
#输入大写字母
if ord(C[x])<=ord("Z") and ord(C[x])-3>=ord("A"):
print chr(ord(C[x])-3),

elif ord(C[x])-3<ord("A") and ord(C[x])<=ord("Z"):
print chr(ord("Z")+ord(C[x])-3-ord("A")+1),2012-04-05
给你写了一个.

def convert(c, key, start = 'a', n = 26):
a = ord(start)
offset = ((ord(c) - a + key)%n)
return chr(a + offset)
def caesarEncode(s, key):
o = ""
for c in s:
if c.islower():
o+= convert(c, key, 'a')
elif c.isupper():
o+= convert(c, key, 'A')
else:
o+= c
return o
def caesarDecode(s, key):
return caesarEncode(s, -key)
if __name__ == '__main__':
key = 3
s = 'Hello world!'
e = caesarEncode(s, key)
d = caesarDecode(e, key)
print e
print d

运行结果:
Khoor zruog!
Hello world!2012-04-01
编码
>>> enc=lambda t,n:''.join([chr(ord(c)+n) for c in t])
解码
>>> dec=lambda t,n:''.join([chr(ord(c)-n) for c in t])

>>> t="hello world"
>>> t
'hello world'

编码
>>> c=enc(t,5)
>>> c
'mjqqt%|twqi'

解码
>>> dec(c,5)
'hello world'2012-04-01
mengvlog 阅读 121 次 更新于 2025-09-11 13:04:54 我来答关注问题0
  •  雁西楼 如何用python编写凯撒密码 ?

    凯撒密码是对字母表整体进行偏移的一种变换加密。因此,建立一个字母表,对明文中每个字母,在这个字母表中偏移固定的长度即可得到对应的密文字母。最基本的实现如下:def caesarcipher(s: str,rot: int=3) ->str: _ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' encode = '' i = 0 for c in ...

  •  qq565143480 python凯撒密码实现

    输入字符串, 输出凯撒加密后字符串def kaisa(string): result = [] for i in range(len(string)): if string[i] in dict_kaisa.keys(): result.append(dict_kaisa[string[i]])

  • anonymous python编程 凯撒密码

    我觉得是因为你那句if, 'Z'的ascii码(十进制)为90, 而 'z'对应的为122。语句判断如果输入的字符串的值+前面写的值大于'Z'(90)并且大于'z'(122),也就是说得到的值必须大于122才进行之后的-26的操作,由于90+5

  • 凯撒密码的核心原理可以概括为“替换”。以我们的字母表为例,若位移量设定为3,则字母A将被替换为字母D,字母B替换为字母E,以此类推。以下是使用Python编写的凯撒密码加密和解密功能的完整代码,以及运行过程和结果。完整代码

  •  文暄生活科普 进行简易的凯撒密码加密

    凯撒密码是一种基础加密方法,其核心在于字母表上的位移。以偏移量3为例,字母A将变换成D,B变为E,以此类推。实现该加密过程并不复杂,以下是一个在Python中执行此操作的代码片段。加密过程:将字母向后移动偏移量个位置。解密过程:将字母向前移动相同偏移量个位置。已验证,该方法可行。处理大量文本...

檬味博客在线解答立即免费咨询

Python相关话题

Copyright © 2023 WWW.MENGVLOG.COM - 檬味博客
返回顶部