8、编写程序实现如下功能:对用户输入的原文(只能为大小写英文字母和阿拉伯数字),加密后输出密文。 原文和密文的对应关系为 原文:abc...xyzABC...XYZ012...789 密文:cde...zabCDE...ZAB123...890 注意:字母循环后错2位,数字循环后错1位 如:程序运行时输入HelloWorld911Zz,则输出显示: JgnnqYqtnf022Bb
时间: 2024-03-02 19:51:16 浏览: 48
可以使用 Python 语言实现该功能,代码如下:
```python
def encrypt(text):
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
shifted = 'cdefghijklmnopqrstuvwxyzabCDEFGHIJKLMNOPQRSTUVWXYZABcdefghijklmnopqrstuv0123456789wxyzab'
mapping = str.maketrans(alphabet, shifted)
return text.translate(mapping)
text = input("请输入原文:")
cipher = encrypt(text)
print("密文为:", cipher)
```
该程序中,`encrypt` 函数接收一个字符串 `text` 作为输入,返回加密后的密文。在函数内部,定义了原文和密文的对应关系 `alphabet` 和 `shifted`,并使用 `str.maketrans` 方法生成一个映射表 `mapping`,将原文中的字符映射为密文中的字符。最后,使用 `translate` 方法将原文中的字符替换为密文中的字符。
在主程序中,先通过 `input` 函数获取用户输入的原文,然后调用 `encrypt` 函数得到密文,最后输出密文。
相关问题
编写程序实现偷单的文 本加密功能:程序运行时接收用户输人的原文《只能为大小 写焚文字母和阿拉伯数宇),并转换为密文输出。以下是原文和密文的对应关系。 原文:abco..xyzABC...XYZ012...789 密文:cde...zabCDE...ZAB123...890
```python
def encrypt(text):
"""
将原文转换为密文
"""
result = ""
for char in text:
if char.isalpha():
if char.islower():
result += chr((ord(char) - ord('a') + 2) % 26 + ord('a'))
else:
result += chr((ord(char) - ord('A') + 2) % 26 + ord('A'))
elif char.isdigit():
result += str((int(char) + 2) % 10)
else:
result += char
return result
text = input("请输入原文:")
print("密文为:", encrypt(text))
```
其中,`ord()` 函数用于获取字符的 ASCII 码,`chr()` 函数用于获取指定 ASCII 码对应的字符。对于字母,先将其转换为 0-25 的数字,加上 2 后取模得到新的数字,再转换回字符。对于数字,先将其转换为整数,加上 2 后取模得到新的数字,再转换回字符串。对于其他字符,直接输出。
python实现维吉尼亚唯密文攻击破译并实现可视化界面输出明文和密钥,输入的密文可以是大小写字母
下面是一个Python实现维吉尼亚密码破译并可视化输出明文和密钥的示例代码:
```python
import tkinter as tk
from tkinter import messagebox
import string
class VigenereDecryptor:
def __init__(self, ciphertext):
self.ciphertext = ciphertext.upper()
self.alphabet = string.ascii_uppercase
self.freqs = [0] * 26
self.key_length = 0
self.key = ""
self.plaintext = ""
def kasiski_test(self):
repeats = {}
for i in range(len(self.ciphertext) - 2):
substr = self.ciphertext[i:i+3]
if substr in repeats:
repeats[substr].append(i)
else:
repeats[substr] = [i]
distances = {}
for substr, locations in repeats.items():
if len(locations) > 1:
distances[substr] = []
for i in range(len(locations) - 1):
distance = locations[i+1] - locations[i]
distances[substr].append(distance)
factors = []
for substr, dists in distances.items():
for dist in dists:
for factor in range(2, dist+1):
if dist % factor == 0:
factors.append(factor)
self.key_length = max(set(factors), key=factors.count)
def frequency_analysis(self, start):
freqs = [0] * 26
for i in range(start, len(self.ciphertext), self.key_length):
c = self.ciphertext[i]
if c in self.alphabet:
freqs[ord(c) - ord('A')] += 1
shift = freqs.index(max(freqs)) - ord('E')
self.key += chr(shift + ord('A'))
def decrypt(self):
for i in range(self.key_length):
self.key += self.frequency_analysis(i)
for i in range(len(self.ciphertext)):
c = self.ciphertext[i]
k = self.key[i % self.key_length]
shift = self.alphabet.index(k)
table = self.alphabet[shift:] + self.alphabet[:shift]
if c in self.alphabet:
p = table.index(c)
self.plaintext += self.alphabet[p]
else:
self.plaintext += c
class VigenereGUI:
def __init__(self):
self.root = tk.Tk()
self.root.title("Vigenere Decryptor")
self.root.geometry("500x300")
self.ciphertext_label = tk.Label(self.root, text="Ciphertext:")
self.ciphertext_label.pack()
self.ciphertext_entry = tk.Entry(self.root, width=50)
self.ciphertext_entry.pack()
self.decrypt_button = tk.Button(self.root, text="Decrypt", command=self.decrypt)
self.decrypt_button.pack()
self.key_label = tk.Label(self.root, text="Key:")
self.key_label.pack()
self.key_entry = tk.Entry(self.root, width=50)
self.key_entry.pack()
self.plaintext_label = tk.Label(self.root, text="Plaintext:")
self.plaintext_label.pack()
self.plaintext_entry = tk.Entry(self.root, width=50, state="readonly")
self.plaintext_entry.pack()
def decrypt(self):
ciphertext = self.ciphertext_entry.get()
if not ciphertext:
messagebox.showerror("Error", "Ciphertext cannot be empty")
return
decryptor = VigenereDecryptor(ciphertext)
decryptor.kasiski_test()
decryptor.decrypt()
self.key_entry.delete(0, tk.END)
self.key_entry.insert(0, decryptor.key)
self.plaintext_entry.configure(state="normal")
self.plaintext_entry.delete(0, tk.END)
self.plaintext_entry.insert(0, decryptor.plaintext)
self.plaintext_entry.configure(state="readonly")
def run(self):
self.root.mainloop()
gui = VigenereGUI()
gui.run()
```
这个代码包括了一个基本的GUI界面,可以输入密文并解密。解密过程中,会自动计算密钥长度和密钥,并输出明文和密钥。注意,这个代码只能处理大写字母的维吉尼亚密码。如果需要处理小写字母或其他字符,需要进行一些修改。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)