Example #1
0
        /// <summary>
        /// 证书方式签名(多证书时使用),指定证书路径。
        /// </summary>
        /// <param name="reqData"></param>
        /// <param name="encoding">编码</param>
        /// <param name="certPath">证书路径</param>
        /// <param name="certPwd">证书密码</param>
        /// <returns></returns>
        public static void SignByCertInfo(Dictionary <string, string> reqData, string certPath, string certPwd, Encoding encoding)
        {
            if (!reqData.ContainsKey("signMethod"))
            {
                Log.Error("signMethod must Not null");
                return;
            }
            string signMethod = reqData["signMethod"];

            if (!reqData.ContainsKey("version"))
            {
                Log.Error("version must Not null");
                return;
            }
            string version = reqData["version"];

            if ("01".Equals(signMethod))
            {
                reqData["certId"] = CertUtil.GetSignCertId(certPath, certPwd);

                //将Dictionary信息转换成key1=value1&key2=value2的形式
                string stringData = SDKUtil.CreateLinkString(reqData, true, false, encoding);
                Log.Info("待签名排序串:[" + stringData + "]");

                if ("5.0.0".Equals(version))
                {
                    byte[] signDigest = SecurityUtil.Sha1(stringData, encoding);

                    string stringSignDigest = SDKUtil.ByteArray2HexString(signDigest);
                    Log.Info("sha1结果:[" + stringSignDigest + "]");

                    byte[] byteSign = SecurityUtil.SignSha1WithRsa(CertUtil.GetSignKeyFromPfx(certPath, certPwd), encoding.GetBytes(stringSignDigest));

                    string stringSign = Convert.ToBase64String(byteSign);
                    Log.Info("5.0.0报文sha1RSA签名结果:[" + stringSign + "]");

                    //设置签名域值
                    reqData["signature"] = stringSign;
                }
                else
                {
                    byte[] signDigest = SecurityUtil.Sha256(stringData, encoding);

                    string stringSignDigest = SDKUtil.ByteArray2HexString(signDigest);
                    Log.Info("sha256结果:[" + stringSignDigest + "]");

                    byte[] byteSign = SecurityUtil.SignSha256WithRsa(CertUtil.GetSignKeyFromPfx(certPath, certPwd), encoding.GetBytes(stringSignDigest));

                    string stringSign = Convert.ToBase64String(byteSign);
                    Log.Info("5.1.0报文sha256RSA签名结果:[" + stringSign + "]");

                    //设置签名域值
                    reqData["signature"] = stringSign;
                }
            }
            else
            {
                Log.Error("Error signMethod [" + signMethod + "] in SignByCertInfo. ");
            }
        }
Example #2
0
 /// <summary>
 /// 解密,多证书
 /// </summary>
 /// <param name="dataString">原字符串</param>
 /// <param name="encoding">编码</param>
 /// <param name="certPath"></param>
 /// <param name="certPwd"></param>
 /// <returns>解密结果</returns>
 public static string DecryptData(string dataString, Encoding encoding, string certPath, string certPwd)
 {
     byte[] data = Convert.FromBase64String(dataString);
     data = DecryptData(data, CertUtil.GetSignKeyFromPfx(certPath, certPwd));
     return(encoding.GetString(data));
 }