谁有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 阅读 6 次 更新于 2025-07-20 21:19:57 我来答关注问题0
  • 在Python2.7.10中编写凯撒密码加密程序,首先需要从用户获取输入文本。具体代码如下:s = raw_input('[开始加密]pleaseinputyourstr:')s = list(s)n = 0 for sw in s:s[n] = chr(ord(sw) + 3)n = n + 1 sout = ''for sw2 in s:sout = sout + sw2 print '[加密结果]:',s...

  • Python凯撒密码编写程序的实现可以通过内置的ord和chr函数来完成。ord函数可以将字符转化为对应的ASCII码,而chr函数则可以将ASCII码转化为对应的字符。通过这两个函数,我们可以很方便地对字符进行移位操作,从而实现凯撒密码的加密和解密。以下是一个简单的凯撒密码加密程序的示例:python def caesar_encrypt(...

  •  雁西楼 如何用python编写凯撒密码 ?

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

  •  誉祥祥知识 python凯撒密码编写程序详解

    Python凯撒密码编写程序详解:加密程序:函数定义:定义一个名为caesar_encrypt的函数,该函数接受两个参数:需要加密的文本text和移位的距离shift。初始化结果字符串:在函数内部,初始化一个空字符串result,用于存储加密后的结果。遍历文本字符:使用for循环遍历文本中的每个字符。字母字符处理:如果字符是字...

  • } n++;/* if some extreme situation, maybe dead loop */ if ( n > 1000000){ h->collision_times += n;memcpy(h->save_info_base + 16, &h->collision_times,8);return -2;}

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

Python相关话题

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