//namespace com.hitrust.trustpay.client //{ // using com.hitrust.Security.Certificates; // using System; // using System.Collections; // using System.IO; // using System.Net; // using System.Net.Sockets; // using System.Security.Cryptography; // using System.Text; // using System.Security.Cryptography.X509Certificates; // public class MerchantConfig // { // private static bool iIsInitialed = false; // private static bool iIsLog = false; // private static string iKeyStoreType = "0"; // private static string iLogPath = ""; // private static ArrayList iMerchantCertificates = new ArrayList(); // private static ArrayList iMerchantIDs = new ArrayList(); // private static ArrayList iMerchantKeys = new ArrayList(); // private static int iMerchantNum = 1; // private static string iNewLine = "1"; // private static IniFileParser iResourceBundle = null; // private static string iSHA1OID = CryptoConfig.MapNameToOID("SHA1"); // private static X509Certificate iTrustpayCertificate = null; // private static string iTrustPayConnectMethod = "http"; // private static string iTrustPayServerName = ""; // private static int iTrustPayServerPort = 0; // private static string iTrustPayTrxURL = ""; // public const string KEY_STORE_TYPE_FILE = "0"; // public const string KEY_STORE_TYPE_SIGN_SERVER = "1"; // private const string RESOURCE_NAME = @"C:\WINNT\system32\TrustMerchant.ini"; // private const string SIGNATURE_ALGORITHM = "SHA1withRSA"; // public MerchantConfig() // { // bundle(); // } // private static void bindMerchantCertificateByFile() // { // string tMerchantCertFiles = getParameterByName("MerchantCertFile"); // if (tMerchantCertFiles.Length == 0) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误", "商户证书储存目录档名[MerchantCertFile]配置错误!"); // } // string tMerchantCertPasswords = getParameterByName("MerchantCertPassword"); // if (tMerchantCertFiles.Length == 0) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误", "商户私钥加密密码[MerchantCertPassword]配置错误!"); // } // char[] tSplit = new char[] { ',' }; // string[] tMerchantCertFileArray = tMerchantCertFiles.Split(tSplit, 100); // string[] tMerchantCertPasswordArray = tMerchantCertPasswords.Split(tSplit, 100); // if ((iMerchantNum != tMerchantCertFileArray.Length) || (iMerchantNum != tMerchantCertPasswordArray.Length)) // { // throw new TrxException("1007", "配置文件中MerchantID、MerchantCertFile、MerchantCertPassword属性个数不一致", ""); // } // iMerchantCertificates.Clear(); // iMerchantKeys.Clear(); // for (int i = 0; i < iMerchantNum; i++) // { // Exception e; // Certificate tCertificate = null; // if (!System.IO.File.Exists(tMerchantCertFileArray[i])) // { // Console.WriteLine("File does not exist: " + tMerchantCertFileArray[i]); // } // if (tMerchantCertPasswordArray[i] != null) // { // try // { // tCertificate = Certificate.CreateFromPfxFile(tMerchantCertFileArray[i], tMerchantCertPasswordArray[i], true, KeysetLocation.LocalMachine); // } // catch (Exception exception1) // { // e = exception1; // throw new TrxException("1002", "无法读取证书文档", "[" + tMerchantCertFileArray[i] + "]!" + e.Message); // } // } // DateTime tCurDateTime = DateTime.Now; // if (tCertificate.GetExpirationDate() < tCurDateTime) // { // throw new TrxException("1005", "证书过期"); // } // iMerchantCertificates.Add(tCertificate); // try // { // iMerchantKeys.Add(tCertificate.PrivateKey); // } // catch (Exception exception2) // { // e = exception2; // throw new TrxException("1003", "无法读取商户私钥", "无法生成私钥证书对象!" + e.Message); // } // } // } // private static void bundle() // { // if (!iIsInitialed) // { // Exception e; // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 开始===================="); // try // { // string tIniFile = Environment.GetEnvironmentVariable("TrustMerchantIniFile"); // if (tIniFile == null) // { // tIniFile = @"C:\WINNT\system32\TrustMerchant.ini"; // } // iResourceBundle = new IniFileParser(tIniFile); // } // catch (Exception exception1) // { // e = exception1; // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 无法读取商户端配置文件"); // throw new TrxException("1000", "无法读取商户端配置文件"); // } // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 读取系统配置文件"); // iTrustPayConnectMethod = getParameterByName("TrustPayConnectMethod"); // if (iTrustPayConnectMethod.Length == 0) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误 - 网上支付平台通讯方式[TrustPayConnectMethod]配置错误!"); // } // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 网上支付平台通讯方式 = [" + iTrustPayConnectMethod + "]"); // iTrustPayServerName = getParameterByName("TrustPayServerName"); // if (iTrustPayServerName.Length == 0) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误 - 网上支付平台服务器IP[TrustPayServerName]配置错误!"); // } // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 网上支付平台服务器IP = [" + iTrustPayServerName + "]"); // string tTrustPayServerPort = getParameterByName("TrustPayServerPort"); // if (tTrustPayServerPort.Length == 0) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误 - 网上支付平台交易端口[TrustPayServerPort]配置错误!"); // } // try // { // iTrustPayServerPort = int.Parse(tTrustPayServerPort); // } // catch (Exception exception2) // { // e = exception2; // throw new TrxException("1001", "商户端配置文件中参数设置错误 - 网上支付平台交易端口[TrustPayServerPort]配置错误!"); // } // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 网上支付平台交易端口 = [" + tTrustPayServerPort + "]"); // iTrustPayTrxURL = getParameterByName("TrustPayTrxURL"); // if (iTrustPayTrxURL.Length == 0) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误 - 网上支付平台交易网址[TrustPayTrxURL]配置错误!"); // } // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 网上支付平台交易网址 = [" + iTrustPayTrxURL + "]"); // string tNewLine = getParameterByName("TrustPayNewLine"); // if (tNewLine.Length == 0) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误 - 网上支付平台接口特性[TrustPayNewLine]配置错误!"); // } // if (tNewLine.Equals("1")) // { // iNewLine = "\n"; // } // else // { // iNewLine = "\r\n"; // } // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 网上支付平台接口特性 = [" + tNewLine + "]"); // string tTrustPayCertFile = getParameterByName("TrustPayCertFile"); // if (tTrustPayCertFile.Length == 0) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误 - 网上支付平台证书[tTrustPayCertFile]配置错误!"); // } // iTrustpayCertificate = getCertificate(tTrustPayCertFile); // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 网上支付平台证书 = [" + tTrustPayCertFile + "]"); // string tIsLog = getParameterByName("EnableLog", false); // if ((tIsLog != null) && tIsLog.ToUpper().Equals("TRUE")) // { // iIsLog = true; // } // if (iIsLog) // { // iLogPath = getParameterByName("LogPath"); // if (iLogPath.Length == 0) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误 - 商户日志目录[LogPath]配置错误!"); // } // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 日志文件目录 = [" + iLogPath + "]"); // } // string tMerchantIDs = getParameterByName("MerchantID"); // if (tMerchantIDs.Length == 0) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误 - 商户号[MerchantID]配置错误!"); // } // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 商户编号 = [" + tMerchantIDs + "]"); // char[] tSplit = new char[] { ',' }; // string[] tMerchantIDArray = tMerchantIDs.Split(tSplit, 100); // iMerchantNum = tMerchantIDArray.Length; // iMerchantIDs.Clear(); // for (int i = 0; i < iMerchantNum; i++) // { // iMerchantIDs.Add(tMerchantIDArray[i].Trim()); // } // iKeyStoreType = getParameterByName("MerchantKeyStoreType"); // if (iKeyStoreType.Equals("0")) // { // bindMerchantCertificateByFile(); // } // else if (!iKeyStoreType.Equals("1")) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误 - 证书储存媒体配置错误!"); // } // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 商户证书及私钥初始完成"); // iIsInitialed = true; // Console.Out.WriteLine("[Trustpay商户端API] - 初始 - 完成===================="); // } // } // private static XMLDocument fileSignMessage(int aMerchantNo, XMLDocument aMessage) // { // RSACryptoServiceProvider tMerchantKey = MerchantKey(aMerchantNo); // byte[] tHashedData = new SHA1Managed().ComputeHash(Encoding.Default.GetBytes(aMessage.ToString())); // byte[] tSigned = tMerchantKey.SignHash(tHashedData, iSHA1OID); // string tSignedBase64 = new Base64().encode(tSigned); // return new XMLDocument(aMessage.ToString() + "SHA1withRSA" + tSignedBase64 + ""); // } // private static X509Certificate getCertificate(string tCertFile) // { // X509Certificate tCertificate = null; // try // { // tCertificate = X509Certificate.CreateFromCertFile(tCertFile); // } // catch (Exception) // { // throw new TrxException("1002", "无法读取证书文档[" + tCertFile + "]!"); // } // return tCertificate; // } // public static string getParameterByName(string aParamName) // { // return getParameterByName(aParamName, true); // } // public static string getParameterByName(string aParamName, bool aThrowException) // { // if (iResourceBundle == null) // { // bundle(); // } // string tValue = null; // try // { // tValue = iResourceBundle.getValue(aParamName).Trim(); // if (tValue.Equals("") & aThrowException) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误", " - 未设定[" + aParamName + "]参数值!"); // } // } // catch (Exception) // { // if (aThrowException) // { // throw new TrxException("1001", "商户端配置文件中参数设置错误", " - 无[" + aParamName + "]参数!"); // } // } // return tValue; // } // public static BufferedStream getTrxLogFile() // { // return getTrxLogFile("TrxLog"); // } // public static BufferedStream getTrxLogFile(string aFileName) // { // bundle(); // BufferedStream tLogFile = null; // if (iIsLog) // { // string tFileName = ""; // try // { // HiCalendar tHiCalendar = new HiCalendar(); // tFileName = iLogPath + "/" + aFileName + "." + tHiCalendar.toString("%Y%m%d.log"); // tLogFile = new BufferedStream(new FileStream(tFileName, FileMode.Append)); // } // catch (IOException) // { // throw new TrxException("1004", "无法写入交易日志文档", " - 系统无法写入交易日志至[" + tFileName + "]中!"); // } // } // return tLogFile; // } // public static Certificate MerchantCertificate(int aMerchantNo) // { // bundle(); // return (Certificate) iMerchantCertificates[aMerchantNo - 1]; // } // public static string MerchantID(int aMerchantNo) // { // bundle(); // return (string) iMerchantIDs[aMerchantNo - 1]; // } // public static RSACryptoServiceProvider MerchantKey(int aMerchantNo) // { // bundle(); // return (RSACryptoServiceProvider) iMerchantKeys[aMerchantNo - 1]; // } // public static XMLDocument signMessage(int aMerchantNo, XMLDocument aMessage) // { // bundle(); // XMLDocument tMessage = null; // try // { // string tKeyStoreType = KeyStoreType; // if (tKeyStoreType.Equals("0")) // { // return fileSignMessage(aMerchantNo, aMessage); // } // if (tKeyStoreType.Equals("1")) // { // tMessage = signServerSignMessage(aMessage); // } // } // catch (TrxException e) // { // throw e; // } // catch (Exception e) // { // throw new TrxException("1102", "签名交易报文时发生错误 - " + e.Message); // } // return tMessage; // } // private static XMLDocument signServerSignMessage(XMLDocument aMessage) // { // string tMessage = ""; // Socket tSocket = null; // string tSignServerIP = getParameterByName("SignServerIP"); // int tSignServerPort = int.Parse(getParameterByName("SignServerPort")); // string tSignServerPass = getParameterByName("SignServerPassword"); // try // { // IPEndPoint tIPEndPoint = new IPEndPoint(Dns.GetHostEntry(tSignServerIP).AddressList[0], tSignServerPort); // tSocket = new Socket(tIPEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // tSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 0x2710); // tSocket.Connect(tIPEndPoint); // string tData = new Base64().encode(Encoding.UTF8.GetBytes(aMessage.ToString())); // string tSignRequest = "" + tSignServerPass + "SHA1withRSA" + tData + "\n"; // tSocket.Send(Encoding.ASCII.GetBytes(tSignRequest)); // byte[] tSignResponseBytes = new byte[0x800]; // int tLen = tSocket.Receive(tSignResponseBytes); // XMLDocument tSignedResponse = new XMLDocument(Encoding.ASCII.GetString(tSignResponseBytes, 0, tLen)); // string tSignBase64 = ""; // if (!tSignedResponse.getValueNoNull("RC").Equals("0")) // { // throw new TrxException("1104", "签名服务器返回签名错误", "错误代码[" + tSignedResponse.getValueNoNull("RC") + "]"); // } // tSignBase64 = tSignedResponse.getValueNoNull("Signature"); // tMessage = "" + aMessage.ToString() + "SHA1withRSA" + tSignBase64 + ""; // } // catch (TrxException e) // { // throw e; // } // catch (Exception e) // { // throw new TrxException("1103", "无法连线签名服务器", e.StackTrace); // } // finally // { // if (tSocket != null) // { // tSocket.Close(); // } // } // return new XMLDocument(tMessage); // } // public static XMLDocument verifySign(XMLDocument aMessage) // { // bundle(); // 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 = new Base64().decode(tSignBase64); // try // { // SHA1Managed tHash = new SHA1Managed(); // byte[] tHashedData = tHash.ComputeHash(Encoding.Default.GetBytes("" + tTrxResponse.ToString() + "")); // RSACryptoServiceProvider tRSACryptoServiceProvider = new RSACryptoServiceProvider(new CspParameters { Flags = CspProviderFlags.UseMachineKeyStore }); // //RSAParameters tRSAParameters = TrustpayCertificate.PublicKey.ExportParameters(false); // RSAParameters parameters = new RSAParameters() { Exponent = TrustpayCertificate.GetPublicKey() }; // tRSACryptoServiceProvider.ImportParameters(parameters); // bool tResult = tRSACryptoServiceProvider.VerifyHash(tHashedData, iSHA1OID, tSign); // tHash.Clear(); // tRSACryptoServiceProvider.Clear(); // if (!tResult) // { // throw new TrxException("1302", "网上支付平台的响应报文签名验证失败"); // } // } // catch (TrxException e) // { // throw e; // } // catch (Exception e) // { // Console.Out.WriteLine(e); // throw new TrxException("1302", "网上支付平台的响应报文签名验证失败 - " + e.Message); // } // return tTrxResponse; // } // public static string KeyStoreType // { // get // { // bundle(); // return iKeyStoreType; // } // } // public static int MerchantNum // { // get // { // bundle(); // return iMerchantNum; // } // } // public static X509Certificate TrustpayCertificate // { // get // { // bundle(); // return iTrustpayCertificate; // } // } // public static string TrustPayConnectMethod // { // get // { // bundle(); // return iTrustPayConnectMethod; // } // } // public static string TrustPayNewLine // { // get // { // bundle(); // return iNewLine; // } // } // public static string TrustPayServerName // { // get // { // bundle(); // return iTrustPayServerName; // } // } // public static int TrustPayServerPort // { // get // { // bundle(); // return iTrustPayServerPort; // } // } // public static string TrustPayTrxURL // { // get // { // bundle(); // return iTrustPayTrxURL; // } // } // } //}