📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace WindowsApplication4
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <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.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 136);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "生成";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 136);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "验证";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
byte[] Hash = {59,4,248,102,77,97,142,201,210,12,224,93,25,41,100,197,213,134,130,135};
byte[] SignedHash ;
private void button1_Click(object sender, System.EventArgs e)
{
try
{
string path = @".\temp\PrivateKey.dat";
RSAParameters gRSAKeyInfo = new RSAParameters();
if (File.Exists(path))
{
// Open the stream and read it back.
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] bDInfo = new byte[128];
byte[] bDPInfo = new byte[64];
byte[] bDQInfo = new byte[64];
byte[] bExponentinfo = new byte[3];
byte[] bInverseQinfo = new byte[64];
byte[] bModulusinfo = new byte[128];
byte[] bPInfo = new byte[64];
byte[] bQInfo = new byte[64];
UnicodeEncoding temp = new UnicodeEncoding();
BinaryReader r = new BinaryReader(fs);
// Read data from PrivateKey.dat.
for (int i =0 ; i < 128; i++)
{
bDInfo[i] = r.ReadByte();
}
for (int i =0 ; i < 64; i++)
{
bDPInfo[i] = r.ReadByte();
}
for (int i =0 ; i < 64; i++)
{
bDQInfo[i] = r.ReadByte();
}
for (int i =0 ; i < 3; i++)
{
bExponentinfo[i] = r.ReadByte();
}
for (int i =0 ; i < 64; i++)
{
bInverseQinfo[i] = r.ReadByte();
}
for (int i =0 ; i < 128; i++)
{
bModulusinfo[i] = r.ReadByte();
}
for (int i =0 ; i < 64; i++)
{
bPInfo[i] = r.ReadByte();
}
for (int i =0 ; i < 64; i++)
{
bQInfo[i] = r.ReadByte();
}
r.Close();
gRSAKeyInfo.D = bDInfo;
gRSAKeyInfo.DP = bDPInfo;
gRSAKeyInfo.DQ = bDQInfo;
gRSAKeyInfo.Exponent = bExponentinfo;
gRSAKeyInfo.InverseQ = bInverseQinfo;
gRSAKeyInfo.Modulus = bModulusinfo;
gRSAKeyInfo.P = bPInfo;
gRSAKeyInfo.Q = bQInfo;
}
}
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(gRSAKeyInfo);
//The hash to sign.
//Create an RSAOPKCS1SignatureFormatter object and pass it the
//RSACryptoServiceProvider to transfer the key information.
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
//Set the hash algorithm to SHA1.
RSAFormatter.SetHashAlgorithm("SHA1");
//Create a signature for HashValue and return it.
SignedHash = RSAFormatter.CreateSignature(Hash);
}
catch(CryptographicException es)
{
MessageBox.Show(es.Message);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
try
{
string path = @".\temp\PrivateKey.dat";
RSAParameters gRSAKeyInfo = new RSAParameters();
if (File.Exists(path))
{
// Open the stream and read it back.
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] bDInfo = new byte[128];
byte[] bDPInfo = new byte[64];
byte[] bDQInfo = new byte[64];
byte[] bExponentinfo = new byte[3];
byte[] bInverseQinfo = new byte[64];
byte[] bModulusinfo = new byte[128];
byte[] bPInfo = new byte[64];
byte[] bQInfo = new byte[64];
UnicodeEncoding temp = new UnicodeEncoding();
BinaryReader r = new BinaryReader(fs);
// Read data from PrivateKey.dat.
for (int i =0 ; i < 128; i++)
{
bDInfo[i] = r.ReadByte();
}
for (int i =0 ; i < 64; i++)
{
bDPInfo[i] = r.ReadByte();
}
for (int i =0 ; i < 64; i++)
{
bDQInfo[i] = r.ReadByte();
}
for (int i =0 ; i < 3; i++)
{
bExponentinfo[i] = r.ReadByte();
}
for (int i =0 ; i < 64; i++)
{
bInverseQinfo[i] = r.ReadByte();
}
for (int i =0 ; i < 128; i++)
{
bModulusinfo[i] = r.ReadByte();
}
for (int i =0 ; i < 64; i++)
{
bPInfo[i] = r.ReadByte();
}
for (int i =0 ; i < 64; i++)
{
bQInfo[i] = r.ReadByte();
}
r.Close();
gRSAKeyInfo.D = bDInfo;
gRSAKeyInfo.DP = bDPInfo;
gRSAKeyInfo.DQ = bDQInfo;
gRSAKeyInfo.Exponent = bExponentinfo;
gRSAKeyInfo.InverseQ = bInverseQinfo;
gRSAKeyInfo.Modulus = bModulusinfo;
gRSAKeyInfo.P = bPInfo;
gRSAKeyInfo.Q = bQInfo;
}
}
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(gRSAKeyInfo);
//Create an RSAPKCS1SignatureDeformatter object and pass it the
//RSACryptoServiceProvider to transfer the key information.
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
RSADeformatter.SetHashAlgorithm("SHA1");
//Verify the hash and display the results to the console.
if(RSADeformatter.VerifySignature(Hash, SignedHash))
{
MessageBox.Show("The signature was verified.");
}
else
{
MessageBox.Show("The signature was not verified.");
}
}
catch(CryptographicException es)
{
MessageBox.Show(es.Message);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -