/// <summary> /// 通用反馈 收到tar就回 /// </summary> private void CurrencyTarBack(EBD ebdb, string value) { try { ServerForm.DeleteFolder(ServerForm.strBeSendFileMakeFolder);//删除原有XML发送文件的文件夹下的XML XmlDocument xmlDoc = new XmlDocument(); responseXML rp = new responseXML(); //rp.SourceAreaCode = ServerForm.strSourceAreaCode; //rp.SourceType = ServerForm.strSourceType; //rp.SourceName = ServerForm.strSourceName; //rp.SourceID = ServerForm.strSourceID; //rp.sHBRONO = SingletonInfo.GetInstance().CurrentResourcecode; string fName = "10" + SingletonInfo.GetInstance().CurrentResourcecode + BBSHelper.GetSequenceCodes(); xmlDoc = rp.EBDResponse(ebdb, "EBDResponse", fName, value); string xmlSignFileName = "\\EBDB_" + fName + ".xml"; CreateXML(xmlDoc, ServerForm.strBeSendFileMakeFolder + xmlSignFileName); //进行签名 ServerForm.mainFrm.GenerateSignatureFile(ServerForm.strBeSendFileMakeFolder, fName); ServerForm.tar.CreatTar(ServerForm.strBeSendFileMakeFolder, ServerForm.sSendTarPath, fName);//使用新TAR string sSendTarName = ServerForm.sSendTarPath + "\\EBDT_" + fName + ".tar"; FileStream fsSnd = new FileStream(sSendTarName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fsSnd); //时间戳 int datalen = (int)fsSnd.Length + 2; int bufferLength = 4096; long offset = 0; //开始上传时间 writeHeader(datalen.ToString(), "EBDT_" + fName + ".tar"); byte[] buffer = new byte[4096]; //已上传的字节数 int size = br.Read(buffer, 0, bufferLength); while (size > 0) { outputStream.Write(buffer, 0, size); offset += size; size = br.Read(buffer, 0, bufferLength); } outputStream.Write(Encoding.UTF8.GetBytes(sEndLine), 0, 2); outputStream.Flush();//提交写入的数据 fsSnd.Close(); } catch (Exception esb) { Console.WriteLine("401:" + esb.Message); } }
/// <summary> /// 验签函数 及通用反馈 /// </summary> /// <param name="filepath"></param> /// <param name="PlatformVerifySignatureresule"></param> public void DealTarBack(string filepath, out bool PlatformVerifySignatureresule) { PlatformVerifySignatureresule = false;//验签是否通过 EBD ebdb = null; if (File.Exists(filepath)) { try { #region 先删除预处理解压缩包中的文件 foreach (string xmlfiledel in Directory.GetFileSystemEntries(ServerForm.strBeUnTarFolder)) { if (File.Exists(xmlfiledel)) { FileInfo fi = new FileInfo(xmlfiledel); if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1) { fi.Attributes = FileAttributes.Normal; } File.Delete(xmlfiledel);//直接删除其中的文件 } } #endregion End ServerForm.tar.UnpackTarFiles(filepath, ServerForm.strBeUnTarFolder); //把压缩包解压到专门存放接收到的XML文件的文件夹下 string[] xmlfilenames = Directory.GetFiles(ServerForm.strBeUnTarFolder, "*.xml"); //从解压XML文件夹下获取解压的XML文件名 string sTmpFile = string.Empty; string sAnalysisFileName = ""; string sSignFileName = ""; if (mainForm.m_UsbPwsSupport == "1") { if (xmlfilenames.Length < 2)//没有签名文件 { PlatformVerifySignatureresule = false; } } for (int i = 0; i < xmlfilenames.Length; i++) { sTmpFile = Path.GetFileName(xmlfilenames[i]); if (sTmpFile.ToUpper().IndexOf("EBDB") > -1 && sTmpFile.ToUpper().IndexOf("EBDS_EBDB") < 0) { sAnalysisFileName = xmlfilenames[i]; } else if (sTmpFile.ToUpper().IndexOf("EBDS_EBDB") > -1) //签名文件 { sSignFileName = xmlfilenames[i]; //签名文件 } } if (!string.IsNullOrWhiteSpace(sAnalysisFileName)) { using (FileStream fsr = new FileStream(sAnalysisFileName, FileMode.Open)) { StreamReader sr = new StreamReader(fsr, Encoding.UTF8); string xmlInfo = sr.ReadToEnd(); xmlInfo = xmlInfo.Replace("xmlns:xs", "xmlns"); sr.Close(); xmlInfo = XmlSerialize.ReplaceLowOrderASCIICharacters(xmlInfo); xmlInfo = XmlSerialize.GetLowOrderASCIICharacters(xmlInfo); ebdb = XmlSerialize.DeserializeXML <EBD>(xmlInfo); } } string myEBDType = string.Empty; if (ebdb != null) { myEBDType = ebdb.EBDType; } if (!string.IsNullOrWhiteSpace(sSignFileName) && myEBDType != "ConnectionCheck") { //读取xml中的文件,转换为byte字节 byte[] xmlArray = File.ReadAllBytes(sAnalysisFileName); #region 签名处理 Console.WriteLine("开始验证签名文件!"); using (FileStream SignFs = new FileStream(sSignFileName, FileMode.Open)) { StreamReader signsr = new StreamReader(SignFs, Encoding.UTF8); string xmlsign = signsr.ReadToEnd(); signsr.Close(); responseXML signrp = new responseXML();//签名回复 XmlDocument xmlSignDoc = new XmlDocument(); try { int nDeviceHandle = (int)ServerForm.mainFrm.phDeviceHandle; xmlsign = XmlSerialize.ReplaceLowOrderASCIICharacters(xmlsign); xmlsign = XmlSerialize.GetLowOrderASCIICharacters(xmlsign); Signature sign = XmlSerialize.DeserializeXML <Signature>(xmlsign); xmlsign = XmlSerialize.ReplaceLowOrderASCIICharacters(xmlsign); xmlsign = XmlSerialize.GetLowOrderASCIICharacters(xmlsign); string PucStr = sign.SignatureValue; byte[] pucsingVi = Encoding.UTF8.GetBytes(sign.SignatureValue); //0是签名通过 var result = ServerForm.mainFrm.usb.PlatformVerifySignature(nDeviceHandle, 1, xmlArray, xmlArray.Length, pucsingVi); PlatformVerifySignatureresule = result == 0; Log.Instance.LogWrite(PlatformVerifySignatureresule ? "签名验证成功" : "签名验证失败-" + result); } catch (Exception ex) { Log.Instance.LogWrite("签名文件错误:" + ex.Message); } } Console.WriteLine("结束验证签名文件!"); #endregion End } ServerForm.DeleteFolder(ServerForm.strBeSendFileMakeFolder);//删除原有XML发送文件的文件夹下的XML if (!PlatformVerifySignatureresule && myEBDType != "ConnectionCheck") { //验签失败 CurrencyTarBack(ebdb, "4"); return; } Console.WriteLine("要解析文件:" + sAnalysisFileName); #region 通用反馈 20181210 CurrencyTarBack(ebdb, "1"); #endregion if (ebdb != null) { switch (ebdb.EBDType) { case "ConnectionCheck": //要提示系统在线 ServerForm.mainFrm.serverFrm.OnlineCheck(true); break; } } } catch (Exception ep) { Log.Instance.LogWrite("验签过程异常:" + ep.Message); } } }