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

📄 rsa加密解密及rsa签名和验证.txt

📁 RSA加密解密及RSA签名和验证算法
💻 TXT
📖 第 1 页 / 共 2 页
字号:
   }
  }
  #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 = "";
   // 
   // btnGetHas 

⌨️ 快捷键说明

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