import numpy as np

def matrix_encrypt(plain_text, key): # 将明文转成矩阵形式 plain_matrix = np.array(list(plain_text)).reshape(-1, key.shape[0]) # 对矩阵进行乘法运算 cipher_matrix = np.dot(plain_matrix, key) % 26 # 将加密后的矩阵转成字符串形式 cipher_text = ''.join([chr(c + 65) for c in cipher_matrix.flatten()]) return cipher_text

def matrix_decrypt(cipher_text, key): # 将密文转成矩阵形式 cipher_matrix = np.array(list(cipher_text)).reshape(-1, key.shape[1]) # 对矩阵进行乘法运算 plain_matrix = np.dot(cipher_matrix, np.linalg.inv(key)) % 26 # 将解密后的矩阵转成字符串形式 plain_text = ''.join([chr(c + 65) for c in plain_matrix.flatten()]) return plain_text

加法密码:

```python
def additive_encrypt(plain_text, key):
    # 将明文转成数字序列
    plain_nums = [ord(c) - 65 for c in plain_text]
    # 对数字序列进行加法运算
    cipher_nums = [(n + key) % 26 for n in plain_nums]
    # 将加密后的数字序列转成字符串形式
    cipher_text = ''.join([chr(c + 65) for c in cipher_nums])
    return cipher_text

def additive_decrypt(cipher_text, key):
    # 将密文转成数字序列
    cipher_nums = [ord(c) - 65 for c in cipher_text]
    # 对数字序列进行加法运算
    plain_nums = [(n - key) % 26 for n in cipher_nums]
    # 将解密后的数字序列转成字符串形式
    plain_text = ''.join([chr(c + 65) for c in plain_nums])
    return plain_text

乘法密码:

def multiplicative_encrypt(plain_text, key):
    # 将明文转成数字序列
    plain_nums = [ord(c) - 65 for c in plain_text]
    # 对数字序列进行乘法运算
    cipher_nums = [(n * key) % 26 for n in plain_nums]
    # 将加密后的数字序列转成字符串形式
    cipher_text = ''.join([chr(c + 65) for c in cipher_nums])
    return cipher_text

def multiplicative_decrypt(cipher_text, key):
    # 将密文转成数字序列
    cipher_nums = [ord(c) - 65 for c in cipher_text]
    # 求逆元
    inv_key = np.linalg.inv(np.array([[key, 1], [0, 1]])) % 26
    # 对数字序列进行乘法运算
    plain_nums = [int(np.dot(np.array([n, 1]), inv_key) % 26) for n in cipher_nums]
    # 将解密后的数字序列转成字符串形式
    plain_text = ''.join([chr(c + 65) for c in plain_nums])
    return plain_text

仿射密码:

def affine_encrypt(plain_text, key):
    # 将明文转成数字序列
    plain_nums = [ord(c) - 65 for c in plain_text]
    # 对数字序列进行仿射变换
    cipher_nums = [(key[0] * n + key[1]) % 26 for n in plain_nums]
    # 将加密后的数字序列转成字符串形式
    cipher_text = ''.join([chr(c + 65) for c in cipher_nums])
    return cipher_text

def affine_decrypt(cipher_text, key):
    # 将密文转成数字序列
    cipher_nums = [ord(c) - 65 for c in cipher_text]
    # 求逆元
    inv_key = np.linalg.inv(np.array([[key[0], 1], [0, 1]])) % 26
    # 对数字序列进行仿射变换
    plain_nums = [int(np.dot(np.array([n - key[1], 1]), inv_key) % 26) for n in cipher_nums]
    # 将解密后的数字序列转成字符串形式
    plain_text = ''.join([chr(c + 65) for c in plain_nums])
    return plain_text
使用tkinter库制作软件内容:import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import numpy as np

class CipherApp:
    def __init__(self):
        self.window = tk.Tk()
        self.window.title('密码学应用')
        self.window.geometry('400x300')
        self.window.resizable(False, False)
        
        self.cipher_type = tk.StringVar(value='matrix')
        self.plain_text = tk.StringVar(value='')
        self.cipher_text = tk.StringVar(value='')
        self.key_entry1 = tk.Entry(self.window, width=3)
        self.key_entry2 = tk.Entry(self.window, width=3)
        self.key_label = tk.Label(self.window, text='')
        self.key_frame = tk.Frame(self.window)
        self.key_frame.grid(row=2, column=1, pady=10, sticky='w')
        
        self.create_widgets()
        
        self.window.mainloop()
    
    def create_widgets(self):
        # 密码类型选择
        type_label = tk.Label(self.window, text='密码类型:')
        type_label.grid(row=0, column=0, padx=10, pady=10, sticky='w')
        
        matrix_rb = ttk.Radiobutton(self.window, text='矩阵密码', value='matrix', variable=self.cipher_type, command=self.show_key_entry)
        matrix_rb.grid(row=0, column=1, padx=10, pady=10, sticky='w')
        
        additive_rb = ttk.Radiobutton(self.window, text='加法密码', value='additive', variable=self.cipher_type, command=self.show_key_entry)
        additive_rb.grid(row=1, column=1, padx=10, pady=10, sticky='w')
        
        multiplicative_rb = ttk.Radiobutton(self.window, text='乘法密码', value='multiplicative', variable=self.cipher_type, command=self.show_key_entry)
        multiplicative_rb.grid(row=2, column=0, padx=10, pady=10, sticky='w')
        
        affine_rb = ttk.Radiobutton(self.window, text='仿射密码', value='affine', variable=self.cipher_type, command=self.show_key_entry)
        affine_rb.grid(row=2, column=1, padx=10, pady=10, sticky='w')
        
        # 明文输入
        plain_label = tk.Label(self.window, text='明文:')
        plain_label.grid(row=3, column=0, padx=10, pady=10, sticky='w')
        
        plain_entry = tk.Entry(self.window, textvariable=self.plain_text)
        plain_entry.grid(row=3, column=1, padx=10, pady=10, sticky='w')
        
        # 密文输出
        cipher_label = tk.Label(self.window, text='密文:')
        cipher_label.grid(row=4, column=0, padx=10, pady=10, sticky='w')
        
        cipher_entry = tk.Entry(self.window, textvariable=self.cipher_text, state='readonly')
        cipher_entry.grid(row=4, column=1, padx=10, pady=10, sticky='w')
        
        # 加密/解密按钮
        encrypt_btn = tk.Button(self.window, text='加密', command=self.encrypt)
        encrypt_btn.grid(row=5, column=0, padx=10, pady=10, sticky='w')
        
        decrypt_btn = tk.Button(self.window, text='解密', command=self.decrypt)
        decrypt_btn.grid(row=5, column=1, padx=10, pady=10, sticky='w')
    
    def show_key_entry(self):
        '''根据密码类型显示对应的密钥输入框'''
        if self.cipher_type.get() == 'matrix':
            self.key_entry1.config(width=3)
            self.key_entry2.config(width=3)
            self.key_entry1.grid(row=0, column=1, padx=10, pady=10)
            self.key_label.config(text='密钥格式:2x2矩阵')
            self.key_label.grid(row=1, column=1, padx=10, pady=10)
        elif self.cipher_type.get() == 'additive':
            self.key_entry1.config(width=5)
            self.key_entry2.config(width=0)
            self.key_entry1.grid(row=0, column=1, padx=10, pady=10)
            self.key_label.config(text='密钥格式:一个数字')
            self.key_label.grid(row=1, column=1, padx=10, pady=10)
        elif self.cipher_type.get() == 'multiplicative':
            self.key_entry1.config(width=5)
            self.key_entry2.config(width=0)
            self.key_entry1.grid(row=0, column=1, padx=10, pady=10)
            self.key_label.config(text='密钥格式:一个数字')
            self.key_label.grid(row=1, column=1, padx=10, pady=10)
        elif self.cipher_type.get() == 'affine':
            self.key_entry1.config(width=5)
            self.key_entry2.config(width=5)
            self.key_entry1.grid(row=0, column=1, padx=10, pady=10)
            self.key_entry2.grid(row=0, column=2, padx=10, pady=10)
            self.key_label.config(text='密钥格式:两个数字')
            self.key_label.grid(row=1, column=1, padx=10, pady=10)
    
    def get_key(self):
        '''获取密钥'''
        if self.cipher_type.get() == 'matrix':
            a = int(self.key_entry1.get())
            b = int(self.key_entry2.get())
            c = int(self.key_entry2.get())
            d = int(self.key_entry1.get())
            return np.array([[a, b], [c, d]])
        elif self.cipher_type.get() == 'additive':
            return int(self.key_entry1.get())
        elif self.cipher_type.get() == 'multiplicative':
            return int(self.key_entry1.get())
        elif self.cipher_type.get() == 'affine':
            a = int(self.key_entry1.get())
            b = int(self.key_entry2.get())
            return (a, b)
    
    def encrypt(self):
        '''加密'''
        if self.plain_text.get() == '':
            messagebox.showwarning('警告', '明文不能为空!')
            return
        if self.cipher_type.get() == 'matrix':
            if self.key_entry1.get() == '' or self.key_entry2.get() == '':
                messagebox.showwarning('警告', '密钥不能为空!')
                return
        else:
            if self.key_entry1.get() == '':
                messagebox.showwarning('警告', '密钥不能为空!')
                return
        if self.cipher_type.get() == 'matrix':
            try:
                key = self.get_key()
            except Exception as e:
                messagebox.showerror('错误', '密钥格式不正确!')
                return
            if key.shape != (2, 2):
                messagebox.showerror('错误', '密钥格式不正确!')
                return
        else:
            try:
                key = self.get_key()
            except Exception as e:
                messagebox.showerror('错误', '密钥格式不正确!')
                return
        if self.cipher_type.get() == 'matrix':
            cipher_text = matrix_encrypt(self.plain_text.get(), key)
        elif self.cipher_type.get() == 'additive':
            cipher_text = additive_encrypt(self.plain_text.get(), key)
        elif self.cipher_type.get() == 'multiplicative':
            cipher_text = multiplicative_encrypt(self.plain_text.get(), key)
        elif self.cipher_type.get() == 'affine':
            cipher_text = affine_encrypt(self.plain_text.get(), key)
        self.cipher_text.set(cipher_text)
    
    def decrypt(self):
        '''解密'''
        if self.cipher_text.get() == '':
            messagebox.showwarning('警告', '密文不能为空!')
            return
        if self.cipher_type.get() == 'matrix':
            if self.key_entry1.get() == '' or self.key_entry2.get() == '':
                messagebox.showwarning('警告', '密钥不能为空!')
                return
        else:
            if self.key_entry1.get() == '':
                messagebox.showwarning('警告', '密钥不能为空!')
                return
        if self.cipher_type.get() == 'matrix':
            try:
                key = self.get_key()
            except Exception as e:
                messagebox.showerror('错误', '密钥格式不正确!')
                return
            if key.shape != (2, 2):
                messagebox.showerror('错误', '密钥格式不正确!')
                return
        else:
            try:
                key = self.get_key()
            except Exception as e:
                messagebox.showerror('错误', '密钥格式不正确!')
                return
        if self.cipher_type.get() == 'matrix':
            plain_text = matrix_decrypt(self.cipher_text.get(), key)
        elif self.cipher_type.get() == 'additive':
            plain_text = additive_decrypt(self.cipher_text.get(), key)
        elif self.cipher_type.get() == 'multiplicative':
            plain_text = multiplicative_decrypt(self.cipher_text.get(), key)
        elif self.cipher_type.get() == 'affine':
            plain_text = affine_decrypt(self.cipher_text.get(), key)
        self.plain_text.set(plain_text)

app = CipherApp()
Python 密码学应用:矩阵密码、加法密码、乘法密码和仿射密码