Esempio n. 1
0
        private TransResult PackJson()
        {
            var ret = TransResult.E_PACKET_FAIL;

            SendObject = PackSendObject();
            if (SendObject == null)
            {
                return(ret);
            }
            try
            {
                string strJson;
                BaseCommunicateSendJson send = SendObject as BaseCommunicateSendJson;
                if (send != null) //对象必须是继承基类型的才采用json发送
                {
                    send.rowCount = send.DS.Length;
                    strJson       = JsonConvert.SerializeObject(SendObject);
                }
                else
                {
                    strJson = SendObject.ToString();
                }
                var b_SendJson = Encoding.Default.GetBytes(strJson);
                var head       = PackHead(strJson.Length);
                headLen     = head.Length;
                sendstr_all = new byte[b_SendJson.Length + headLen];

                Array.Copy(head, sendstr_all, head.Length);
                Array.Copy(b_SendJson, 0, sendstr_all, head.Length, b_SendJson.Length);
                ret = TransResult.E_SUCC;
                Log.Debug("PackJson Succ!");
            }
            catch (Exception ex)
            {
                Log.Error("Pack Json Error!", ex);
            }
            return(ret);
        }
Esempio n. 2
0
        private TransResult Pack8583()
        {
            var ret = TransResult.E_PACKET_FAIL;

            try
            {
                var SendBytes = new byte[0];
                PackFix();
                Packet();
                if (SendPackage.IsNull())
                {
                    return(TransResult.E_INVALID);
                }
                var MAC = new byte[8];

                //计算mac并将mac打包进去
                if (NeedCalcMac())
                {
                    byte[] macKey = null;
                    if (EnType == EncryptType.Hardware)
                    {
                        macKey = KeyManager.GetEnMacKey(mSectionName);
                    }
                    else
                    {
                        macKey = KeyManager.GetDeMacKey(mSectionName);
                    }
                    if (macKey == null)
                    {
                        throw new Exception("尚未设置MAC密钥");
                    }
                    if ((DType == DesType.Des && macKey.Length == 16) ||
                        (DType == DesType.TripleDes && macKey.Length == 8))
                    {
                        throw new Exception("MAC密钥长度不符合DES算法");
                    }
                    var macField = 64;
                    if (SendPackage.ExistBit(1))
                    {
                        macField = 128;
                    }
                    SendPackage.SetArrayData(macField, new byte[8]);
                    var tmp      = SendPackage.GetSendBuffer();
                    var macBytes = new byte[tmp.Length - 8];
                    Array.Copy(tmp, macBytes, macBytes.Length);

                    if (CalcMacByMackey(macBytes, MAC))
                    {
                        SendPackage.SetArrayData(macField, MAC);
                    }
                    else
                    {
                        SendPackage.ClearBitAndValue(macField);
                        throw new Exception("计算MAC失败");
                    }
                }
                SendBytes = SendPackage.GetSendBuffer();
                if (SendBytes.Length <= 0)
                {
                    return(ret);
                }
                var head_8583 = PackBytesAtFront(SendBytes.Length);
                headLen_8583 = head_8583.Length;

                #region Json复合8583包打包(子类中重写PackSendObject())方法
                SendObject = PackSendObject();
                string strJson = "";
                BaseCommunicateSendJson send = SendObject as BaseCommunicateSendJson;
                if (send != null) //对象必须是继承基类型的才采用json发送
                {
                    send.rowCount = send.DS.Length;
                    strJson       = JsonConvert.SerializeObject(SendObject);
                }
                #endregion


                //if (!string.IsNullOrEmpty(strJson))
                //{
                byte[] json = Encoding.Default.GetBytes(strJson);
                //}
                var sendLen_all = SendBytes.Length + head_8583.Length + json.Length;

                var t_head = PackHead(sendLen_all);//自定义报文头
                headLen      = t_head.Length;
                sendLen_all += headLen;
                sendstr_all  = new byte[sendLen_all];

                Array.Copy(t_head, sendstr_all, t_head.Length);
                Array.Copy(head_8583, 0, sendstr_all, headLen, head_8583.Length);
                Array.Copy(SendBytes, 0, sendstr_all, head_8583.Length + headLen, SendBytes.Length);
                if (!string.IsNullOrEmpty(strJson))
                {
                    Array.Copy(json, 0, sendstr_all, head_8583.Length + headLen + SendBytes.Length, json.Length);
                }

                ret = TransResult.E_SUCC;
            }
            catch (Exception ex)
            {
                Log.Error(GetType().Name, ex);
            }
            return(ret);
        }
Esempio n. 3
0
        private TransResult PackBankBag()
        {
            var    ret = TransResult.E_PACKET_FAIL;
            string str = PackSendBag();

            if (str == null)
            {
                return(ret);
            }
            try
            {
                int    sendLen  = str.Length;
                byte[] SendData = Encoding.GetEncoding("GBK").GetBytes(str);
                int    len      = (SendData.Length % 8 == 0 ? SendData.Length : SendData.Length + 8 - SendData.Length % 8);
                byte[] cData    = new byte[len];
                for (int i = 0; i < len; i++)
                {
                    cData[i] = (byte)(len - SendData.Length);
                }
                Array.Copy(SendData, cData, SendData.Length);
                byte[] bkey     = Convert.FromBase64String(mainKey);
                byte[] Senddata = Encrypt.DES3Encrypt(cData, bkey);
                string sendstr  = Convert.ToBase64String(Senddata);

                sendLen = sendstr.Length;
                byte[] Baghead = PackBagHead(sendLen);
                byte[] BagData = new byte[Baghead.Length + sendLen];
                byte[] sendbag = Encoding.GetEncoding("GBK").GetBytes(sendstr);

                Array.Copy(Baghead, BagData, Baghead.Length);
                Array.Copy(sendbag, 0, BagData, Baghead.Length, sendLen);

                #region Json复合8583包打包(子类中重写PackSendObject())方法
                SendObject = PackSendObject();
                string strJson = "";
                BaseCommunicateSendJson send = SendObject as BaseCommunicateSendJson;
                if (send != null) //对象必须是继承基类型的才采用json发送
                {
                    send.rowCount = send.DS.Length;
                    strJson       = JsonConvert.SerializeObject(SendObject);
                }
                #endregion


                //if (!string.IsNullOrEmpty(strJson))
                //{
                byte[] json = Encoding.GetEncoding("GBK").GetBytes(strJson);
                //}
                byte[] head = PackHead(BagData.Length + json.Length);
                headLen     = head.Length;
                sendstr_all = new byte[headLen + BagData.Length + json.Length];
                Array.Copy(head, sendstr_all, head.Length);
                Array.Copy(BagData, 0, sendstr_all, head.Length, BagData.Length);
                Array.Copy(json, 0, sendstr_all, headLen + BagData.Length, json.Length);

                ret = TransResult.E_SUCC;
            }
            catch (Exception ex)
            {
                ret = TransResult.E_PACKET_FAIL;
                Log.Error(GetType().Name, ex);
            }
            return(ret);
        }