C#开发记录

C#开发记录,第1张

文章目录
  • 一、获取一个类指定的属性值
  • 二、退款响应固定字段
  • 三、将参数排序组装
  • 四、MD5加签
  • 五、MD5withRSA


一、获取一个类指定的属性值

代码如下:

        /// 
        /// 获取一个类指定的属性值
        /// 
        /// object对象
        /// 属性名称
        /// 
        public static object GetPropertyValue(object info, string field)
        {
            if (info == null) return null;
            Type t = info.GetType();
            IEnumerable<System.Reflection.PropertyInfo> 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;
        }
三、将参数排序组装

代码如下:

 /// 
        /// 将参数排序组装
        /// 
        /// 
        /// 
        /// 
        private string BuildParamStr(SortedDictionary<string, string> param, string key)
        {
            if (param == null || param.Count == 0)
            {
                return "";
            }
            param.Remove("sign");
            SortedDictionary<String, String> ascDic = new SortedDictionary<String, String>();
            foreach (KeyValuePair<string, string> 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();
        }
四、MD5加签

代码如下:

        /// 
        /// 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();
        }
五、MD5withRSA

代码如下:

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();


            /* Base 64 encode the sig so its 8-bit clean */
            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);

            /* Get the bytes to be signed from the string */
            var msgBytes = encoding.GetBytes(content);

            /* Calculate the signature and see if it matches */
            signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
            return signer.VerifySignature(expectedSig);
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/726005.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存