Cc customized RSA encryption and decryption and RSA signature and verification encapsulation classes

From , 4 Years ago, written in C#, viewed 54 times.
URL https://pastebin.vip/view/20cf775f
  1. using System;
  2. using System.Text;
  3. using System.Security.Cryptography;
  4. namespace DotNet.Utilities
  5. {
  6.         /// <summary>
  7.         /// RSA加密解密及RSA签名和验证
  8.         /// </summary>
  9.         public class RSACryption
  10.         {              
  11.                 public RSACryption()
  12.                 {                      
  13.                 }
  14.                
  15.  
  16.                 #region RSA 加密解密
  17.  
  18.                 #region RSA 的密钥产生
  19.        
  20.                 /// <summary>
  21.                 /// RSA 的密钥产生 产生私钥 和公钥
  22.                 /// </summary>
  23.                 /// <param name="xmlKeys"></param>
  24.                 /// <param name="xmlPublicKey"></param>
  25.                 public void RSAKey(out string xmlKeys,out string xmlPublicKey)
  26.                 {                      
  27.                                 System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  28.                                 xmlKeys=rsa.ToXmlString(true);
  29.                                 xmlPublicKey = rsa.ToXmlString(false);                 
  30.                 }
  31.                 #endregion
  32.  
  33.                 #region RSA的加密函数
  34.                 //##############################################################################
  35.                 //RSA 方式加密
  36.                 //说明KEY必须是XML的行式,返回的是字符串
  37.                 //在有一点需要说明!!该加密方式有 长度 限制的!!
  38.                 //##############################################################################
  39.  
  40.                 //RSA的加密函数  string
  41.                 public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString )
  42.                 {
  43.                        
  44.                         byte[] PlainTextBArray;
  45.                         byte[] CypherTextBArray;
  46.                         string Result;
  47.                         RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  48.                         rsa.FromXmlString(xmlPublicKey);
  49.                         PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
  50.                         CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
  51.                         Result=Convert.ToBase64String(CypherTextBArray);
  52.                         return Result;
  53.                        
  54.                 }
  55.                 //RSA的加密函数 byte[]
  56.                 public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString )
  57.                 {
  58.                        
  59.                         byte[] CypherTextBArray;
  60.                         string Result;
  61.                         RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  62.                         rsa.FromXmlString(xmlPublicKey);
  63.                         CypherTextBArray = rsa.Encrypt(EncryptString, false);
  64.                         Result=Convert.ToBase64String(CypherTextBArray);
  65.                         return Result;
  66.                        
  67.                 }
  68.                 #endregion
  69.  
  70.                 #region RSA的解密函数
  71.                 //RSA的解密函数  string
  72.                 public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString )
  73.                 {                      
  74.                         byte[] PlainTextBArray;
  75.                         byte[] DypherTextBArray;
  76.                         string Result;
  77.                         System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  78.                         rsa.FromXmlString(xmlPrivateKey);
  79.                         PlainTextBArray =Convert.FromBase64String(m_strDecryptString);
  80.                         DypherTextBArray=rsa.Decrypt(PlainTextBArray, false);
  81.                         Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
  82.                         return Result;
  83.                        
  84.                 }
  85.  
  86.                 //RSA的解密函数  byte
  87.                 public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString )
  88.                 {                      
  89.                         byte[] DypherTextBArray;
  90.                         string Result;
  91.                         System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
  92.                         rsa.FromXmlString(xmlPrivateKey);
  93.                         DypherTextBArray=rsa.Decrypt(DecryptString, false);
  94.                         Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
  95.                         return Result;
  96.                        
  97.                 }
  98.                 #endregion
  99.  
  100.                 #endregion
  101.  
  102.                 #region RSA数字签名
  103.  
  104.                 #region 获取Hash描述表
  105.                 //获取Hash描述表 ,sharejs.com
  106.                 public bool GetHash(string m_strSource, ref byte[] HashData)
  107.                 {                      
  108.                         //从字符串中取得Hash描述
  109.                         byte[] Buffer;
  110.                         System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  111.                         Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
  112.                         HashData = MD5.ComputeHash(Buffer);
  113.  
  114.                         return true;                   
  115.                 }
  116.  
  117.                 //获取Hash描述表
  118.                 public bool GetHash(string m_strSource, ref string strHashData)
  119.                 {
  120.                        
  121.                         //从字符串中取得Hash描述
  122.                         byte[] Buffer;
  123.                         byte[] HashData;
  124.                         System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  125.                         Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
  126.                         HashData = MD5.ComputeHash(Buffer);
  127.  
  128.                         strHashData = Convert.ToBase64String(HashData);
  129.                         return true;
  130.                        
  131.                 }
  132.  
  133.                 //获取Hash描述表
  134.                 public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
  135.                 {
  136.                        
  137.                         //从文件中取得Hash描述
  138.                         System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  139.                         HashData = MD5.ComputeHash(objFile);
  140.                         objFile.Close();
  141.  
  142.                         return true;
  143.                        
  144.                 }
  145.  
  146.                 //获取Hash描述表
  147.                 public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
  148.                 {
  149.                        
  150.                         //从文件中取得Hash描述
  151.                         byte[] HashData;
  152.                         System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
  153.                         HashData = MD5.ComputeHash(objFile);
  154.                         objFile.Close();
  155.  
  156.                         strHashData = Convert.ToBase64String(HashData);
  157.  
  158.                         return true;
  159.                        
  160.                 }
  161.                 #endregion
  162.  
  163.                 #region RSA签名
  164.                 //RSA签名
  165.                 public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
  166.                 {
  167.                        
  168.                                 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  169.  
  170.                                 RSA.FromXmlString(p_strKeyPrivate);
  171.                                 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  172.                                 //设置签名的算法为MD5
  173.                                 RSAFormatter.SetHashAlgorithm("MD5");
  174.                                 //执行签名
  175.                                 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  176.  
  177.                                 return true;
  178.                        
  179.                 }
  180.  
  181.                 //RSA签名
  182.                 public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData)
  183.                 {
  184.                        
  185.                                 byte[] EncryptedSignatureData;
  186.  
  187.                                 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  188.  
  189.                                 RSA.FromXmlString(p_strKeyPrivate);
  190.                                 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  191.                                 //设置签名的算法为MD5
  192.                                 RSAFormatter.SetHashAlgorithm("MD5");
  193.                                 //执行签名
  194.                                 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  195.  
  196.                                 m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
  197.  
  198.                                 return true;
  199.                        
  200.                 }
  201.  
  202.                 //RSA签名
  203.                 public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData)
  204.                 {
  205.                        
  206.                                 byte[] HashbyteSignature;
  207.  
  208.                                 HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
  209.                                 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  210.  
  211.                                 RSA.FromXmlString(p_strKeyPrivate);
  212.                                 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  213.                                 //设置签名的算法为MD5
  214.                                 RSAFormatter.SetHashAlgorithm("MD5");
  215.                                 //执行签名
  216.                                 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  217.  
  218.                                 return true;
  219.                        
  220.                 }
  221.  
  222.                 //RSA签名
  223.                 public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData)
  224.                 {
  225.                        
  226.                                 byte[] HashbyteSignature;
  227.                                 byte[] EncryptedSignatureData;
  228.  
  229.                                 HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
  230.                                 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  231.  
  232.                                 RSA.FromXmlString(p_strKeyPrivate);
  233.                                 System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
  234.                                 //设置签名的算法为MD5
  235.                                 RSAFormatter.SetHashAlgorithm("MD5");
  236.                                 //执行签名
  237.                                 EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
  238.  
  239.                                 m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
  240.  
  241.                                 return true;
  242.                        
  243.                 }
  244.                 #endregion
  245.  
  246.                 #region RSA 签名验证
  247.  
  248.                 public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
  249.                 {
  250.                        
  251.                                 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  252.  
  253.                                 RSA.FromXmlString(p_strKeyPublic);
  254.                                 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  255.                                 //指定解密的时候HASH算法为MD5
  256.                                 RSADeformatter.SetHashAlgorithm("MD5");
  257.  
  258.                                 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  259.                                 {
  260.                                         return true;
  261.                                 }
  262.                                 else
  263.                                 {
  264.                                         return false;
  265.                                 }
  266.                        
  267.                 }
  268.  
  269.                 public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData)
  270.                 {
  271.                        
  272.                                 byte[] HashbyteDeformatter;
  273.  
  274.                                 HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
  275.  
  276.                                 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  277.  
  278.                                 RSA.FromXmlString(p_strKeyPublic);
  279.                                 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  280.                                 //指定解密的时候HASH算法为MD5
  281.                                 RSADeformatter.SetHashAlgorithm("MD5");
  282.  
  283.                                 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  284.                                 {
  285.                                         return true;
  286.                                 }
  287.                                 else
  288.                                 {
  289.                                         return false;
  290.                                 }
  291.                        
  292.                 }
  293.  
  294.                 public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData)
  295.                 {
  296.                        
  297.                                 byte[] DeformatterData;
  298.  
  299.                                 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  300.  
  301.                                 RSA.FromXmlString(p_strKeyPublic);
  302.                                 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  303.                                 //指定解密的时候HASH算法为MD5
  304.                                 RSADeformatter.SetHashAlgorithm("MD5");
  305.  
  306.                                 DeformatterData =Convert.FromBase64String(p_strDeformatterData);
  307.  
  308.                                 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  309.                                 {
  310.                                         return true;
  311.                                 }
  312.                                 else
  313.                                 {
  314.                                         return false;
  315.                                 }
  316.                        
  317.                 }
  318.  
  319.                 public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
  320.                 {
  321.                        
  322.                                 byte[] DeformatterData;
  323.                                 byte[] HashbyteDeformatter;
  324.  
  325.                                 HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
  326.                                 System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
  327.  
  328.                                 RSA.FromXmlString(p_strKeyPublic);
  329.                                 System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
  330.                                 //指定解密的时候HASH算法为MD5
  331.                                 RSADeformatter.SetHashAlgorithm("MD5");
  332.  
  333.                                 DeformatterData =Convert.FromBase64String(p_strDeformatterData);
  334.  
  335.                                 if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
  336.                                 {
  337.                                         return true;
  338.                                 }
  339.                                 else
  340.                                 {
  341.                                         return false;
  342.                                 }
  343.                        
  344.                 }
  345.  
  346.  
  347.                 #endregion
  348.  
  349.  
  350.                 #endregion
  351.  
  352.         }
  353. }
  354.  
  355. //csharp/8613

Reply to "Cc customized RSA encryption and decryption and RSA signature and verification encapsulation classes"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website