python凯撒密码加密文字块加密
时间: 2024-09-29 07:08:10 浏览: 56
Python中的凯撒密码是一种简单的替换式加密方法,也称为移位密码。它通过将明文中的每个字母按照固定数量的字母表顺序向右或向左移动来进行加密。例如,如果你选择将每个字母向右移动3位,A就会变成D,B变为E,以此类推。
下面是一个简单的Python函数,用于对单个字符进行凯撒密码加密:
```python
def caesar_cipher_encrypt(char, shift):
ascii_offset = 65 if char.isupper() else 97 # 对大写字母和小写字母处理 ASCII 码位置
encrypted_char = chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
return encrypted_char
# 示例:加密 'Hello, World!' 中的每个字符,偏移量为3
def encrypt_text(text, shift=3):
encrypted_text = ""
for char in text:
encrypted_text += caesar_cipher_encrypt(char, shift)
return encrypted_text
plaintext = "Hello, World!"
encrypted_text = encrypt_text(plaintext)
print(f"原文: {plaintext}\n加密后的文本: {encrypted_text}")
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)