⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 RAS加密机密的dotnet例子
💻 CS
📖 第 1 页 / 共 2 页
字号:
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;
using System.IO;

namespace WindowsApplication2
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox2;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		byte[] encryptData;
		
		//initialze the byte arrays to the public key information.
		byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
							   74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,
							   207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,
							   108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,
							   240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,
							   168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,
							   38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,
							   106,99,179,68,175,211,164,116,64,148,226,254,172,147};
		byte[] Exponent = {1,0,1};

		RSAParameters gRSAKeyInfo,gRSAKeyInfo1,gRSAKeyInfo2,gRSAKeyInfo3;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.Button button6;
		private System.Windows.Forms.Button button7;
		private System.Windows.Forms.Button button8;
		private System.Windows.Forms.Button button4;


		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();	
			RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
			gRSAKeyInfo2 = new RSAParameters();
			gRSAKeyInfo3 = new RSAParameters();
			gRSAKeyInfo2 = RSA.ExportParameters(false);
			gRSAKeyInfo3 = RSA.ExportParameters(true);
			gRSAKeyInfo = new RSAParameters();
			gRSAKeyInfo1 = new RSAParameters();
			//
			// 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.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.button5 = new System.Windows.Forms.Button();
			this.button6 = new System.Windows.Forms.Button();
			this.button7 = new System.Windows.Forms.Button();
			this.button8 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(91, 328);
			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(171, 328);
			this.button2.Name = "button2";
			this.button2.TabIndex = 1;
			this.button2.Text = "解密";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(8, 32);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(488, 120);
			this.textBox1.TabIndex = 2;
			this.textBox1.Text = "";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.TabIndex = 3;
			this.label1.Text = "要加密的内容:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 168);
			this.label2.Name = "label2";
			this.label2.TabIndex = 5;
			this.label2.Text = "解密出的内容:";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(8, 200);
			this.textBox2.Multiline = true;
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(488, 120);
			this.textBox2.TabIndex = 4;
			this.textBox2.Text = "";
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(259, 328);
			this.button3.Name = "button3";
			this.button3.TabIndex = 6;
			this.button3.Text = "加密1";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(339, 328);
			this.button4.Name = "button4";
			this.button4.TabIndex = 7;
			this.button4.Text = "解密1";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button5
			// 
			this.button5.Location = new System.Drawing.Point(64, 360);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(88, 23);
			this.button5.TabIndex = 8;
			this.button5.Text = "保存公开密钥";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// button6
			// 
			this.button6.Location = new System.Drawing.Point(160, 360);
			this.button6.Name = "button6";
			this.button6.Size = new System.Drawing.Size(88, 23);
			this.button6.TabIndex = 9;
			this.button6.Text = "保存私有密钥";
			this.button6.Click += new System.EventHandler(this.button6_Click);
			// 
			// button7
			// 
			this.button7.Location = new System.Drawing.Point(352, 360);
			this.button7.Name = "button7";
			this.button7.Size = new System.Drawing.Size(88, 23);
			this.button7.TabIndex = 11;
			this.button7.Text = "提取私有密钥";
			this.button7.Click += new System.EventHandler(this.button7_Click);
			// 
			// button8
			// 
			this.button8.Location = new System.Drawing.Point(256, 360);
			this.button8.Name = "button8";
			this.button8.Size = new System.Drawing.Size(88, 23);
			this.button8.TabIndex = 10;
			this.button8.Text = "提取公开密钥";
			this.button8.Click += new System.EventHandler(this.button8_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(504, 389);
			this.Controls.Add(this.button7);
			this.Controls.Add(this.button8);
			this.Controls.Add(this.button6);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			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());
		}

		static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
		{
			try
			{   
				//Create a new instance of RSACryptoServiceProvider.
				RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

				//Import the RSA Key information. This only needs
				//toinclude the public key information.
				RSA.ImportParameters(RSAKeyInfo);			

				//Encrypt the passed byte array and specify OAEP padding.  
				//OAEP padding is only available on Microsoft Windows XP or
				//later.  
				return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
			}
				//Catch and display a CryptographicException  
				//to the console.
			catch(CryptographicException e)
			{
				
				MessageBox.Show(e.Message);

				return null;
			}

		}

		static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)
		{
			try
			{
				//Create a new instance of RSACryptoServiceProvider.
				RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

				//Import the RSA Key information. This needs
				//to include the private key information.
				RSA.ImportParameters(RSAKeyInfo);				

				//Decrypt the passed byte array and specify OAEP padding.  
				//OAEP padding is only available on Microsoft Windows XP or
				//later.  
				return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
			}
				//Catch and display a CryptographicException  
				//to the console.
			catch(CryptographicException e)
			{
				Console.WriteLine(e.ToString());

				return null;
			}

		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			try
			{        
				//Create a UnicodeEncoder to convert between byte array and string.
				UnicodeEncoding ByteConverter = new UnicodeEncoding();

				//Create byte arrays to hold original, encrypted, and decrypted data.				
				
				byte[] dataToEncrypt = ByteConverter.GetBytes(textBox1.Text.Trim());
				byte[] encryptedData;
//				byte[] decryptedData;
        
				//Create a new instance of RSACryptoServiceProvider to generate
				//public and private key data.
				RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

				RSAParameters RSAKeyInfo = new RSAParameters();	
//				RSAParameters RSAKeyInfo = RSA.ExportParameters(false);

//				PublicKey = RSAKeyInfo.Modulus;
//				Exponent = RSAKeyInfo.Exponent;				

				//Set RSAKeyInfo to the public key values. 
//				RSAKeyInfo.Modulus = PublicKey;
//				RSAKeyInfo.Exponent = Exponent;

//				RSA.ImportParameters(RSAKeyInfo);

//				RijndaelManaged RM = new RijndaelManaged();

				//Encrypt the symmetric key and IV.
//				EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false);
//				EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false);

				//Pass the data to ENCRYPT, the public key information 
				//(using RSACryptoServiceProvider.ExportParameters(false),
				//and a boolean flag specifying no OAEP padding.
				gRSAKeyInfo1 = RSA.ExportParameters(false);

				encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);				

				gRSAKeyInfo = RSA.ExportParameters(true);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -