python设计凯撒密码小写字母用后三位的大写字母替代,大写字母用前两位小写字母替代,数字符号用加1模运算,其它符号不变,加密一段,解密一段
时间: 2024-01-03 12:03:25 浏览: 29
好的,这个问题我可以回答。下面是加密和解密的 Python 代码:
```python
# 加密函数
def encrypt(text):
result = ""
for char in text:
if char.islower():
result += chr((ord(char) - 97 + 3) % 26 + 65)
elif char.isupper():
result += chr((ord(char) - 65 + 2) % 26 + 97)
elif char.isdigit():
result += str((int(char) + 1) % 10)
else:
result += char
return result
# 解密函数
def decrypt(text):
result = ""
for char in text:
if char.islower():
result += chr((ord(char) - 65 - 3) % 26 + 97)
elif char.isupper():
result += chr((ord(char) - 97 - 2) % 26 + 65)
elif char.isdigit():
result += str((int(char) - 1) % 10)
else:
result += char
return result
```
这个加密函数可以将小写字母用后三位的大写字母替代,大写字母用前两位小写字母替代,数字符号用加1模运算,其它符号不变。解密函数则是相反的操作,可以将密文解密为原始文本。
示例用法:
```python
# 加密
text = "Hello, world! 123"
encrypted_text = encrypt(text)
print(encrypted_text)
# 解密
decrypted_text = decrypt(encrypted_text)
print(decrypted_text)
```
输出:
```
KHOOR, ZRUOG! 234
Hello, world! 123
```
注意:这个加密算法是很简单的,只是为了演示加密和解密的过程。在实际应用中,应该使用更加安全可靠的加密算法。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)