pycharm维吉尼亚密码随机输入一段明文加密
时间: 2023-07-19 16:19:57 浏览: 57
可以使用Python的 `random` 模块生成随机的明文,然后对其进行维吉尼亚密码加密。以下是一个示例代码:
```python
import random
def generate_random_text(length):
# 生成随机明文
text = ""
for i in range(length):
text += chr(random.randint(65, 90))
return text
# 生成随机明文并加密
text = generate_random_text(10)
key = "SECRETKEY"
cipher = vigenere_cipher(text, key)
# 输出明文和密文
print("明文:", text)
print("密文:", cipher)
```
这段代码使用 `generate_random_text` 函数生成指定长度的随机明文,然后使用 `vigenere_cipher` 函数对其进行加密。最后输出明文和密文。你可以在PyCharm中运行此代码并观察输出结果。
相关问题
pycharm playfair密码随机输入一段明文加密
好的,您可以使用如下代码实现 Playfair 密码的加密:
```python
import random
def generate_key():
# 生成 Playfair 密码的密钥矩阵
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
key = random.sample(alphabet, len(alphabet))
key_matrix = [key[i:i+5] for i in range(0, len(key), 5)]
return key_matrix
def playfair_cipher(text, key_matrix):
# 实现 Playfair 密码的加密
def get_coords(char):
# 获取字符在密钥矩阵中的坐标
for i, row in enumerate(key_matrix):
for j, col in enumerate(row):
if col == char:
return (i, j)
def get_char(coords):
# 根据坐标获取密钥矩阵中的字符
i, j = coords
return key_matrix[i][j]
def encrypt_pair(pair):
# 处理一对明文字符
a, b = pair
if a == b:
b = 'X'
a_coords, b_coords = get_coords(a), get_coords(b)
if a_coords[0] == b_coords[0]:
# 同行
return get_char((a_coords[0], (a_coords[1]+1)%5)) + get_char((b_coords[0], (b_coords[1]+1)%5))
elif a_coords[1] == b_coords[1]:
# 同列
return get_char(((a_coords[0]+1)%5, a_coords[1])) + get_char(((b_coords[0]+1)%5, b_coords[1]))
else:
# 不同行不同列
return get_char((a_coords[0], b_coords[1])) + get_char((b_coords[0], a_coords[1]))
# 去除空格和非字母字符,并将字母转换为大写形式
text = "".join(filter(str.isalpha, text.upper().replace("J", "I")))
# 将明文划分为一对一对的字符
pairs = [text[i:i+2] for i in range(0, len(text), 2)]
# 对每一对字符进行加密
encrypted = "".join(encrypt_pair(pair) for pair in pairs)
return encrypted
text = input("请输入明文:")
key_matrix = generate_key() # 随机生成密钥矩阵
encrypted = playfair_cipher(text, key_matrix)
print("加密后的密文为:", encrypted)
print("密钥矩阵为:")
for row in key_matrix:
print(row)
```
其中,`generate_key` 函数用于随机生成 Playfair 密码的密钥矩阵,`playfair_cipher` 函数实现了 Playfair 密码的加密,`text` 表示输入的明文。
您可以运行该代码,输入明文,程序会随机生成密钥矩阵并进行加密操作,并输出加密后的密文和密钥矩阵。
pycharm仿射密码随机输入一段明文加密或者密文解密
好的,我可以为您提供一些关于仿射密码的代码示例,并且您可以根据需要进行修改。
加密代码:
```
import random
# 定义明文和密文的字符集
plaintext_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ciphertext_chars = "XPMGTDHLYONZBWEARKJUFSCIQV"
# 随机生成密钥
a = random.randint(1, 25)
b = random.randint(1, 25)
# 定义仿射密码加密函数
def affine_encrypt(plaintext):
ciphertext = ""
for char in plaintext:
if char in plaintext_chars:
# 明文字符对应的索引
char_index = plaintext_chars.index(char)
# 仿射运算
cipher_index = (a * char_index + b) % 26
# 加密后的密文字符
cipher_char = ciphertext_chars[cipher_index]
ciphertext += cipher_char
else:
ciphertext += char
return ciphertext
# 获取用户输入的明文
plaintext = input("请输入明文:")
# 加密明文并输出
ciphertext = affine_encrypt(plaintext)
print("密文为:", ciphertext)
```
解密代码:
```
# 定义仿射密码解密函数
def affine_decrypt(ciphertext):
plaintext = ""
# 求逆元
a_inverse = 0
for i in range(26):
if (a * i) % 26 == 1:
a_inverse = i
break
for char in ciphertext:
if char in ciphertext_chars:
# 密文字符对应的索引
char_index = ciphertext_chars.index(char)
# 仿射运算的逆运算
plain_index = (a_inverse * (char_index - b)) % 26
# 解密后的明文字符
plain_char = plaintext_chars[plain_index]
plaintext += plain_char
else:
plaintext += char
return plaintext
# 获取用户输入的密文
ciphertext = input("请输入密文:")
# 解密密文并输出
plaintext = affine_decrypt(ciphertext)
print("明文为:", plaintext)
```
这是一个简单的仿射密码实现,您可以根据需要进行修改和扩展。