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

📄 form1.cs

📁 RAS加密机密的dotnet例子
💻 CS
📖 第 1 页 / 共 2 页
字号:
				//decryptedData = RSAEncrypt(encryptedData, RSAKeyInfo, false);

				encryptData = encryptedData;
				textBox1.Text = "";
				//textBox1.Text = ByteConverter.GetString(encryptedData,0,encryptedData.Length).Trim();
				textBox2.Text = ByteConverter.GetString(dataToEncrypt,0,dataToEncrypt.Length).Trim();
			}				
			catch(CryptographicException ce)
			{
				MessageBox.Show(ce.Message);
			}
		}

		private void button2_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[] dataToDecrypt = encryptData;
				byte[] decryptedData;
        
				//Create a new instance of RSACryptoServiceProvider to generate
				//public and private key data.
				RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//				RSAParameters RSAKeyInfo = new RSAParameters();
//
//				//RSAKeyInfo = RSA.ExportParameters(true);
//				//Set RSAKeyInfo to the public key values. 
//				RSAKeyInfo.Modulus = PublicKey;
//				RSAKeyInfo.Exponent = Exponent;

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

				decryptedData = RSADecrypt(dataToDecrypt, gRSAKeyInfo, false);				
				textBox1.Text = ByteConverter.GetString(decryptedData,0,decryptedData.Length);
				textBox2.Text = "";
			}
			catch(CryptographicException ce)
			{
				MessageBox.Show(ce.Message);
			}
		}

		private void button3_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.

				encryptedData = RSAEncrypt(dataToEncrypt, gRSAKeyInfo1, false);	

				RSAParameters RASP = gRSAKeyInfo;

				encryptData = encryptedData;
				textBox1.Text = "";
				//textBox1.Text = ByteConverter.GetString(encryptedData,0,encryptedData.Length).Trim();
				textBox2.Text = ByteConverter.GetString(dataToEncrypt,0,dataToEncrypt.Length).Trim();
			}				
			catch(CryptographicException ce)
			{
				MessageBox.Show(ce.Message);
			}
		}

		private void button4_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[] dataToDecrypt = encryptData;
				byte[] decryptedData;
        
				decryptedData = RSADecrypt(dataToDecrypt, gRSAKeyInfo, false);	

				textBox1.Text = ByteConverter.GetString(decryptedData,0,decryptedData.Length);
				textBox2.Text = "";
			}
			catch(CryptographicException ce)
			{
				MessageBox.Show(ce.Message);
			}
		}

		private void button5_Click(object sender, System.EventArgs e)
		{
			string path = @".\temp\PublicKey.dat";
			byte[] Exponentinfo = gRSAKeyInfo1.Exponent;
			byte[] Modulusinfo = gRSAKeyInfo1.Modulus;

			// Delete the file if it exists.
			if (!File.Exists(path)) 
			{
				// Create the file.
				using (FileStream fs = new FileStream(path,FileMode.CreateNew)) 
				{
					BinaryWriter w = new BinaryWriter(fs);
					// Write data to PublicKey.dat.
					w.Write(Exponentinfo);
					w.Write(Modulusinfo);
					w.Close();
				}
			}

			// Open the stream and read it back.
			using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) 
			{
				byte[] bExponentinfo = new byte[3];
				byte[] bModulusinfo = new byte[128];
				UnicodeEncoding temp = new UnicodeEncoding();

				BinaryReader r = new BinaryReader(fs);
				// Read data from PublicKey.dat.
				for (int i =0 ; i < 3; i++) 
				{
					bExponentinfo[i] = r.ReadByte();
				}

				for (int i =0 ; i < 128; i++) 
				{
					bModulusinfo[i] = r.ReadByte();
				}				
				r.Close();
			}
		}

		private void button6_Click(object sender, System.EventArgs e)
		{
			string path = @".\temp\PrivateKey.dat";
			byte[] DInfo = gRSAKeyInfo.D;
			byte[] DPInfo = gRSAKeyInfo.DP;
			byte[] DQInfo = gRSAKeyInfo.DQ;
			byte[] Exponentinfo = gRSAKeyInfo.Exponent;
			byte[] InverseQinfo = gRSAKeyInfo.InverseQ;
			byte[] Modulusinfo = gRSAKeyInfo.Modulus;
			byte[] PInfo = gRSAKeyInfo.P;
			byte[] QInfo = gRSAKeyInfo.Q;

			// Delete the file if it exists.
			if (!File.Exists(path)) 
			{
				// Create the file.
				using (FileStream fs = new FileStream(path,FileMode.CreateNew)) 
				{
					BinaryWriter w = new BinaryWriter(fs);
					// Write data to PrivateKey.dat.
					w.Write(DInfo);
					w.Write(DPInfo);
					w.Write(DQInfo);
					w.Write(Exponentinfo);
					w.Write(InverseQinfo);
					w.Write(Modulusinfo);
					w.Write(PInfo);
					w.Write(QInfo);
					w.Close();
					// Add some information to the file.
					//fs.Write(info, 0, info.Length);
				}
			}

			// 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();
			}
		}

		private void button8_Click(object sender, System.EventArgs e)
		{
			string path = @".\temp\PublicKey.dat";

			if (File.Exists(path)) 
			{
				using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) 
				{
					byte[] bExponentinfo = new byte[3];
					byte[] bModulusinfo = new byte[128];
					UnicodeEncoding temp = new UnicodeEncoding();

					BinaryReader r = new BinaryReader(fs);
					// Read data from PublicKey.dat.
					for (int i =0 ; i < 3; i++) 
					{
						bExponentinfo[i] = r.ReadByte();
					}

					for (int i =0 ; i < 128; i++) 
					{
						bModulusinfo[i] = r.ReadByte();
					}				
					r.Close();
					gRSAKeyInfo1.Exponent = bExponentinfo;
					gRSAKeyInfo1.Modulus = bModulusinfo;
				}				
			}
			
		}

		private void button7_Click(object sender, System.EventArgs e)
		{
			string path = @".\temp\PrivateKey.dat";

			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;					
				}
			}			
		}
		
	}
}

⌨️ 快捷键说明

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