pythonrot123简单加密
时间: 2023-09-02 20:07:01 浏览: 32
Pythonrot123 简单加密是一种基于 ROT13 加密算法的简单加密方法,它将明文的每个字符都向后移动 13 个位置,得到密文。
以下是 Pythonrot123 简单加密的 Python 代码实现:
```python
def pythonrot123_encrypt(plaintext):
ciphertext = ""
for char in plaintext:
if char.isalpha():
if char.islower():
ciphertext += chr((ord(char) - 97 + 13) % 26 + 97)
else:
ciphertext += chr((ord(char) - 65 + 13) % 26 + 65)
else:
ciphertext += char
return ciphertext
```
使用方法如下:
```python
plaintext = "Hello, world!"
ciphertext = pythonrot123_encrypt(plaintext)
print(ciphertext) # Uryyb, jbeyq!
```
解密方法与加密方法相同,只需将移动的位数从 13 改为 -13 即可。
相关问题
维吉尼亚密码加密python123
维吉尼亚密码是一种多表代换密码,可以使用Python实现。
以下是一个简单的维吉尼亚密码加密程序:
```python
def vigenere_encrypt(plain_text, key):
cipher_text = ""
key_index = 0
for char in plain_text:
if char.isalpha():
key_char = key[key_index % len(key)]
key_index += 1
char_val = (ord(char.upper()) + ord(key_char.upper())) % 26
cipher_text += chr(char_val + ord('A'))
else:
cipher_text += char
return cipher_text
```
该程序接受两个参数:明文和密钥。它遍历明文中的每个字符,如果是字母,则使用密钥中对应位置的字符进行加密。加密操作是将明文字符和密钥字符转换为0到25之间的数字,相加后对26取余,再转换为字母。
下面是一个示例:
```python
plain_text = "python123"
key = "secret"
cipher_text = vigenere_encrypt(plain_text, key)
print(cipher_text)
```
输出:
```
XGKWIX123
```
这是使用密钥"secret"对明文"python123"进行加密后得到的密文。
python123维吉尼亚密码——加密
维吉尼亚密码是一种古典密码,它是通过对明文中的每个字母按照一定规则进行替换来进行加密的。下面是用 Python 实现维吉尼亚密码加密的代码:
```python
def vigenere_encrypt(plaintext, key):
ciphertext = ""
key_index = 0
for char in plaintext:
if char.isalpha():
# 将明文中的字母按照一定规则替换
shift = ord(key[key_index % len(key)].upper()) - 65
cipher_char = chr((ord(char.upper()) - 65 + shift) % 26 + 65)
ciphertext += cipher_char
key_index += 1
else:
ciphertext += char
return ciphertext
```
其中,`plaintext` 是明文,`key` 是密钥。这个函数会将明文中的每个字母按照一定规则替换成密文中的字母。具体来说,它会使用密钥中的字母来生成一个偏移量,然后将明文中的字母向右移动这个偏移量个位置,得到密文中的字母。如果明文中的某个字符不是字母,则直接将它添加到密文中。
以下是一个使用这个函数进行加密的例子:
```python
plaintext = "HELLO WORLD"
key = "SECRET"
ciphertext = vigenere_encrypt(plaintext, key)
print(ciphertext) # 输出:"ZICVTWQNGR"
```
在这个例子中,我们使用密钥 "SECRET" 对明文 "HELLO WORLD" 进行加密,得到了密文 "ZICVTWQNGR"。