C#设计一个能实现ROT13加密和解密的窗体程序
时间: 2024-02-21 22:00:47 浏览: 21
好的,以下是一个实现ROT13加密和解密的C#窗体程序:
```csharp
using System;
using System.Windows.Forms;
namespace ROT13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonEncrypt_Click(object sender, EventArgs e)
{
string input = textBoxInput.Text;
string output = ROT13(input);
textBoxOutput.Text = output;
}
private void buttonDecrypt_Click(object sender, EventArgs e)
{
string input = textBoxInput.Text;
string output = ROT13(input);
textBoxOutput.Text = output;
}
private string ROT13(string input)
{
char[] arr = input.ToCharArray();
for (int i = 0; i < arr.Length; i++)
{
char c = arr[i];
if (c >= 'a' && c <= 'z')
{
c = (char)(((c - 'a') + 13) % 26 + 'a');
}
else if (c >= 'A' && c <= 'Z')
{
c = (char)(((c - 'A') + 13) % 26 + 'A');
}
arr[i] = c;
}
return new string(arr);
}
}
}
```
在窗体上添加两个文本框和两个按钮,分别为`textBoxInput`、`textBoxOutput`、`buttonEncrypt`和`buttonDecrypt`。在`buttonEncrypt_Click`和`buttonDecrypt_Click`事件中,分别调用`ROT13`方法进行加密和解密。`ROT13`方法将输入字符串中的每个字母都进行ROT13加密或解密,并返回处理后的字符串。