用python表达式解密密文,减密规则:空格代码未变,前4个字母的代码减1,后4个字母的代码加1:J Bn b Fhqk
时间: 2024-05-05 10:16:14 浏览: 14
代码如下:
```
ciphertext = "J Bn b Fhqk"
plaintext = ""
for i in range(len(ciphertext)):
if ciphertext[i] == " ":
plaintext += " "
elif i < 4:
plaintext += chr(ord(ciphertext[i]) - 1)
else:
plaintext += chr(ord(ciphertext[i]) + 1)
print(plaintext)
```
输出为 "I Am a Egpj",即解密后的明文。
相关问题
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
```
python用表达式解密密文 加密规则:空格代码未变,前四个字母的代码加1,后四个字母的代码减1
假设密文为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))
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)