C# 操作pem 文件
Posted 笨小孩做开发
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 操作pem 文件相关的知识,希望对你有一定的参考价值。
using Dscf.Bpl.InformationAuditBpl;
using Dscf.Bpl.ProductBpl;
using Dscf.Global.CommonAduit;
using Dscf.Global.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using LitJson;
using Dscf.Client.InformationAuditWeb.App_Start;
using System.Configuration;
using System.Web.SessionState;
using Dscf.Bpl.UserBpl;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Text;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
namespace Dscf.Client.InformationAuditWeb.Handler
{
/// <summary>
/// ZhiChengHandler 的摘要说明
/// </summary>
public class ZhiChengHandler : IHttpHandler, IReadOnlySessionState, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
string type = context.Request["type"];
context.Response.ContentType = "text/plain";
switch (type)
{
case "0"://查询显示
{
context.Response.Write(getUserinfo(context));
break;
}
case "1"://借款显示
{
context.Response.Write(Loadselect(context));
break;
}
}
}
/// <summary>
/// 查询人员身份信息
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public UserInfoModel getUserInfoModel(UserInfoModel model)
{
UserInfoModel userinfo = new UserInfoModel();
try
{
userinfo = UserInfoBpl.QueryObject(model);
return userinfo;
}
catch (Exception ex)
{
}
return userinfo;
}
/// <summary>
/// 返回结果
/// </summary>
/// <param name="url"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static string ResponseBind(string url, IDictionary<string, string> parameters)
{
SecurityUtils su = new SecurityUtils();//给值
string strReturn = su.ReturnActionFromXml(url, parameters);
//JObject jo = (JObject)JsonConvert.DeserializeObject(strReturn);
//string responsestr = jo["response"] != null ? jo["response"].ToString().Trim() : "";//执行结果
//JObject jo2 = (JObject)JsonConvert.DeserializeObject(responsestr);
//return jo2;
return strReturn;
}
/// <summary>
/// 获取个人信息
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public ResultMessage getUserinfo(HttpContext context)
{
try
{
int orderId;
if (int.TryParse(context.Request["Userid"], out orderId))
{
UserInfoModel userinfo = new UserInfoModel();
userinfo.UserId = Convert.ToInt32(context.Request["Userid"]);
userinfo = getUserInfoModel(userinfo);
if (userinfo != null)
{
return new ResultMessage(true, "成功", userinfo);
}
else
{
return new ResultMessage(false, "失败");
}
}
else
{
return new ResultMessage(false, "暂无数据!");
}
}
catch (Exception ex)
{
return new ResultMessage(false, ex.Message);
}
}
/// <summary>
/// 借款查询
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public ResultMessage Loadselect(HttpContext context)
{
try
{
int orderId;
if (int.TryParse(context.Request["Userid"], out orderId))
{
UserInfoModel userinfo = new UserInfoModel();
userinfo.UserId = Convert.ToInt32(context.Request["Userid"]);
userinfo = getUserInfoModel(userinfo);
if (userinfo != null)
{
//读取文件
using (var fileStream = System.IO.File.OpenText(context.Server.MapPath("/attached/publickey/ZC_PublicKey_V2.pem")))
{
var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(fileStream);
var keyParameter = (Org.BouncyCastle.Crypto.AsymmetricKeyParameter)pemReader.ReadObject();
//userid
string input = "dingsheng_testusr";
byte[] testData = Encoding.UTF8.GetBytes(input);
var decryptEngine = new Pkcs1Encoding(new RsaEngine());
decryptEngine.Init(true, keyParameter);
input = Convert.ToBase64String(decryptEngine.ProcessBlock(testData, 0, testData.Length));
//params
ZhichengLoanSerachModel ZhichengLoanSerachModel = new ZhichengLoanSerachModel();
ZhichengLoanSerachModel.name = userinfo.Name;
ZhichengLoanSerachModel.idType = "101";
ZhichengLoanSerachModel.idNo = userinfo.IDCard;
ZhichengLoanSerachModel.queryReason = "10";
//把对象序列化成json
SerializerHelper serializer = new SerializerHelper();
string param = JsonConvert.SerializeObject(ZhichengLoanSerachModel);
testData = Encoding.UTF8.GetBytes(param);
testData = new RC4Crypto().EncryptEx(testData, "bd3fe9f1bf134f50");
param = Convert.ToBase64String(testData);
IDictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("userid", HttpUtility.UrlEncode(input));
parameters.Add("params", HttpUtility.UrlEncode(param));
//解析请求结果
SecurityUtils su = new SecurityUtils();
string _creditUrl = "http://www.zhichengcredit.com/CreditPortal/api/queryLoan/V2";
string result = su.ReturnActionFromXml(_creditUrl, parameters);
JObject jo = JObject.Parse(result);
String data = jo["data"].ToString();
var resultStr = new RC4Crypto().Decrypt(data, "bd3fe9f1bf134f50", CryptoBase.EncoderMode.Base64Encoder);
string json = HttpUtility.UrlDecode(resultStr);
var model = JsonConvert.DeserializeObject<ZhichengLoanSeachResultModel>(json);
return new ResultMessage(true, "成功", model);
}
// return new ResultMessage(true, "成功", userinfo);
}
else
{
return new ResultMessage(false, "失败");
}
}
else
{
return new ResultMessage(false, "暂无数据!");
}
}
catch (Exception ex)
{
return new ResultMessage(false, ex.Message);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
以上是关于C# 操作pem 文件的主要内容,如果未能解决你的问题,请参考以下文章
仅使用 Bouncy Castle 读取 PEM RSA 公钥