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#程序设计有所帮助。

相关文章

  • C# Nullable的使用小结

    C# Nullable的使用小结

    本文主要介绍了C# Nullable的使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • C#裁剪,缩放,清晰度,水印处理操作示例

    C#裁剪,缩放,清晰度,水印处理操作示例

    这篇文章主要为大家详细介绍了C#裁剪,缩放,清晰度,水印处理操作示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • c#委托详解和和示例分享

    c#委托详解和和示例分享

    这篇文章详细探讨了C#中的委托,列举其主要的实现方式,并分析其在设计层面和编码层面带来的好处,最后会讨论其安全性和执行效率等,当然还有实现示例
    2014-03-03
  • C#难点逐个击破(6):C#数据类型与.net framework数据类型

    C#难点逐个击破(6):C#数据类型与.net framework数据类型

    最近开始看Illustrator C#2008,这真是一本好书,我读计算机书籍这么多了,能让我称为好书的没有多少。
    2010-02-02
  • C#与C++枚举的区别对比和使用案例

    C#与C++枚举的区别对比和使用案例

    本文详细讲解了C#与C++枚举的区别对比和使用案例,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#视频转换类分享

    C#视频转换类分享

    这篇文章主要为大家分享了C#视频转换类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C#中word导出功能的骚操作详解

    C#中word导出功能的骚操作详解

    这篇文章主要给大家介绍了关于C#中word导出功能骚操作的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Unity实现OCR文字识别功能

    Unity实现OCR文字识别功能

    这篇文章主要介绍了通过Unity接入百度AI接口,实现OCR文字识别功能,文中的实现步骤讲解详细,对我们学习或工作有一定的参考价值,需要的可以了解一下
    2022-01-01
  • C#实现XOR密码(异或密码)的示例代码

    C#实现XOR密码(异或密码)的示例代码

    XOR密码(异或密码)是一种简单的加密算法,它使用异或(XOR)操作来对明文和密钥进行加密和解密,本文为大家介绍了C#实现XOR密码的相关知识,希望对大家有所帮助
    2024-01-01
  • C#/VB.NET实现将XML转为PDF

    C#/VB.NET实现将XML转为PDF

    可扩展标记语言(XML)文件是一种标准的文本文件,它使用特定的标记来描述文档的结构以及其他特性。本文将利用C#实现XML文件转PDF ,需要的可以参考一下
    2022-03-03

最新评论