RSA加密解密及RSA簽名和驗證 - 中國WEB開發者網絡 (http://www.webasp.net) -- 技術教程 (http://www.webasp.net/article/) --- RSA加密解密及RSA簽名和驗證 (http://www.webasp.net/article/15/14831.htm) |
| -- 作者:未知 -- 發佈日期: 2004-11-15 |
| 此Demo包含兩個文件,建立一個解決方案,然後建立兩個文件,一個為Form,一個為Class,把代碼分別複製進去即可
RSA正確的執行過程: 加密解密: 1、獲取密鑰,這裡是產生密鑰,實際應用中可以從各種存儲介質上讀取密鑰 2、加密 3、解密 簽名和驗證: 簽名: 1、獲取密鑰,這裡是產生密鑰,實際應用中可以從各種存儲介質上讀取密鑰 2、獲取待簽名的Hash碼 3、簽名 其中,1和2的步驟無所謂,在本例中,我們將對txtSource裡的內容進行簽名,也可以對文件進行簽名 驗證簽名: 1、獲取密鑰,這裡是產生密鑰,實際應用中可以從各種存儲介質上讀取密鑰 2、獲取待驗證簽名的Hash碼 3、獲取簽名的字串,這裡簽名的字串存儲在m_strEncryptedSignatureData變量中,在DEMO中必須通過簽名才能獲得這個字串,因此需要先執行簽名,當然也可以更改之後通過別的方式獲得 4、驗證 其中,1和2的步驟無所謂,在本例中,我們將對txtSource裡的內容進行簽名驗證,也可以對文件進行簽名驗證 如果是文件,取得文件之後把文件的內容以byte[]的方式代入即可 /////////////////////////////////////////////////////////////////////////////////////////////////////////// //RSACryption.cs /////////////////////////////////////////////////////////////////////////////////////////////////////////// using System; using System.Text; using System.Security.Cryptography; namespace RSAApplication { /// <summary> /// RSACryption 的摘要說明。 /// </summary> public class RSACryption { #region 構造函數 public RSACryption() { // // TODO: 在此處添加構造函數邏輯 // } #endregion #region RSA 加密解密 #region RSA 的密鑰產生 //RSA 的密鑰產生 //產生私鑰 和公鑰 public void RSAKey(out string xmlKeys,out string xmlPublicKey) { try { System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); xmlKeys=rsa.ToXmlString(true); xmlPublicKey = rsa.ToXmlString(false); } catch(Exception ex) { throw ex; } } #endregion #region RSA的加密函數 //############################################################################## //RSA 方式加密 //說明KEY必須是XML的行式,返回的是字符串 //在有一點需要說明!!該加密方式有 長度 限制的!! //############################################################################## //RSA的加密函數 public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString ) { try { byte[] PlainTextBArray; byte[] CypherTextBArray; string Result; System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPublicKey); PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString); CypherTextBArray = rsa.Encrypt(PlainTextBArray, false); Result=Convert.ToBase64String(CypherTextBArray); return Result; } catch(Exception ex) { throw ex; } } //RSA的加密函數 public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString ) { try { byte[] CypherTextBArray; string Result; System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPublicKey); CypherTextBArray = rsa.Encrypt(EncryptString, false); Result=Convert.ToBase64String(CypherTextBArray); return Result; } catch(Exception ex) { throw ex; } } #endregion #region RSA的解密函數 //RSA的解密函數 public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString ) { try { byte[] PlainTextBArray; byte[] DypherTextBArray; string Result; System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPrivateKey); PlainTextBArray =Convert.FromBase64String(m_strDecryptString); DypherTextBArray=rsa.Decrypt(PlainTextBArray, false); Result=(new UnicodeEncoding()).GetString(DypherTextBArray); return Result; } catch(Exception ex) { throw ex; } } //RSA的解密函數 public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString ) { try { byte[] DypherTextBArray; string Result; System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); rsa.FromXmlString(xmlPrivateKey); DypherTextBArray=rsa.Decrypt(DecryptString, false); Result=(new UnicodeEncoding()).GetString(DypherTextBArray); return Result; } catch(Exception ex) { throw ex; } } #endregion #endregion #region RSA數字簽名 #region 獲取Hash描述表 //獲取Hash描述表 public bool GetHash(string m_strSource, ref byte[] HashData) { try { //從字符串中取得Hash描述 byte[] Buffer; System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource); HashData = MD5.ComputeHash(Buffer); return true; } catch(Exception ex) { throw ex; } } //獲取Hash描述表 public bool GetHash(string m_strSource, ref string strHashData) { try { //從字符串中取得Hash描述 byte[] Buffer; byte[] HashData; System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource); HashData = MD5.ComputeHash(Buffer); strHashData = Convert.ToBase64String(HashData); return true; } catch(Exception ex) { throw ex; } } //獲取Hash描述表 public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData) { try { //從文件中取得Hash描述 System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); HashData = MD5.ComputeHash(objFile); objFile.Close(); return true; } catch(Exception ex) { throw ex; } } //獲取Hash描述表 public bool GetHash(System.IO.FileStream objFile, ref string strHashData) { try { //從文件中取得Hash描述 byte[] HashData; System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); HashData = MD5.ComputeHash(objFile); objFile.Close(); strHashData = Convert.ToBase64String(HashData); return true; } catch(Exception ex) { throw ex; } } #endregion #region RSA簽名 //RSA簽名 public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData) { try { System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //設置簽名的算法為MD5 RSAFormatter.SetHashAlgorithm("MD5"); //執行簽名 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); return true; } catch(Exception ex) { throw ex; } } //RSA簽名 public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData) { try { byte[] EncryptedSignatureData; System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //設置簽名的算法為MD5 RSAFormatter.SetHashAlgorithm("MD5"); //執行簽名 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData); return true; } catch(Exception ex) { throw ex; } } //RSA簽名 public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData) { try { byte[] HashbyteSignature; HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature); System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //設置簽名的算法為MD5 RSAFormatter.SetHashAlgorithm("MD5"); //執行簽名 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); return true; } catch(Exception ex) { throw ex; } } //RSA簽名 public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData) { try { byte[] HashbyteSignature; byte[] EncryptedSignatureData; HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature); System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPrivate); System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA); //設置簽名的算法為MD5 RSAFormatter.SetHashAlgorithm("MD5"); //執行簽名 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature); m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData); return true; } catch(Exception ex) { throw ex; } } #endregion #region RSA 簽名驗證 public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData) { try { System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的時候HASH算法為MD5 RSADeformatter.SetHashAlgorithm("MD5"); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } catch(Exception ex) { throw ex; } } public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData) { try { byte[] HashbyteDeformatter; HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter); System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的時候HASH算法為MD5 RSADeformatter.SetHashAlgorithm("MD5"); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } catch(Exception ex) { throw ex; } } public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData) { try { byte[] DeformatterData; System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的時候HASH算法為MD5 RSADeformatter.SetHashAlgorithm("MD5"); DeformatterData =Convert.FromBase64String(p_strDeformatterData); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } catch(Exception ex) { throw ex; } } public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData) { try { byte[] DeformatterData; byte[] HashbyteDeformatter; HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter); System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider(); RSA.FromXmlString(p_strKeyPublic); System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA); //指定解密的時候HASH算法為MD5 RSADeformatter.SetHashAlgorithm("MD5"); DeformatterData =Convert.FromBase64String(p_strDeformatterData); if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) { return true; } else { return false; } } catch(Exception ex) { throw ex; } } #endregion #endregion } } /////////////////////////////////////////////////////////////////////////////////////////////////////////// //frmRSACryptionTest.cs /////////////////////////////////////////////////////////////////////////////////////////////////////////// using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace RSAApplication { /// <summary> /// frmRSACryptionTest 的摘要說明。 /// </summary> public class frmRSACryptionTest : System.Windows.Forms.Form { #region 必需的設計器變量 /// <summary> /// 必需的設計器變量 /// </summary> private System.Windows.Forms.Button btnBuildKey; private System.Windows.Forms.TextBox txtKeyPublic; private System.Windows.Forms.TextBox txtKeyPrivate; private System.ComponentModel.Container components = null; private System.Windows.Forms.Button btnRSAEncrypt; private System.Windows.Forms.TextBox txtRSADecrypt; private System.Windows.Forms.Button btnRSADecrypt; private System.Windows.Forms.TextBox txtSource; private System.Windows.Forms.TextBox txtRSAEncrypt; private System.Windows.Forms.Button btnSignature; private System.Windows.Forms.Button btnDeformatter; private System.Windows.Forms.Button btnGetHashSignature; private System.Windows.Forms.Button btnGetHashDeformatter; private System.Windows.Forms.TextBox txtSignature; private System.Windows.Forms.TextBox txtGetHashSignature; private System.Windows.Forms.TextBox txtGetHashDeformatter; private string m_strKeyPrivate = ""; private string m_strKeyPublic = ""; private string m_strHashbyteSignature = ""; private string m_strHashbyteDeformatter = ""; private string m_strEncryptedSignatureData = ""; #endregion #region 構造函數 public frmRSACryptionTest() { // // Windows 窗體設計器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼 // } /// <summary> /// 清理所有正在使用的資源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #endregion #region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// </summary> private void InitializeComponent() { this.btnBuildKey = new System.Windows.Forms.Button(); this.txtKeyPublic = new System.Windows.Forms.TextBox(); this.txtKeyPrivate = new System.Windows.Forms.TextBox(); this.btnRSAEncrypt = new System.Windows.Forms.Button(); this.txtSource = new System.Windows.Forms.TextBox(); this.txtRSAEncrypt = new System.Windows.Forms.TextBox(); this.txtRSADecrypt = new System.Windows.Forms.TextBox(); this.btnRSADecrypt = new System.Windows.Forms.Button(); this.btnDeformatter = new System.Windows.Forms.Button(); this.btnSignature = new System.Windows.Forms.Button(); this.txtSignature = new System.Windows.Forms.TextBox(); this.btnGetHashSignature = new System.Windows.Forms.Button(); this.btnGetHashDeformatter = new System.Windows.Forms.Button(); this.txtGetHashSignature = new System.Windows.Forms.TextBox(); this.txtGetHashDeformatter = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // btnBuildKey // this.btnBuildKey.Location = new System.Drawing.Point(11, 17); this.btnBuildKey.Name = "btnBuildKey"; this.btnBuildKey.Size = new System.Drawing.Size(77, 34); this.btnBuildKey.TabIndex = 0; this.btnBuildKey.Text = "產生密鑰"; this.btnBuildKey.Click += new System.EventHandler(this.btnBuildKey_Click); // // txtKeyPublic // this.txtKeyPublic.Location = new System.Drawing.Point(137, 11); this.txtKeyPublic.Multiline = true; this.txtKeyPublic.Name = "txtKeyPublic"; this.txtKeyPublic.Size = new System.Drawing.Size(602, 44); this.txtKeyPublic.TabIndex = 1; this.txtKeyPublic.Text = ""; // // txtKeyPrivate // this.txtKeyPrivate.Location = new System.Drawing.Point(137, 58); this.txtKeyPrivate.Multiline = true; this.txtKeyPrivate.Name = "txtKeyPrivate"; this.txtKeyPrivate.Size = new System.Drawing.Size(602, 44); this.txtKeyPrivate.TabIndex = 2; this.txtKeyPrivate.Text = ""; // // btnRSAEncrypt // this.btnRSAEncrypt.Location = new System.Drawing.Point(11, 157); this.btnRSAEncrypt.Name = "btnRSAEncrypt"; this.btnRSAEncrypt.Size = new System.Drawing.Size(77, 34); this.btnRSAEncrypt.TabIndex = 3; this.btnRSAEncrypt.Text = "RSA加密"; this.btnRSAEncrypt.Click += new System.EventHandler(this.btnRSAEncrypt_Click); // // txtSource // this.txtSource.Location = new System.Drawing.Point(137, 108); this.txtSource.Multiline = true; this.txtSource.Name = "txtSource"; this.txtSource.Size = new System.Drawing.Size(602, 44); this.txtSource.TabIndex = 4; this.txtSource.Text = "字串不能太長j——km,.ewm.m, .vkj中國福建"; // // txtRSAEncrypt // this.txtRSAEncrypt.Location = new System.Drawing.Point(137, 155); this.txtRSAEncrypt.Multiline = true; this.txtRSAEncrypt.Name = "txtRSAEncrypt"; this.txtRSAEncrypt.Size = new System.Drawing.Size(602, 44); this.txtRSAEncrypt.TabIndex = 5; this.txtRSAEncrypt.Text = ""; // // txtRSADecrypt // this.txtRSADecrypt.Location = new System.Drawing.Point(137, 203); this.txtRSADecrypt.Multiline = true; this.txtRSADecrypt.Name = "txtRSADecrypt"; this.txtRSADecrypt.Size = new System.Drawing.Size(602, 44); this.txtRSADecrypt.TabIndex = 6; this.txtRSADecrypt.Text = ""; // // btnRSADecrypt // this.btnRSADecrypt.Location = new System.Drawing.Point(11, 202); this.btnRSADecrypt.Name = "btnRSADecrypt"; this.btnRSADecrypt.Size = new System.Drawing.Size(77, 34); this.btnRSADecrypt.TabIndex = 7; this.btnRSADecrypt.Text = "RSA解密"; this.btnRSADecrypt.Click += new System.EventHandler(this.btnRSADecrypt_Click); // // btnDeformatter // this.btnDeformatter.Location = new System.Drawing.Point(11, 396); this.btnDeformatter.Name = "btnDeformatter"; this.btnDeformatter.Size = new System.Drawing.Size(77, 34); this.btnDeformatter.TabIndex = 10; this.btnDeformatter.Text = "RSA驗證"; this.btnDeformatter.Click += new System.EventHandler(this.btnDeformatter_Click); // // btnSignature // this.btnSignature.Location = new System.Drawing.Point(11, 297); this.btnSignature.Name = "btnSignature"; this.btnSignature.Size = new System.Drawing.Size(77, 34); this.btnSignature.TabIndex = 9; this.btnSignature.Text = "RSA簽名"; this.btnSignature.Click += new System.EventHandler(this.btnSignature_Click); // // txtSignature // this.txtSignature.Location = new System.Drawing.Point(137, 298); this.txtSignature.Multiline = true; this.txtSignature.Name = "txtSignature"; this.txtSignature.Size = new System.Drawing.Size(602, 44); this.txtSignature.TabIndex = 11; this.txtSignature.Text = ""; // // btnGetHashSignature // this.btnGetHashSignature.Location = new System.Drawing.Point(11, 252); this.btnGetHashSignature.Name = "btnGetHashSignature"; this.btnGetHashSignature.Size = new System.Drawing.Size(117, 36); this.btnGetHashSignature.TabIndex = 13; this.btnGetHashSignature.Text = "獲取哈稀碼(簽名)"; this.btnGetHashSignature.Click += new System.EventHandler(this.btnGetHashSignature_Click); // // btnGetHashDeformatter // this.btnGetHashDeformatter.Location = new System.Drawing.Point(11, 348); this.btnGetHashDeformatter.Name = "btnGetHashDeformatter"; this.btnGetHashDeformatter.Size = new System.Drawing.Size(117, 36); this.btnGetHashDeformatter.TabIndex = 14; this.btnGetHashDeformatter.Text = "獲取哈稀碼(驗證)"; this.btnGetHashDeformatter.Click += new System.EventHandler(this.btnGetHashDeformatter_Click); // // txtGetHashSignature // this.txtGetHashSignature.Location = new System.Drawing.Point(137, 251); this.txtGetHashSignature.Multiline = true; this.txtGetHashSignature.Name = "txtGetHashSignature"; this.txtGetHashSignature.Size = new System.Drawing.Size(602, 44); this.txtGetHashSignature.TabIndex = 15; this.txtGetHashSignature.Text = ""; // // txtGetHashDeformatter // this.txtGetHashDeformatter.Location = new System.Drawing.Point(137, 346); this.txtGetHashDeformatter.Multiline = true; this.txtGetHashDeformatter.Name = "txtGetHashDeformatter"; this.txtGetHashDeformatter.Size = new System.Drawing.Size(602, 44); this.txtGetHashDeformatter.TabIndex = 16; this.txtGetHashDeformatter.Text = ""; // // frmRSACryptionTest // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(764, 444); this.Controls.Add(this.txtGetHashDeformatter); this.Controls.Add(this.txtGetHashSignature); this.Controls.Add(this.txtSignature); this.Controls.Add(this.txtRSADecrypt); this.Controls.Add(this.txtRSAEncrypt); this.Controls.Add(this.txtSource); this.Controls.Add(this.txtKeyPrivate); this.Controls.Add(this.txtKeyPublic); this.Controls.Add(this.btnGetHashDeformatter); this.Controls.Add(this.btnGetHashSignature); this.Controls.Add(this.btnDeformatter); this.Controls.Add(this.btnSignature); this.Controls.Add(this.btnRSADecrypt); this.Controls.Add(this.btnRSAEncrypt); this.Controls.Add(this.btnBuildKey); this.Name = "frmRSACryptionTest"; this.Text = "RSA加密解密"; this.ResumeLayout(false); } #endregion #region 應用程序的主入口點 /// <summary> /// 應用程序的主入口點 /// </summary> [STAThread] static void Main() { Application.Run(new frmRSACryptionTest()); } #endregion #region 產生密鑰 private void btnBuildKey_Click(object sender, System.EventArgs e) { try { RSACryption RC = new RSACryption(); RC.RSAKey(out m_strKeyPrivate, out m_strKeyPublic); this.txtKeyPrivate.Text = m_strKeyPrivate; this.txtKeyPublic.Text = m_strKeyPublic; } catch(Exception ex) { MessageBox.Show(this,ex.Message,"錯誤",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error); } } #endregion #region 加密解密 private void btnRSAEncrypt_Click(object sender, System.EventArgs e) { try { RSACryption RC = new RSACryption(); this.txtRSAEncrypt.Text = RC.RSAEncrypt(m_strKeyPublic, this.txtSource.Text); } catch(Exception ex) { MessageBox.Show(this,ex.Message,"錯誤",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error); } } private void btnRSADecrypt_Click(object sender, System.EventArgs e) { try { RSACryption RC = new RSACryption(); this.txtRSADecrypt.Text = RC.RSADecrypt(m_strKeyPrivate, this.txtRSAEncrypt.Text); } catch(Exception ex) { MessageBox.Show(this,ex.Message,"錯誤",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error); } } #endregion #region 簽名、驗證 #region 獲取Hash碼---針對簽名 private void btnGetHashSignature_Click(object sender, System.EventArgs e) { try { RSACryption RC = new RSACryption(); if( RC.GetHash(this.txtSource.Text,ref m_strHashbyteSignature) == false) { MessageBox.Show(this,"取Hash碼錯誤!","提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning); } this.txtGetHashSignature.Text = m_strHashbyteSignature; } catch(Exception ex) { MessageBox.Show(this,ex.Message,"錯誤",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error); } } #endregion #region 簽名 private void btnSignature_Click(object sender, System.EventArgs e) { try { RSACryption RC = new RSACryption(); if( RC.SignatureFormatter(m_strKeyPrivate,m_strHashbyteSignature, ref m_strEncryptedSignatureData) == false) { MessageBox.Show(this,"RSA數字簽名錯誤!","提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning); } this.txtSignature.Text = m_strEncryptedSignatureData; } catch(Exception ex) { MessageBox.Show(this,ex.Message,"錯誤",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error); } } #endregion #region 獲取Hash碼---針對驗證 private void btnGetHashDeformatter_Click(object sender, System.EventArgs e) { try { RSACryption RC = new RSACryption(); if( RC.GetHash(this.txtSource.Text,ref m_strHashbyteDeformatter) == false) { MessageBox.Show(this,"取Hash碼錯誤!","提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning); } this.txtGetHashDeformatter.Text = m_strHashbyteDeformatter; } catch(Exception ex) { MessageBox.Show(this,ex.Message,"錯誤",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error); } } #endregion #region 驗證 private void btnDeformatter_Click(object sender, System.EventArgs e) { try { RSACryption RC = new RSACryption(); if( RC.SignatureDeformatter(m_strKeyPublic,m_strHashbyteDeformatter, m_strEncryptedSignatureData) == false) { MessageBox.Show(this,"身份驗證失敗!","提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning); } else { MessageBox.Show(this,"身份驗證通過!","提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning); } } catch(Exception ex) { MessageBox.Show(this,ex.Message,"錯誤",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error); } } #endregion #endregion } } |
| webasp.net |