Пример #1
0
        static public string SyncDecode(SMSSyncAlgo alg, byte[] data, byte[] key, byte[] iv, out byte[] res)
        {
            string err = null;

            res = null;
            try
            {
                switch (alg)
                {
                case SMSSyncAlgo.DES:
                    err = DecDES(data, key, iv, out res);
                    break;

                default:
                    err = "Invalid Symmetric algorithm";
                    break;
                }
            }
            catch (Exception exp)
            {
                res = null;
                err = exp.Message;
            }
            return(err);
        }
Пример #2
0
        public bool updateSynKey(SMSHash hash, byte[] ext, SMSSyncAlgo sync, byte[] key, byte[] iv, string contact)
        {
            IMORZEContact cnt;
            bool          bres = false;

            cnt = m_addressBook.GetContact(contact, true);
            if (cnt != null)
            {
                try
                {
                    bres = cnt.updateSynKey(hash, ext, sync, key, iv);
                    if (bres == true)
                    {
                        if (string.IsNullOrEmpty(m_addressBook.Save()) == true)
                        {
                            bres = true;
                        }
                    }
                }
                catch
                {
                    bres = false;
                }
            }
            return(bres);
        }
Пример #3
0
        public ExtKey(SMSHash hash, byte[] ext, SMSSyncAlgo sync, byte[] key, byte[] iv)
        {
            m_date = DateTime.Now;

            if (sync == SMSSyncAlgo.DES && hash == SMSHash.MD5)
            {
                m_alg   = sync;
                m_halg  = hash;
                m_pk    = key;
                m_pkIV  = iv;
                m_inExt = ext;
            }
            else
            {
                throw new Exception("Invalid arguments");
            }
        }
Пример #4
0
        public ExtKey(RNGCryptoServiceProvider rng, SMSSyncAlgo alg, SMSHash hash)
        {
            m_date = DateTime.Now;

            if (alg == SMSSyncAlgo.DES && hash == SMSHash.MD5)
            {
                m_alg  = alg;
                m_halg = hash;
                m_pk   = new byte[8];
                rng.GetBytes(m_pk);
                m_pkIV = new byte[8];
                rng.GetBytes(m_pkIV);
                m_inExt = new byte[16];
                rng.GetBytes(m_inExt);
            }
            else
            {
                throw new Exception("Invalid arguments");
            }
        }
Пример #5
0
        public bool updateSynKey(SMSHash hash, byte[] ext, SMSSyncAlgo sync, byte[] key, byte[] iv)
        {
            bool bret = false;

            try
            {
                ExtKey extKey = new ExtKey(hash, ext, sync, key, iv);
                if (m_Exts == null)
                {
                    m_Exts = new List <ExtKey>();
                }
                m_Exts.Add(extKey);

                bret = true;
            }
            catch
            {
                bret = false;
            }
            return(bret);
        }
Пример #6
0
        static public string SyncDecode(SMSSyncAlgo alg, byte[] data, byte[] key, byte[] iv, out string text)
        {
            string err = null;

            text = null;
            try
            {
                byte[] res = null;
                err = SyncDecode(alg, data, key, iv, out res);
                if (res != null && string.IsNullOrEmpty(err) == false)
                {
                    text = Encoding.ASCII.GetString(res);
                }
            }
            catch (Exception exp)
            {
                text = null;
                err  = exp.Message;
            }
            return(err);
        }
Пример #7
0
        static public string SyncEncode(SMSSyncAlgo alg, string text, byte[] key, byte[] iv, out byte[] res)
        {
            string err = null;

            res = null;
            try
            {
                byte[] data = Encoding.ASCII.GetBytes(text);
                err = SyncEncode(alg, data, key, iv, out res);
                if (string.IsNullOrEmpty(err) == false)
                {
                    res = null;
                }
            }
            catch (Exception exp)
            {
                res = null;
                err = exp.Message;
            }
            return(err);
        }
        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);
        }