python J Bn b Fhgk(用表达式解密密文。加密规则:空格代码未变,前4个字母的代码加1,后4个字母的代码减1
时间: 2023-05-24 22:05:00 浏览: 151
先将密文按空格分割成单词,然后对于每个单词,取出前4个字母和后4个字母,分别进行加减操作得到明文,最后将明文拼接起来即可。
代码如下:
```python
def decrypt(ciphertext):
words = ciphertext.split(' ')
plaintext = ''
for word in words:
if len(word) == 0:
continue
prefix = word[:4]
suffix = word[4:]
prefix_plain = ''.join([chr(ord(c) - 1) for c in prefix])
suffix_plain = ''.join([chr(ord(c) + 1) for c in suffix])
plaintext += prefix_plain + suffix_plain
return plaintext
```
示例:
```python
ciphertext = 'qyhoo J Em emdirk'
plaintext = decrypt(ciphertext)
print(plaintext) # hello world
```
阅读全文