using System;
|
|
using System.IO;
|
|
using com.hitrust.Security.Certificates;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using Bwp.ABCClient2.Core;
|
|
using System.Text;
|
|
using System.IO.Compression;
|
|
namespace com.hitrust.trustpay.client
|
|
{
|
|
/// <summary>
|
|
/// 用来替换原来的配置,以适应我们的程序的需要
|
|
/// </summary>
|
|
public class MerchantConfig
|
|
{
|
|
public static Encoding DefaultEncoding = Encoding.GetEncoding("GB18030");
|
|
|
|
public MerchantConfig()
|
|
{
|
|
}
|
|
|
|
static ABCClientConfig Config
|
|
{
|
|
get
|
|
{
|
|
return new ABCClientConfig();
|
|
}
|
|
}
|
|
|
|
public static BufferedStream getTrxLogFile()
|
|
{
|
|
BufferedStream tLogFile = null;
|
|
if (Config.EnableLog)
|
|
{
|
|
string tFileName = "";
|
|
try
|
|
{
|
|
HiCalendar tHiCalendar = new HiCalendar();
|
|
tFileName = Config.LogPath + "/TrxLog." + tHiCalendar.toString("%Y%m%d.log");
|
|
tLogFile = new BufferedStream(new FileStream(tFileName, FileMode.Append));
|
|
}
|
|
catch (IOException)
|
|
{
|
|
throw new TrxException("1004", "无法写入交易日志文档", " - 系统无法写入交易日志至[" + tFileName + "]中!");
|
|
}
|
|
}
|
|
return tLogFile;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 这里程序传入的aMerchantNo不起作用
|
|
/// 通过外部环境来知道所选择的商户
|
|
/// </summary>
|
|
/// <param name="aMerchantNo"></param>
|
|
/// <returns></returns>
|
|
public static string MerchantID(int aMerchantNo)
|
|
{
|
|
return Config.MerchanID;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 得到商户的私钥
|
|
/// </summary>
|
|
/// <param name="aMerchantNo"></param>
|
|
/// <returns></returns>
|
|
public static RSACryptoServiceProvider MerchantKey(int aMerchantNo)
|
|
{
|
|
return Config.MerchanPrivateKey;
|
|
}
|
|
|
|
public static XMLDocument signMessage(int aMerchantNo, XMLDocument aMessage)
|
|
{
|
|
var tMerchantKey = MerchantKey(aMerchantNo);
|
|
if (tMerchantKey == null)
|
|
{
|
|
throw new Exception("未能找到私钥");
|
|
}
|
|
byte[] tHashedData = new SHA1Managed().ComputeHash(DefaultEncoding.GetBytes(aMessage.ToString()));
|
|
byte[] tSigned = tMerchantKey.SignHash(tHashedData, CryptoConfig.MapNameToOID("SHA1"));
|
|
string tSignedBase64 = Convert.ToBase64String(tSigned);
|
|
return new XMLDocument(aMessage.ToString() + "<Signature-Algorithm>SHA1withRSA</Signature-Algorithm><Signature>" + tSignedBase64 + "</Signature>");
|
|
}
|
|
|
|
public static XMLDocument verifySign(XMLDocument aMessage)
|
|
{
|
|
return verifySign(aMessage, true);
|
|
}
|
|
|
|
public static XMLDocument verifySign(XMLDocument aMessage, bool needMessageHeader)
|
|
{
|
|
XMLDocument tTrxResponse = aMessage.getValue("Message");
|
|
if (tTrxResponse == null)
|
|
{
|
|
throw new TrxException("1301", "网上支付平台的响应报文不完整", "无[Message]段!");
|
|
}
|
|
if (aMessage.getValueNoNull("Signature-Algorithm") == null)
|
|
{
|
|
throw new TrxException("1301", "网上支付平台的响应报文不完整", "无[Signature-Algorithm]段!");
|
|
}
|
|
string tSignBase64 = aMessage.getValueNoNull("Signature");
|
|
if (tSignBase64 == null)
|
|
{
|
|
throw new TrxException("1301", "网上支付平台的响应报文不完整", "无[Signature]段!");
|
|
}
|
|
byte[] tSign = Convert.FromBase64String(tSignBase64);
|
|
try
|
|
{
|
|
SHA1Managed tHash = new SHA1Managed();
|
|
var message = "";
|
|
if (needMessageHeader)
|
|
message = "<Message>" + tTrxResponse.ToString() + "</Message>";
|
|
else
|
|
message = tTrxResponse.ToString();
|
|
byte[] tHashedData = tHash.ComputeHash(DefaultEncoding.GetBytes(message));
|
|
|
|
|
|
var publicKey = (RSACryptoServiceProvider)TrustpayCertificate.PublicKey.Key;
|
|
|
|
bool tResult = publicKey.VerifyHash(tHashedData, Const.SHA1OID, tSign);
|
|
tHash.Clear();
|
|
publicKey.Clear();
|
|
if (!tResult)
|
|
{
|
|
throw new TrxException("1302", "网上支付平台的响应报文签名验证失败");
|
|
}
|
|
}
|
|
catch (TrxException e)
|
|
{
|
|
throw e;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.Out.WriteLine(e);
|
|
throw new TrxException("1302", "网上支付平台的响应报文签名验证失败 - " + e.ToString());
|
|
}
|
|
return tTrxResponse;
|
|
}
|
|
|
|
public static int MerchantNum
|
|
{
|
|
get
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
public static X509Certificate2 TrustpayCertificate
|
|
{
|
|
get
|
|
{
|
|
return (X509Certificate2)Config.ABCPublicKey;
|
|
}
|
|
}
|
|
|
|
public static string TrustPayConnectMethod
|
|
{
|
|
get
|
|
{
|
|
return Config.TrustPayConnectMethod;
|
|
}
|
|
}
|
|
|
|
public static string TrustPayNewLine
|
|
{
|
|
get
|
|
{
|
|
return "\r\n";
|
|
}
|
|
}
|
|
|
|
public static string TrustPayServerName
|
|
{
|
|
get
|
|
{
|
|
return Config.TrustPayServerName;
|
|
}
|
|
}
|
|
|
|
public static int TrustPayServerPort
|
|
{
|
|
get
|
|
{
|
|
return Config.TrustPayServerPort;
|
|
}
|
|
}
|
|
|
|
public static string TrustPayTrxURL
|
|
{
|
|
get
|
|
{
|
|
return Config.TrustPayTrxURL;
|
|
}
|
|
}
|
|
|
|
internal static string DeCompress(string comppressedString)
|
|
{
|
|
byte[] byteInput = Convert.FromBase64String(comppressedString);
|
|
using (var srcStream = new MemoryStream(byteInput))
|
|
{
|
|
using (var destStream = new MemoryStream())
|
|
{
|
|
using (GZipStream decompress = new GZipStream(srcStream, CompressionMode.Decompress))
|
|
{
|
|
decompress.CopyTo(destStream);
|
|
var resultBytes = destStream.ToArray();
|
|
return CompressEncoding.GetString(resultBytes, 0, resultBytes.Length);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static Encoding CompressEncoding = Encoding.GetEncoding("GB18030");
|
|
|
|
internal static string Compress(string uncompressedString)
|
|
{
|
|
byte[] byteData = CompressEncoding.GetBytes(uncompressedString);
|
|
using (MemoryStream srcStream = new MemoryStream(byteData))
|
|
{
|
|
using (var destStream = new MemoryStream())
|
|
{
|
|
using (GZipStream compress = new GZipStream(destStream, CompressionMode.Compress))
|
|
{
|
|
srcStream.CopyTo(compress);
|
|
}
|
|
return Convert.ToBase64String(destStream.ToArray());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|