Пример #1
0
        private bool recvNotifyRecvExtKeys(ExtKey key, byte[] msg)
        {
            bool bres = false;

            try
            {
                byte[]  rcvhash = null;
                SMSHash hashid  = (SMSHash)msg[1];
                switch (hashid)
                {
                case SMSHash.MD5:
                    rcvhash = new byte[0x10];
                    break;
                }
                if (rcvhash != null)
                {
                    Array.Copy(msg, 2, rcvhash, 0, rcvhash.Length);
                    bres = SMSCrypt.CheckHash(hashid, key.SyncKey, rcvhash);
                }
                if (bres == true && OnRecvNotifyAcceptecExtKey != null)
                {
                    OnRecvNotifyAcceptecExtKey(this);
                }
            }
            catch
            {
                bres = false;
            }

            return(bres);
        }
Пример #2
0
        public bool PutReciveMessage(byte[] msg, byte[] hash, SMSHash hashid, byte[] ext)
        {
            bool   bRes = false;
            ExtKey key  = getExt(ext);

            if (SMSCrypt.CheckHash(hashid, msg, hash) == true)
            {
                byte [] res;
                string  err;
                err = SMSCrypt.SyncDecode(key.SyncID, msg, key.SyncKey, key.SyncIV, out res);
                if (string.IsNullOrEmpty(err) == true)
                {
                    if (res.Length != 0)
                    {
                        switch (res[0])
                        {
                        case 2:    //Type 2 - уведомление о получении ключей
                            bRes = recvNotifyRecvExtKeys(key, res);

                            break;

                        case 3:    //Type 3 - new messages
                            bRes = recvNewMessage(key, res, hashid, hash);
                            break;

                        case 4:    //Type 4 уведомление о принятых сообщениях .
                            bRes = recvDeliveryNotify(key, res);
                            break;
                        }
                    }
                }
            }
            return(bRes);
        }
Пример #3
0
        public string SendMessage(MORZEMessage msg, IMORZEContact to)
        {
            string err = null;

            if (to.isHasConfirmEtx() == true)
            {
                try
                {
                    ExtKey           ext;
                    byte []          netmsg = to.getMORZENetMessage(msg, out ext);
                    MORZESendMessage m      = new MORZESendMessage();
                    m.AddMessageBody(netmsg, ext.HashID, ext.Ext);
                    m.Send(m_netStream);
                    msg.Status = MORZEMessageStatus.sended;
                    msg.AddHashInfo(ext.HashID, SMSCrypt.CalcHash(ext.HashID, netmsg));
                }
                catch (Exception exp)
                {
                    OnConnectChange(false, exp.Message);
                }
            }
            else
            {
                SendInitialMessage(to);
            }
            return(err);
        }
Пример #4
0
        public byte[] SyncEncrypt(byte[] input)
        {
            byte[] result = null;
            if (string.IsNullOrEmpty(SMSCrypt.SyncEncode(m_alg, input, SyncKey, SyncIV, out result)) == false)
            {
                result = null;
            }

            return(result);
        }
Пример #5
0
        /// <summary>
        /// message send
        /// </summary>
        /// <param name="msg">mesage's plain text</param>
        /// <param name="to">reciver</param>
        /// <returns>error description</returns>
        public string SendMessage(string msg, IMORZEContact to)
        {
            string err = null;

            if (to.isHasConfirmEtx() == false)
            {
                try
                {
                    SendInitialMessage(to);

                    MORZEMessages msgs;
                    msgs = m_account.GetMessages(to);
                    if (msgs != null)
                    {
                        msgs.AddUnsendedNewMessages(msg);
                    }
                }
                catch (Exception exp)
                {
                    OnConnectChange(false, exp.Message);
                }
            }
            else
            {
                ExtKey       ext;
                MORZEMessage mmsg   = new MORZEMessage(msg.ToString());
                byte[]       netmsg = to.getMORZENetMessage(mmsg, out ext);

                MORZESendMessage m = new MORZESendMessage();
                m.AddMessageBody(netmsg, ext.HashID, ext.Ext);

                m.Send(m_netStream);

                MORZEMessages msgs;
                msgs = m_account.GetMessages(to);
                if (msgs != null)
                {
                    mmsg.AddHashInfo(ext.HashID, SMSCrypt.CalcHash(ext.HashID, netmsg));
                    msgs.AddSendedNewMessages(mmsg);
                }
            }
            return(err);
        }
Пример #6
0
        public byte[] getMORZENetMessage(MORZEMessage msg, out ExtKey ext)
        {
            byte[] enetmsg = null;
            ext = null;
            if (m_Exts != null && m_Exts.Any() == true)
            {
                byte[] bmsg   = null;
                byte[] netmsg = null;
                string err;
                ExtKey key = m_Exts[m_Exts.Count - 1];
                bmsg = Encoding.UTF8.GetBytes(msg.ToString());


                netmsg    = new byte[bmsg.Length + 5];
                netmsg[0] = 3; //type of message  - text message

                byte[] num;

                num = BitConverter.GetBytes(msg.GetOrderNumber); //must 2butes
                Array.Copy(num, 0, netmsg, 1, num.Length);

                num = BitConverter.GetBytes(msg.GetResnedCount); //must 2butes
                Array.Copy(num, 0, netmsg, 3, num.Length);

                Array.Copy(bmsg, 0, netmsg, 5, bmsg.Length);


                err = SMSCrypt.SyncEncode(key.SyncID, netmsg, key.SyncKey, key.SyncIV, out enetmsg);
                if (string.IsNullOrEmpty(err) == false)
                {
                    enetmsg = null;
                }
                else
                {
                    ext = key;
                }
            }

            return(enetmsg);
        }
Пример #7
0
        private bool recvNewMessage(ExtKey key, byte[] msg, SMSHash hashid, byte[] hash)
        {
            ushort nummsg = BitConverter.ToUInt16(msg, 1);

            ushort retry = BitConverter.ToUInt16(msg, 3);

            string       text = Encoding.UTF8.GetString(msg, 5, msg.Length - 5);
            MORZEMessage mmsg = new MORZEMessage(text, hashid, hash, nummsg);

            if (OnRecvMessage != null)
            {
                OnRecvMessage(this, mmsg);
            }



            BufferBuilder bb = new BufferBuilder();

            bb.AddByte(4); // Type 4 - notify recived message
            bb.AddByte((byte)hashid);
            bb.AddBytes(hash);


            byte [] netmsg;
            bool    bres = false;
            string  err  = SMSCrypt.SyncEncode(key.SyncID, bb.GetAllBytes(), key.SyncKey, key.SyncIV, out netmsg);

            if (string.IsNullOrEmpty(err) == true)
            {
                if (m_responseMsg == null)
                {
                    m_responseMsg = new List <byte[]>();
                }
                m_responseMsg.Add(netmsg);
                bres = true;
            }

            return(bres);
        }
        bool operateAsyncType1(byte[] data, byte[] tail)
        {
            bool isSuccess = false;
            int  off       = 1;

            byte[]      ext    = null;
            byte[]      sync   = null;
            byte[]      iv     = null;
            SMSHash     hashid = SMSHash.None;
            SMSSyncAlgo syncid = SMSSyncAlgo.None;

            int extlen = 0;
            int synlen = 0;

            switch (data[off])
            {
            case 1:    //MD5
                extlen = 0x10;
                hashid = SMSHash.MD5;

                break;
            }

            if (extlen > 0)
            {
                off++;
                ext = new byte[extlen];
                Array.Copy(data, off, ext, 0, ext.Length);
                off += ext.Length;
            }

            if (hashid != SMSHash.None)
            {
                switch (data[off])
                {
                case 1:    //DES
                    syncid = SMSSyncAlgo.DES;
                    synlen = 8;
                    break;
                }
            }
            if (synlen > 0)
            {
                off++;
                sync = new byte[synlen];
                iv   = new byte[synlen];
                Array.Copy(data, off, sync, 0, sync.Length);
                off += sync.Length;
                Array.Copy(data, off, iv, 0, iv.Length);
                off += iv.Length;
            }
            if (off + 2 == data.Length) // +2  - CRC16
            {
                byte[] contactaddres;
                string cont = null;
                if (string.IsNullOrEmpty(SMSCrypt.SyncDecode(syncid, tail, sync, iv, out contactaddres)) == false)
                {
                    contactaddres = null;
                }
                if (contactaddres != null)
                {
                    try
                    {
                        cont = Encoding.ASCII.GetString(contactaddres);
                        Convert.FromBase64String(cont.Substring(4));
                        isSuccess = m_acc.updateSynKey(hashid, ext, syncid, sync, iv, cont);
                    }
                    catch
                    {
                        isSuccess = false;
                    }
                }
            }
            if (isSuccess == true)
            {
                MORZESendMessage cmdMsgTyp2;

                //---------------
                cmdMsgTyp2 = new MORZESendMessage();

                BufferBuilder bb = new BufferBuilder();
                bb.AddByte(2); // Type 2 - уведомление о получении ключей
                bb.AddByte((byte)hashid);
                bb.AddBytes(SMSCrypt.CalcHash(hashid, sync));

                //byte[] msg = bb.GetAllBytes();
                //bb = new BufferBuilder();
                //bb.AddByte((byte)((int)0x80 ^ (int)hashid));
                //bb.AddBytes(SMSCrypt.CalcHash(hashid, msg));
                //bb.AddBytes(ext);
                //bb.AddBytes(msg);
                //cmdMsgTyp2.WriteBytes(bb.GetAllBytes());
                byte[] res;
                if (string.IsNullOrEmpty(SMSCrypt.SyncEncode(syncid, bb.GetAllBytes(), sync, iv, out res)) == true)
                {//send notify to client
                    cmdMsgTyp2.AddMessageBody(res, hashid, ext);
                    if (m_responses == null)
                    {
                        m_responses = new List <SMSSendCommand>();
                    }

                    //set server filter setting
                    SMSSendExt setext = new SMSSendExt();
                    setext.pushExt(hashid, ext);
                    m_responses.Add(setext);
                    m_responses.Add(cmdMsgTyp2);
                }
                else
                {
                    isSuccess = false;
                }
            }
            return(isSuccess);
        }