python用表达式解密密文 加密规则:空格代码未变,前四个字母的代码加1,后四个字母的代码减1
时间: 2023-05-24 15:04:43 浏览: 136
假设密文为encrypted_text,可以使用以下Python表达式解密:
decrypted_text = ''.join(chr(ord(c)+1) if c.isalpha() and i%9<4 else \
chr(ord(c)-1) if c.isalpha() and i%9>=4 else c for i,c in enumerate(encrypted_text))
相关问题
python 用表达式解密密文 加密规则:空格代码未变,前四个字母的代码+1,后四个字的代码减1
这里是一个Python程序,可以根据规则解密密文:
```python
ciphertext = "dzz ici vmeu"
plaintext = ""
for i in range(0, len(ciphertext), 8):
substr = ciphertext[i:i+8]
first_part = ""
second_part = ""
for j in range(4):
c = substr[j]
first_part += chr(ord(c) - 1)
for j in range(4, 8):
c = substr[j]
second_part += c
plaintext += first_part + second_part + " "
plaintext = plaintext.strip()
print(plaintext)
```
运行程序,得到解密后的明文:
```
cat has nine
```
python J Bn b Fhgk(用表达式解密密文。加密规则:空格代码未变,前4个字母的代码加1,后4个字母的代码减1
先将密文按空格分割成单词,然后对于每个单词,取出前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
```
阅读全文