用python编写一个程序,输入一个字符串,对这个字符串的进行简单加密和解密,并输出加密和解密后的结果(加密规则为A->D,B->E,...,X->A,Y->B,Z->C
时间: 2023-06-04 15:02:06 浏览: 678
这是一个技术问题,我可以为您解答。以下是用Python编写一个简单加密和解密程序的代码:
```
def encrypt(text):
result = ''
for char in text:
if char.isalpha():
char_code = ord(char)
if char_code >= ord('X') and char_code <= ord('Z'):
char_code -= 23
else:
char_code += 3
result += chr(char_code)
else:
result += char
return result
def decrypt(text):
result = ''
for char in text:
if char.isalpha():
char_code = ord(char)
if char_code >= ord('A') and char_code <= ord('C'):
char_code += 23
else:
char_code -= 3
result += chr(char_code)
else:
result += char
return result
# 调用示例
plain_text = 'hello world'
encrypted_text = encrypt(plain_text)
print(encrypted_text) # 'khoor zruog'
decrypted_text = decrypt(encrypted_text)
print(decrypted_text) # 'hello world'
```
注意,这个加密算法是循环移位算法,我们可以通过 ASCII 码值进行加密和解密。如果字符不属于字母表,则直接原样返回。如果您对代码不熟悉或不理解,请告诉我,我可以给您详细解释。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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://img-home.csdnimg.cn/images/20241231045053.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)