cipher = [(ord(char) ** key) % n for char in plaintext] TypeError: ord() expected string of length 1, but int found
时间: 2024-02-29 11:55:10 浏览: 22
这个错误提示是因为你在使用 ord() 函数时传入的参数不是一个单一字符的字符串。 ord() 函数用于将一个字符转换为对应的 ASCII 数值。请确保你传入的参数是一个单一字符的字符串,比如 'a' 或 '1',而不是一个整数或者一个长度大于 1 的字符串。如果你想要将一个整数转换为对应的字符,可以使用 chr() 函数。例如,chr(65) 返回的是字符 'A'。
相关问题
将该代码转化当C#窗体程序中using System; namespace VigenereCipher { class Program { static void Main(string[] args) { Console.WriteLine("请选择操作:\n1. 加密\n2. 解密"); int choice = int.Parse(Console.ReadLine()); Console.WriteLine("请输入明文/密文:"); string text = Console.ReadLine(); Console.WriteLine("请输入密钥:"); string key = Console.ReadLine(); if (choice == 1) { string cipherText = Encrypt(text, key); Console.WriteLine("加密后的结果为:" + cipherText); } else if (choice == 2) { string plainText = Decrypt(text, key); Console.WriteLine("解密后的结果为:" + plainText); } else { Console.WriteLine("输入错误!"); } } static string Encrypt(string text, string key) { string cipherText = ""; int keyIndex = 0; for (int i = 0; i < text.Length; i++) { char c = text[i]; int offset = key[keyIndex] - 'A'; char encryptedChar = (char)((c + offset) % 256); cipherText += encryptedChar; keyIndex = (keyIndex + 1) % key.Length; } return cipherText; } static string Decrypt(string text, string key) { string plainText = ""; int keyIndex = 0; for (int i = 0; i < text.Length; i++) { char c = text[i]; int offset = key[keyIndex] - 'A'; char decryptedChar = (char)((c - offset + 256) % 256); plainText += decryptedChar; keyIndex = (keyIndex + 1) % key.Length; } return plainText; } } }
以下是将该代码转化为C#窗体程序的示例代码:
```csharp
using System;
using System.Windows.Forms;
namespace VigenereCipher
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void encryptButton_Click(object sender, EventArgs e)
{
string text = inputTextBox.Text;
string key = keyTextBox.Text;
string cipherText = Encrypt(text, key);
outputTextBox.Text = cipherText;
}
private void decryptButton_Click(object sender, EventArgs e)
{
string text = inputTextBox.Text;
string key = keyTextBox.Text;
string plainText = Decrypt(text, key);
outputTextBox.Text = plainText;
}
static string Encrypt(string text, string key)
{
string cipherText = "";
int keyIndex = 0;
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
int offset = key[keyIndex] - 'A';
char encryptedChar = (char)((c + offset) % 256);
cipherText += encryptedChar;
keyIndex = (keyIndex + 1) % key.Length;
}
return cipherText;
}
static string Decrypt(string text, string key)
{
string plainText = "";
int keyIndex = 0;
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
int offset = key[keyIndex] - 'A';
char decryptedChar = (char)((c - offset + 256) % 256);
plainText += decryptedChar;
keyIndex = (keyIndex + 1) % key.Length;
}
return plainText;
}
}
}
```
在窗体设计器中添加一个`TextBox`控件用于输入明文/密文,一个`TextBox`控件用于输入密钥,一个`TextBox`控件用于显示加密/解密结果,以及两个`Button`控件分别用于加密和解密操作。
然后,将`encryptButton_Click`和`decryptButton_Click`事件处理程序中的代码替换为原来的`Main`方法中的代码即可。注意修改控件的名称以与代码中的名称相匹配。
import random def generate_random_text(length): # 生成随机明文 text = "" for i in range(length): text += chr(random.randint(ord('A'), ord('Z'))) return text def vigenere_cipher(text, key): # 将明文和密钥转换为大写字母 text = text.upper() key = key.upper() # 检查密钥是否合法 if not key.isalpha(): raise ValueError("密钥只能包含字母") # 初始化密文和密钥索引 cipher = "" key_index = 0 # 遍历明文中的每个字符 for char in text: if char.isalpha(): # 计算字符的偏移量 offset = ord(key[key_index]) - ord('A') key_index = (key_index + 1) % len(key) # 加密字符 if char.isupper(): cipher += chr((ord(char) - ord('A') + offset) % 26 + ord('A')) else: cipher += chr((ord(char) - ord('a') + offset) % 26 + ord('a')) else: # 不加密非字母字符 cipher += char return cipher # 生成随机明文并加密 text = generate_random_text(10) key = "SECRETKEY" cipher = vigenere_cipher(text, key) # 输出明文和密文 print("明文:", text) print("密文:", cipher)
这是一个使用维吉尼亚密码对随机生成的明文进行加密的 Python 代码。维吉尼亚密码是一种多表密码,通过将明文中的每个字符与密钥中对应位置的字符进行加密,从而实现加密的过程。在这个代码中,先生成了一个长度为 10 的随机明文,然后使用 "SECRETKEY" 作为密钥对明文进行加密,最后输出明文和密文。