📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace RSA
{
public partial class Form1 : Form
{
Random ran = new Random();
int RandomKey = 1234567890;
string Key;
byte[] S = new byte[256]; //状态向量S
byte[] T = new byte[256]; //临时向量T
byte[] K = new byte[16] { 15, 79, 11, 203, 9, 48, 39, 19, 22, 93, 52, 234, 106, 148, 194, 210 };
int kenlen=16;
String Plain;
String Cipher;
String PlainAfterDecrypt;
public Form1()
{
InitializeComponent();
}
private void btnInsert_Click(object sender, EventArgs e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
string filePath = openFileDialog1.FileName;
using (StreamReader reader = new StreamReader(filePath))
{
while (reader.Peek() > -1)
{
string lines = reader.ReadToEnd();
tMessage.Text = lines;
}
}
myStream.Close();
}
}
}
private void btnMessageClear_Click(object sender, EventArgs e)
{
RandomKey = ran.Next(1,99999);
Key = Convert.ToString(RandomKey);
kenlen = Key.Length;
K = System.Text.ASCIIEncoding.ASCII.GetBytes(Key);
}
private void btnGetKey_Click(object sender, EventArgs e)
{
tMessage.Text = "";
}
private void btnCrypt_Click(object sender, EventArgs e)
{
if (tMessage.Text == "")
{
MessageBox.Show("请输入需要加密的消息");
return;
}
Plain = tMessage.Text;
int PlainLength = tMessage.Text.Length;
int i, j;
//状态向量S的初始化
for (i = 0; i < 256; i++)
{
S[i] = (byte)i;
T[i] = K[i % kenlen];
}
j = 0;
byte temp;
for (i = 0; i < 256; i++)
{
j = (j + (int)S[i] + T[i]) % 256;
temp = S[i];
S[i] = S[j];
S[j] = temp;
}
//状态向量S的初始化结束
i = 0;
j = 0;
int t;
char k;
int n = 0;
while (PlainLength!=0)
{
i = (i + 1) % 256;
j = (j + S[i]) % 256;
temp = S[i];
S[i] = S[j];
S[j] = temp;
t= (S[i] + S[j]) % 256;
k = (char)S[t];
int Ascii = Plain[n] ^ k;
char r=(char)Ascii;
//r = String.Format("{0}", (Plain[n] ^ k));
Cipher += r;
n++;
PlainLength--;
}
tCipher.Text = Cipher;
}
private void btnCipherClear_Click(object sender, EventArgs e)
{
tCipher.Text = "";
}
private void btnDeCrypt_Click(object sender, EventArgs e)
{
if (tCipher.Text == "")
{
MessageBox.Show("请输入需要解密的信息");
return;
}
tResult.Text = "";
// Cipher = tCipher.Text;
int CipherLength = Cipher.Length;
int i, j;
//状态向量S的初始化
for (i = 0; i < 256; i++)
{
S[i] = (byte)i;
T[i] = K[i % kenlen];
}
j = 0;
byte temp;
for (i = 0; i < 256; i++)
{
j = (j + (int)S[i] + T[i]) % 256;
temp = S[i];
S[i] = S[j];
S[j] = temp;
}
//状态向量S的初始化结束
i = 0;
j = 0;
int t;
char k;
int n = 0;
while ( CipherLength!=0)
{
i = (i + 1) % 256;
j = (j + S[i]) % 256;
temp = S[i];
S[i] = S[j];
S[j] = temp;
t = (S[i] + S[j]) % 256;
k = (char)S[t];
int Ascii = Cipher[n] ^ k;
char r = (char)Ascii;
//r = String.Format("{0}", (Plain[n] ^ k));
PlainAfterDecrypt += r;
n++;
CipherLength--;
}
tResult.Text = PlainAfterDecrypt;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -