C#自定义RSA加密解密及RSA签名和验证类实例

 更新时间:2015年03月26日 09:36:57   作者:feige  
这篇文章主要介绍了C#自定义RSA加密解密及RSA签名和验证类,实例分析了C#实现RSA加密解密及RSA签名和验证的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#自定义RSA加密解密及RSA签名和验证类。分享给大家供大家参考。具体分析如下:

这个C#类自定义RSA加密解密及RSA签名和验证,包含了RSA加密、解密及签名所需的相关函数,带有详细的注释说明。

using System; 
using System.Text; 
using System.Security.Cryptography;
namespace DotNet.Utilities
{ 
 /// <summary> 
 /// RSA加密解密及RSA签名和验证
 /// </summary> 
 public class RSACryption 
 {   
  public RSACryption() 
  {    
  } 
  
  #region RSA 加密解密 
  #region RSA 的密钥产生 
 
  /// <summary>
  /// RSA 的密钥产生 产生私钥 和公钥 
  /// </summary>
  /// <param name="xmlKeys"></param>
  /// <param name="xmlPublicKey"></param>
  public void RSAKey(out string xmlKeys,out string xmlPublicKey) 
  {    
    System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); 
    xmlKeys=rsa.ToXmlString(true); 
    xmlPublicKey = rsa.ToXmlString(false);    
  } 
  #endregion 
  #region RSA的加密函数 
  //############################################################################## 
  //RSA 方式加密 
  //说明KEY必须是XML的行式,返回的是字符串 
  //在有一点需要说明!!该加密方式有 长度 限制的!! 
  //############################################################################## 
  //RSA的加密函数 string
  public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString ) 
  { 
   
   byte[] PlainTextBArray; 
   byte[] CypherTextBArray; 
   string Result; 
   RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); 
   rsa.FromXmlString(xmlPublicKey); 
   PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString); 
   CypherTextBArray = rsa.Encrypt(PlainTextBArray, false); 
   Result=Convert.ToBase64String(CypherTextBArray); 
   return Result; 
   
  } 
  //RSA的加密函数 byte[]
  public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString ) 
  { 
   
   byte[] CypherTextBArray; 
   string Result; 
   RSACryptoServiceProvider rsa=new RSACryptoServiceProvider(); 
   rsa.FromXmlString(xmlPublicKey); 
   CypherTextBArray = rsa.Encrypt(EncryptString, false); 
   Result=Convert.ToBase64String(CypherTextBArray); 
   return Result; 
   
  } 
  #endregion 
  #region RSA的解密函数 
  //RSA的解密函数 string
  public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString ) 
  {   
   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; 
   
  } 
  //RSA的解密函数 byte
  public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString ) 
  {   
   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; 
   
  } 
  #endregion 
  #endregion 
  #region RSA数字签名 
  #region 获取Hash描述表 
  //获取Hash描述表 ,sharejs.com
  public bool GetHash(string m_strSource, ref byte[] HashData) 
  {    
   //从字符串中取得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;    
  } 
  //获取Hash描述表 
  public bool GetHash(string m_strSource, ref string strHashData) 
  { 
   
   //从字符串中取得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; 
   
  } 
  //获取Hash描述表 
  public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData) 
  { 
   
   //从文件中取得Hash描述 
   System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5"); 
   HashData = MD5.ComputeHash(objFile); 
   objFile.Close(); 
   return true; 
   
  } 
  //获取Hash描述表 
  public bool GetHash(System.IO.FileStream objFile, ref string strHashData) 
  { 
   
   //从文件中取得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; 
   
  } 
  #endregion 
  #region RSA签名 
  //RSA签名 
  public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref 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); 
    return true; 
   
  } 
  //RSA签名 
  public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData) 
  { 
   
    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; 
   
  } 
  //RSA签名 
  public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData) 
  { 
   
    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; 
   
  } 
  //RSA签名 
  public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData) 
  { 
   
    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; 
   
  } 
  #endregion 
  #region RSA 签名验证 
  public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, 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"); 
    if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
   
  } 
  public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, 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"); 
    if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData)) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
   
  } 
  public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData) 
  { 
   
    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; 
    } 
   
  } 
  public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData) 
  { 
   
    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; 
    } 
   
  } 

  #endregion 

  #endregion 
 } 
}

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • 基于WPF实现控件轮廓跑马灯动画效果

    基于WPF实现控件轮廓跑马灯动画效果

    这篇文章主要介绍了如何利用WPF实现控件轮廓跑马灯动画效果,文中的示例代码讲解详细,对我们学习或工作有一定帮助,需要的可以参考一下
    2022-08-08
  • C# 在项目中引用x86 x64的非托管代码的方法

    C# 在项目中引用x86 x64的非托管代码的方法

    使用宏最简单的方法是编译两个版本,编译多个版本可以点击配置管理器,然后创建x86和x64,然后版本添加宏,这样就可以判断宏来使用不同的dll。这篇文章主要介绍了C# 在项目中引用x86 x64的非托管代码的方法,需要的朋友可以参考下
    2018-03-03
  • C#在子线程中更新窗口部件的写法

    C#在子线程中更新窗口部件的写法

    这篇文章主要介绍了C#在子线程中更新窗口部件的写法,本文直接给出代码示例,需要的朋友可以参考下
    2015-06-06
  • C#图表算法之有向图

    C#图表算法之有向图

    这篇文章介绍了C#图表算法之有向图,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • Unity3D实现飞机大战游戏(2)

    Unity3D实现飞机大战游戏(2)

    这篇文章主要为大家详细介绍了Unity3D实现飞机大战游戏的第二部分,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • C#中事件的定义和使用

    C#中事件的定义和使用

    在使用事件时,通常要定义两个方法,一个是和事件定义的委托签名一致的方法。下面让我们看看使用事件的具体步骤。
    2016-06-06
  • 详细分析c# 客户端内存优化

    详细分析c# 客户端内存优化

    这篇文章主要介绍了c# 客户端内存优化的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • c#正反序列化XML文件示例(xml序列化)

    c#正反序列化XML文件示例(xml序列化)

    这篇文章主要介绍了c#正反序列化XML文件示例,可以将对象序列化为XML文本或者文件,或者将XML文件或文本反序列化为对象,要求进行序列化的对象不能出现序列化异常,支持类的嵌套,要求类的属性为public
    2014-03-03
  • C# wpf解决Popup弹出位置异常问题解决

    C# wpf解决Popup弹出位置异常问题解决

    本文主要介绍了C# wpf解决Popup弹出位置异常问题解决,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • c# WPF中System.Windows.Interactivity的使用

    c# WPF中System.Windows.Interactivity的使用

    这篇文章主要介绍了c# WPF中System.Windows.Interactivity的使用,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03

最新评论