用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

非对称RSA加解密,私钥加密/公钥解密

2013-11-22 作者: 小蜜锋举报

[c#]代码库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;

namespace Kad.ThridParty.ChinaPayWap
{
    /// <summary>
    /// 非对称RSA加解密,私钥加密/公钥解密
    /// 仅用于银联Wap支付报文收发
    /// By : EnVon(E旺) 2013-08-20
    /// </summary>
    internal class RSAHelper
    {

        /// <summary>
        /// RSA加密(用私钥加密哟)
        /// </summary>
        /// <param name="key">私钥</param>
        /// <param name="data">待加密的数据</param>
        /// <returns></returns>
        public static byte[] Encrypt(String key, byte[] data)
        {
            //由密钥xml取得RSA对象
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(key);
            //取得加密时使用的2个参数
            RSAParameters par = rsa.ExportParameters(true);
            BigInteger mod = new BigInteger(par.Modulus);
            BigInteger ep = new BigInteger(par.D);
            //计算填充长度
            int mLen = par.Modulus.Length;
            int fLen = mLen - data.Length - 3;
            //组建bytes
            List<byte> lis = new List<byte>();
            lis.Add(0x00);
            lis.Add(0x01);//兼容java
            for (int i = 0; i < fLen; i++) lis.Add(0xff);
            lis.Add(0x00);
            lis.AddRange(data);
            byte[] bytes = lis.ToArray();
            //加密就这么简单?
            BigInteger m = new BigInteger(bytes);
            BigInteger c = m.modPow(ep, mod);
            return c.getBytes();
        }


        /// <summary>
        /// RSA解密(用公钥解密哟)
        /// </summary>
        /// <param name="key">公钥</param>
        /// <param name="data">待解密的数据</param>
        /// <returns></returns>
        public static byte[] Decrypt(String key, byte[] data)
        {
            //由密钥xml取得RSA对象
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(key);
            //取得解密时使用的2个参数
            RSAParameters par = rsa.ExportParameters(false);
            BigInteger mod = new BigInteger(par.Modulus);
            BigInteger ep = new BigInteger(par.Exponent);
            //解密?
            BigInteger m = new BigInteger(data);
            BigInteger c = m.modPow(ep, mod);
            byte[] bytes = c.getBytes();
            //去掉填充域(头部可能填充了一段0xff)
            byte flag = 0;
            for (int i = 1/*我从1开始啦*/; i < bytes.Length; i++)
            {
                if (bytes[i] == flag && i != (bytes.Length - 1))
                {
                    byte[] retBytes = new byte[bytes.Length - i - 1];
                    Array.Copy(bytes, i + 1, retBytes, 0, retBytes.Length);
                    return retBytes;
                }
            }
            return bytes;
        }


        /// <summary>
        /// 取得证书私钥
        /// </summary>
        /// <param name="pfxPath">证书的绝对路径</param>
        /// <param name="password">访问证书的密码</param>
        /// <returns></returns>
        public static String GetPrivateKey(string pfxPath, string password)
        {
            X509Certificate2 pfx = new X509Certificate2(pfxPath, password, X509KeyStorageFlags.Exportable);
            string privateKey = pfx.PrivateKey.ToXmlString(true);
            return privateKey;
        }


        /// <summary>
        /// 取得证书的公钥
        /// </summary>
        /// <param name="cerPath">证书的绝对路径</param>
        /// <returns></returns>
        public static String GetPublicKey(string cerPath)
        {
            X509Certificate2 cer = new X509Certificate2(cerPath);
            string publicKey = cer.PublicKey.Key.ToXmlString(false);
            return publicKey;
        }

    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...