📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.Xml;
using System.IO;
using productLicense;
using readLicense;
namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 8);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(864, 24);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 48);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(864, 24);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
//
// button1
//
this.button1.Location = new System.Drawing.Point(280, 256);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "加密";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(376, 256);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "生成钥匙对";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(8, 192);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(488, 21);
this.textBox3.TabIndex = 5;
this.textBox3.Text = "公钥";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(8, 216);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(488, 21);
this.textBox4.TabIndex = 6;
this.textBox4.Text = "密钥";
//
// button3
//
this.button3.Location = new System.Drawing.Point(176, 256);
this.button3.Name = "button3";
this.button3.TabIndex = 7;
this.button3.Text = "解密";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(376, 288);
this.button4.Name = "button4";
this.button4.TabIndex = 8;
this.button4.Text = "button4";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(896, 317);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//输入 RSA Key 信息. 包括 public key 信息.
RSA.ImportParameters(RSAKeyInfo);
//加密这个字节数组和是否用 OAEP 来填充
//OAEP 填充仅仅在Microsoft Windows XP 或者
//以后的版本中有用
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}
catch(CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)
{
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
//输入 RSA Key 信息. 这个需要包括
// private key 信息
RSA.ImportParameters(RSAKeyInfo);
//解密这个字节数组和是否用 OAEP 填充.
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
catch(CryptographicException e)
{
Console.WriteLine(e.ToString());
return null;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
// byte[] dataToEncrypt ;
byte[] encryptedData;
// byte[] decryptedData;
//创建一个 RSACryptoServiceProvider 的实例来产生公共和私有密钥数据
RSAPriEncrypt obj=new RSAPriEncrypt();
obj.xmldataToEncrypted=this.textBox1.Text ;
obj.encrypt() ;
this.textBox2.Text=obj.xmldataEncrypted;
//加密结果,用数字表示出来,还原的时候必须先转换成正确的byte[]数组
//this.textBox2.Text =cvtByteArraytoStr(encryptedData);
}
private void button2_Click(object sender, System.EventArgs e)
{
RSACryptoServiceProvider crypt=new RSACryptoServiceProvider();
//DSACryptoServiceProvider crypt=new DSACryptoServiceProvider();
this.textBox3.Text =crypt.ToXmlString(false);//pubkey
this.textBox4.Text =crypt.ToXmlString(true);//pubkey+prikey
crypt.Clear ();
}
private void button3_Click(object sender, System.EventArgs e)
{
// byte[] encryptedData,decryptedData;
//
// RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
// RSA.FromXmlString(this.textBox4.Text );
// encryptedData=Convert.FromBase64String(this.textBox2.Text);
// //cvtStrtoByteArray(this.textBox2.Text,' ',encryptedData);
// decryptedData=RSA.Decrypt(encryptedData,false);
// this.textBox1.Text=Encoding.Unicode.GetString(decryptedData,0,decryptedData.Length);
// //显示加密结果
// //this.textBox1.Text =cvtByteArraytoStr(decryptedData);
//
readLicense.RSAPriDecrypt obj=new readLicense.RSAPriDecrypt();
obj.xmldataToDecrypted=this.textBox2.Text ;
obj.decrypt();
this.textBox1.Text =obj.xmldataDecrypted;
}
private void button4_Click(object sender, System.EventArgs e)
{
}
private bool cvtStrtoByteArray(string str,char divChar,byte[] byteArray)
{
string tempChar="";
int j=0;
for(int i=0;i<str.Length ;i++)
{
if( str[i]!=divChar)
{
tempChar=tempChar+str[i];
}
else
{
if (tempChar.Length >0)
{
byteArray[j]=byte.Parse(tempChar);
j=j+1;
tempChar="";
}
}
}
if (tempChar.Length >0)
byteArray[j]=byte.Parse(tempChar);
return true;
}
private string cvtByteArraytoStr(byte[] byteArray)
{
string strCode="";
for(int i = 0; i < byteArray.Length; i++)
{
strCode +=Convert.ToChar(byteArray[i]);
}
return strCode;
}
private bool comparebytes(byte[] b1,byte[] b2)
{
bool t=true;
for (int i=0;i<b1.Length ;i++)
{
if (b1[i]!=b2[i])
{
t=false;
}
}
return t;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -