Python凯撒加密解密字母字符
时间: 2023-05-21 20:04:37 浏览: 137
Python凯撒加密解密字母字符的方法如下:
加密:
```python
def caesar_cipher_encryption(string, shift):
letters = string.lower()
cipher_text = ''
for letter in letters:
if letter.isalpha():
new_letter = chr((ord(letter) - 97 + shift) % 26 + 97)
cipher_text += new_letter
else:
cipher_text += letter
return cipher_text
```
解密:
```python
def caesar_cipher_decryption(string, shift):
letters = string.lower()
plain_text = ''
for letter in letters:
if letter.isalpha():
new_letter = chr((ord(letter) - 97 - shift) % 26 + 97)
plain_text += new_letter
else:
plain_text += letter
return plain_text
```
其中,`string`表示要加密(解密)的字符串,`shift`表示字母移动的位数。如果`shift`是正整数,则表示加密,如果是负整数,则表示解密。
阅读全文