Asymmetric RSA encryption and decryption, private key encryption / public key decryption

From , 5 Years ago, written in C#, viewed 78 times.
URL https://pastebin.vip/view/dda04f9d
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography.X509Certificates;
  6. using System.Security.Cryptography;
  7.  
  8. namespace Kad.ThridParty.ChinaPayWap
  9. {
  10.     /// <summary>
  11.     /// 非对称RSA加解密,私钥加密/公钥解密
  12.     /// 仅用于银联Wap支付报文收发
  13.     /// By : EnVon(E旺) 2013-08-20
  14.     /// </summary>
  15.     internal class RSAHelper
  16.     {
  17.  
  18.         /// <summary>
  19.         /// RSA加密(用私钥加密哟)
  20.         /// </summary>
  21.         /// <param name="key">私钥</param>
  22.         /// <param name="data">待加密的数据</param>
  23.         /// <returns></returns>
  24.         public static byte[] Encrypt(String key, byte[] data)
  25.         {
  26.             //由密钥xml取得RSA对象
  27.             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  28.             rsa.FromXmlString(key);
  29.             //取得加密时使用的2个参数
  30.             RSAParameters par = rsa.ExportParameters(true);
  31.             BigInteger mod = new BigInteger(par.Modulus);
  32.             BigInteger ep = new BigInteger(par.D);
  33.             //计算填充长度
  34.             int mLen = par.Modulus.Length;
  35.             int fLen = mLen - data.Length - 3;
  36.             //组建bytes
  37.             List<byte> lis = new List<byte>();
  38.             lis.Add(0x00);
  39.             lis.Add(0x01);//兼容java
  40.             for (int i = 0; i < fLen; i++) lis.Add(0xff);
  41.             lis.Add(0x00);
  42.             lis.AddRange(data);
  43.             byte[] bytes = lis.ToArray();
  44.             //加密就这么简单?
  45.             BigInteger m = new BigInteger(bytes);
  46.             BigInteger c = m.modPow(ep, mod);
  47.             return c.getBytes();
  48.         }
  49.  
  50.  
  51.         /// <summary>
  52.         /// RSA解密(用公钥解密哟)
  53.         /// </summary>
  54.         /// <param name="key">公钥</param>
  55.         /// <param name="data">待解密的数据</param>
  56.         /// <returns></returns>
  57.         public static byte[] Decrypt(String key, byte[] data)
  58.         {
  59.             //由密钥xml取得RSA对象
  60.             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  61.             rsa.FromXmlString(key);
  62.             //取得解密时使用的2个参数
  63.             RSAParameters par = rsa.ExportParameters(false);
  64.             BigInteger mod = new BigInteger(par.Modulus);
  65.             BigInteger ep = new BigInteger(par.Exponent);
  66.             //解密?
  67.             BigInteger m = new BigInteger(data);
  68.             BigInteger c = m.modPow(ep, mod);
  69.             byte[] bytes = c.getBytes();
  70.             //去掉填充域(头部可能填充了一段0xff)
  71.             byte flag = 0;
  72.             for (int i = 1/*我从1开始啦*/; i < bytes.Length; i++)
  73.             {
  74.                 if (bytes[i] == flag && i != (bytes.Length - 1))
  75.                 {
  76.                     byte[] retBytes = new byte[bytes.Length - i - 1];
  77.                     Array.Copy(bytes, i + 1, retBytes, 0, retBytes.Length);
  78.                     return retBytes;
  79.                 }
  80.             }
  81.             return bytes;
  82.         }
  83.  
  84.  
  85.         /// <summary>
  86.         /// 取得证书私钥
  87.         /// </summary>
  88.         /// <param name="pfxPath">证书的绝对路径</param>
  89.         /// <param name="password">访问证书的密码</param>
  90.         /// <returns></returns>
  91.         public static String GetPrivateKey(string pfxPath, string password)
  92.         {
  93.             X509Certificate2 pfx = new X509Certificate2(pfxPath, password, X509KeyStorageFlags.Exportable);
  94.             string privateKey = pfx.PrivateKey.ToXmlString(true);
  95.             return privateKey;
  96.         }
  97.  
  98.  
  99.         /// <summary>
  100.         /// 取得证书的公钥
  101.         /// </summary>
  102.         /// <param name="cerPath">证书的绝对路径</param>
  103.         /// <returns></returns>
  104.         public static String GetPublicKey(string cerPath)
  105.         {
  106.             X509Certificate2 cer = new X509Certificate2(cerPath);
  107.             string publicKey = cer.PublicKey.Key.ToXmlString(false);
  108.             return publicKey;
  109.         }
  110.  
  111.     }
  112. }
  113.  

Reply to "Asymmetric RSA encryption and decryption, private key encryption / public key decryption"

Here you can reply to the paste above

captcha

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