- 一、获取一个类指定的属性值
- 二、退款响应固定字段
- 三、将参数排序组装
- 四、MD5加签
- 五、MD5withRSA
一、获取一个类指定的属性值
代码如下:
///二、退款响应固定字段/// 获取一个类指定的属性值 /// /// object对象 /// 属性名称 ///public static object GetPropertyValue(object info, string field) { if (info == null) return null; Type t = info.GetType(); IEnumerable property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi; return property.First().GetValue(info, null); }
代码如下:
///三、将参数排序组装/// 获取实体类固定字段 /// /// ///private static Object Deserialize_Data(object obj) { dynamic param = new ExpandoObject(); param.mer_order_no = GetPropertyValue(obj, "field1"); param.mer_refund_no = GetPropertyValue(obj, "field2"); param.refund_state = GetPropertyValue(obj, "field3"); param.nonce_str = GetPropertyValue(obj, "field4"); return param; }
代码如下:
///四、MD5加签/// 将参数排序组装 /// /// /// ///private string BuildParamStr(SortedDictionary param, string key) { if (param == null || param.Count == 0) { return ""; } param.Remove("sign"); SortedDictionary ascDic = new SortedDictionary (); foreach (KeyValuePair item in param) { ascDic.Add(item.Key, item.Value); } StringBuilder sb = new StringBuilder(); foreach (var item in ascDic) { if (!string.IsNullOrEmpty(item.Value)) { sb.Append(item.Key).Append("=").Append(item.Value).Append("&"); } } sb.Append("key=" + key); return sb.ToString(); }
代码如下:
///五、MD5withRSA/// md5加签 /// /// ///public static string MD5Encrypt(string strText) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strText)); return BitConverter.ToString(result).Replace("-", "").ToLower(); }
代码如下:
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using System;
using System.Collections.Generic;
using System.Text;
namespace Tools
{
class CryptoUtil
{
public static Encoding encoding = Encoding.UTF8;
public static string SignerSymbol = "MD5withRSA";
private static AsymmetricKeyParameter CreateKEY(bool isPrivate, string key)
{
byte[] keyInfoByte = Convert.FromBase64String(key);
if (isPrivate)
return PrivateKeyFactory.CreateKey(keyInfoByte);
else
return PublicKeyFactory.CreateKey(keyInfoByte);
}
public static string Sign(string content, string privatekey)
{
ISigner sig = SignerUtilities.GetSigner(SignerSymbol);
sig.Init(true, CreateKEY(true, privatekey));
var bytes = encoding.GetBytes(content);
sig.BlockUpdate(bytes, 0, bytes.Length);
byte[] signature = sig.GenerateSignature();
var signedString = Convert.ToBase64String(signature);
return signedString;
}
public static bool Verify(string content, string signData, string publickey)
{
ISigner signer = SignerUtilities.GetSigner(SignerSymbol);
signer.Init(false, CreateKEY(false, publickey));
var expectedSig = Convert.FromBase64String(signData);
var msgBytes = encoding.GetBytes(content);
signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
return signer.VerifySignature(expectedSig);
}
}
}



