using System; using System.IO; using System.Security.Cryptography;public class StoreKey{ public static void Main() { try { // Create a key and save it in a container. GenKey_SaveInContainer("MyKeyContainer");
// Retrieve the key from the container. GetKeyFromContainer("MyKeyContainer");
// Delete the key from the container. DeleteKeyFromContainer("MyKeyContainer"); // Create a key and save it in a container. GenKey_SaveInContainer("MyKeyContainer"); // Delete the key from the container. DeleteKeyFromContainer("MyKeyContainer"); } catch(CryptographicException e) { Console.WriteLine(e.Message); } } public static void GenKey_SaveInContainer(string ContainerName) { // Create the CspParameters object and set the key container // name used to store the RSA key pair. CspParameters cp = new CspParameters(); cp.KeyContainerName = ContainerName; // Create a new instance of RSACryptoServiceProvider that accesses // the key container MyKeyContainerName. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp); // Display the key information to the console. Console.WriteLine("Key added to container: \n {0}", rsa.ToXmlString(true)); } public static void GetKeyFromContainer(string ContainerName) { // Create the CspParameters object and set the key container // name used to store the RSA key pair. CspParameters cp = new CspParameters(); cp.KeyContainerName = ContainerName; // Create a new instance of RSACryptoServiceProvider that accesses // the key container MyKeyContainerName. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp); // Display the key information to the console. Console.WriteLine("Key retrieved from container : \n {0}", rsa.ToXmlString(true)); } public static void DeleteKeyFromContainer(string ContainerName) { // Create the CspParameters object and set the key container // name used to store the RSA key pair. CspParameters cp = new CspParameters(); cp.KeyContainerName = ContainerName; // Create a new instance of RSACryptoServiceProvider that accesses // the key container. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp); // Delete the key entry in the container. rsa.PersistKeyInCsp = false; // Call Clear to release resources and delete the key from the container. rsa.Clear(); Console.WriteLine("Key deleted."); } } 运行上述示例时,控制台上将显示以下内容。Key added to container:<RSAKeyValue> ...Key Information A...</RSAKeyValue>Key retrieved from container :<RSAKeyValue> ...Key Information A...</RSAKeyValue>Key deleted.Key added to container:<RSAKeyValue> ...Key Information B...</RSAKeyValue>Key deleted.
晕,不要把MSDN的东西贴出来呀,以为我没看过啊? 我要的是实际的问题。MSDN上是有例子,但是还是搞不懂啊?以下是代码: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Security.Cryptography; using System.Text;namespace 网络安全三 { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); private RSAParameters RSApar = new RSAParameters();
还有,RSAParameters型变量的Modulus,Exponent属性到底放的是什么东东?
在RSACryptoServiceProvider类的介绍上就有一段例程。
using System.IO;
using System.Security.Cryptography;public class StoreKey{
public static void Main()
{
try
{
// Create a key and save it in a container.
GenKey_SaveInContainer("MyKeyContainer");
// Retrieve the key from the container.
GetKeyFromContainer("MyKeyContainer");
// Delete the key from the container.
DeleteKeyFromContainer("MyKeyContainer"); // Create a key and save it in a container.
GenKey_SaveInContainer("MyKeyContainer"); // Delete the key from the container.
DeleteKeyFromContainer("MyKeyContainer");
}
catch(CryptographicException e)
{
Console.WriteLine(e.Message);
} } public static void GenKey_SaveInContainer(string ContainerName)
{
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
CspParameters cp = new CspParameters();
cp.KeyContainerName = ContainerName; // Create a new instance of RSACryptoServiceProvider that accesses
// the key container MyKeyContainerName.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp); // Display the key information to the console.
Console.WriteLine("Key added to container: \n {0}", rsa.ToXmlString(true));
} public static void GetKeyFromContainer(string ContainerName)
{
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
CspParameters cp = new CspParameters();
cp.KeyContainerName = ContainerName; // Create a new instance of RSACryptoServiceProvider that accesses
// the key container MyKeyContainerName.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp); // Display the key information to the console.
Console.WriteLine("Key retrieved from container : \n {0}", rsa.ToXmlString(true));
} public static void DeleteKeyFromContainer(string ContainerName)
{
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
CspParameters cp = new CspParameters();
cp.KeyContainerName = ContainerName; // Create a new instance of RSACryptoServiceProvider that accesses
// the key container.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp); // Delete the key entry in the container.
rsa.PersistKeyInCsp = false; // Call Clear to release resources and delete the key from the container.
rsa.Clear(); Console.WriteLine("Key deleted.");
}
}
运行上述示例时,控制台上将显示以下内容。Key added to container:<RSAKeyValue> ...Key Information A...</RSAKeyValue>Key retrieved from container :<RSAKeyValue> ...Key Information A...</RSAKeyValue>Key deleted.Key added to container:<RSAKeyValue> ...Key Information B...</RSAKeyValue>Key deleted.
我要的是实际的问题。MSDN上是有例子,但是还是搞不懂啊?以下是代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.Text;namespace 网络安全三
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
private RSAParameters RSApar = new RSAParameters();
/// <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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(344, 176);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 32);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(64, 56);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(232, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(64, 104);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(424, 64);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(584, 246);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
this.textBox2.Text = System.Convert.ToBase64String(RSA.Encrypt(System.Text.Encoding.Default.GetBytes(this.textBox1.Text),true));
}
}
}
对相同的的明文,每次按了BUTTON1以后,在TEXTBOX2里的内容都不一样的!!!