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

 更新时间:2015年03月26日 09:36:57   作者:feige  
这篇文章主要介绍了C#自定义RSA加密解密及RSA签名和验证类,实例分析了C#实现RSA加密解密及RSA签名和验证的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

本文实例讲述了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#程序设计有所帮助。

相关文章

  • C# CultureInfo类案例详解

    C# CultureInfo类案例详解

    这篇文章主要介绍了C# CultureInfo类案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C# 中 “$” 符号的作用以及用法详解

    C# 中 “$” 符号的作用以及用法详解

    这篇文章主要介绍了C# 中 “$” 符号的作用以及用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • c# wpf使用GMap.NET类库,实现地图轨迹回放

    c# wpf使用GMap.NET类库,实现地图轨迹回放

    这篇文章主要介绍了c# wpf使用GMap.NET类库,实现地图轨迹回放的方法,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • c# winform 关闭窗体时同时结束线程实现思路

    c# winform 关闭窗体时同时结束线程实现思路

    th.IsBackground = true解决线程问题,意思就是把线程设置为后台线程,感兴趣的朋友可以多了解下,如何有什么妙招还请多多指导哈
    2013-02-02
  • C#利用GDI+绘制旋转文字等效果实例

    C#利用GDI+绘制旋转文字等效果实例

    这篇文章主要介绍了C#利用GDI+绘制旋转文字等效果实例,是非常实用的重要技巧,需要的朋友可以参考下
    2014-09-09
  • C#11新特性使用案例详解

    C#11新特性使用案例详解

    这篇文章主要为大家介绍了C#11新特性的使用案例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • C# 定时器定时更新的简单实例

    C# 定时器定时更新的简单实例

    这篇文章主要介绍了C#中定时器定时更新的简单实例。需要的朋友可以过来参考下,希望对大家有所帮助
    2013-12-12
  • 猜数字小游戏C#实现代码

    猜数字小游戏C#实现代码

    这篇文章主要为大家详细介绍了C#实现猜数字小游戏的代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • c#判断数据库服务器是否已经启动的方法

    c#判断数据库服务器是否已经启动的方法

    这篇文章主要介绍了使用c#判断数据库服务器是否已经启动的方法,大家参考使用吧
    2014-01-01
  • C#实现多文件压缩与解压功能

    C#实现多文件压缩与解压功能

    这篇文章主要为大家详细介绍了如何利用C#语言实现多文件压缩与解压功能,即选择多个文件压缩成ZIP文件和解压ZIP文件,需要的可以参考一下
    2022-08-08

最新评论

?


http://www.vxiaotou.com