Ejemplo n.º 1
0
        private bool writeCard(string mac)
        {
            NdefMessage msg = new NdefMessage();
            NdefRecord rec_hs = new NdefRecord();	//Handover(static)
            NdefRecord rec_ac = new NdefRecord();	//rec_hsのペイロードになる
            NdefRecord rec_cr = new NdefRecord();	//Config Record

            byte[] TYPE_HS = { (byte)'H', (byte)'s' };
            byte[] TYPE_AC = { (byte)'a', (byte)'c' };
            byte[] TYPE_CR_BT = {
                                0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f,
                                //a   p     p     l     i     c     a     t     i     o     n     /
                                0x76, 0x6e, 0x64, 0x2e, 0x62, 0x6c, 0x75, 0x65, 0x74, 0x6f, 0x6f, 0x74, 0x68, 0x2e, 0x65, 0x70, 0x2e, 0x6f, 0x6f, 0x62
                                //v   n     d     .     b     l     u     e     t     o     o     t     h     .     e     p     .     o     o     b
                             };

            rec_hs.MB = true;
            rec_hs.ME = false;
            rec_hs.setType(NdefRecord.TNF_TYPE.WKS, TYPE_HS);

            rec_ac.MB = true;
            rec_ac.ME = true;
            rec_ac.setType(NdefRecord.TNF_TYPE.WKS, TYPE_AC);
            rec_ac.Payload = new byte[] { 0x01, 0x01, (byte)'0', 0x00 };
            byte[] rec_ac_byte = rec_ac.getRecord();

            byte[] hs_pl = new byte[1 + rec_ac_byte.Length];
            hs_pl[0] = 0x12;		//version
            Buffer.BlockCopy(rec_ac_byte, 0, hs_pl, 1, rec_ac_byte.Length);
            rec_hs.Payload = hs_pl;

            rec_cr.MB = false;
            rec_cr.ME = true;
            rec_cr.setType(NdefRecord.TNF_TYPE.MIME, TYPE_CR_BT);
            rec_cr.ID = new byte[] { (byte)'0' };

            byte[] mac_byte = new byte[6];
            for(int i = 0; i < 6; i++) {
                mac_byte[i] = Convert.ToByte(mac.Substring((5-i) * 2, 2), 16);
            }

            string localname = "hiro99ma";
            List<TLV> tlvs = new List<TLV> {
                new TLV(0x0d, new byte[] { 0x80, 0x06, 0x04 }),
                new TLV(0x03, new byte[] { 0x01, 0x11 }),
                new TLV(0x09, Encoding.ASCII.GetBytes(localname))
            };
            int tlvs_len = 0;
            foreach(TLV tlv in tlvs) {
                tlvs_len += tlv.getLength();
            }
            byte[] tlvs_byte = new byte[2 + 6 + tlvs_len];
            int pos = 0;

            //OOB LEN
            pos += 2;

            //MAC
            Buffer.BlockCopy(mac_byte, 0, tlvs_byte, pos, mac_byte.Length);
            pos += 6;

            //TLV
            foreach(TLV tlv in tlvs) {
                byte[] tlv_byte = tlv.get();
                Buffer.BlockCopy(tlv_byte, 0, tlvs_byte, pos, tlv_byte.Length);
                pos += tlv_byte.Length;
            }

            //OOB LEN
            ushort oob_len = (ushort)(pos - 2);
            tlvs_byte[0] = (byte)(oob_len & 0xff);
            tlvs_byte[1] = (byte)(oob_len >> 8);

            rec_cr.Payload = tlvs_byte;

            int len2 = rec_hs.getLength() + rec_cr.getLength();

            msg.Add(rec_hs);
            msg.Add(rec_cr);

            byte[] msg_byte = msg.getMessage();

            bool ret = false;

            //ユーザブロック
            int blocks = (msg_byte.Length + nfc.BLOCK_SIZE - 1) / nfc.BLOCK_SIZE;
            for(ushort blk = 0; blk < blocks; blk++) {
                ret = mLite.Write(msg_byte, (ushort)(1 + blk), 16 * blk);
            }

            //MCブロック
            if(ret && (mWriteMc != null)) {
                ret = mLite.Write(mWriteMc, FelicaLite.BLOCK_MC);
            }

            //Type3ヘッダ
            if(ret) {
                ret = writeType3Head(msg_byte.Length);
            }

            return ret;
        }
Ejemplo n.º 2
0
        private bool writeCard(string ssid, string key, int auth, int enc, string mac)
        {
            //Authentication Type:インデックス値→WPS値
            byte auth_val;
            switch(auth) {
            case 0:	//OPEN
                auth_val = 0x01;
                break;
            case 1:	//WPA/PSK
                auth_val = 0x02;
                break;
            case 2:	//SHARED
                auth_val = 0x04;
                break;
            case 3:	//WPA
                auth_val = 0x08;
                break;
            case 4:	//WPA2
                auth_val = 0x10;
                break;
            case 5:	//WPA2/PSK
                auth_val = 0x20;
                break;
            default:
                auth_val = 0x00;
                break;
            }

            //Encryption Type:インデックス値→WPS値
            byte enc_val;
            switch(enc) {
            case 0:	//NONE
                enc_val = 0x01;
                break;
            case 1:	//WEP
                enc_val = 0x02;
                break;
            case 2:	//TKIP
                enc_val = 0x04;
                break;
            case 3:	//AES
                enc_val = 0x08;
                break;
            default:
                enc_val = 0x00;
                break;
            }

            NdefMessage msg = new NdefMessage();
            NdefRecord rec_hs = new NdefRecord();	//Handover(static)
            NdefRecord rec_ac = new NdefRecord();	//rec_hsのペイロードになる
            NdefRecord rec_cr = new NdefRecord();	//Config Record

            byte[] TYPE_HS = { (byte)'H', (byte)'s' };
            byte[] TYPE_AC = { (byte)'a', (byte)'c' };
            byte[] TYPE_CR_WIFI = {
                                0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f,
                                //a   p     p     l     i     c     a     t     i     o     n     /
                                0x76, 0x6e, 0x64, 0x2e, 0x77, 0x66, 0x61, 0x2e, 0x77, 0x73, 0x63,
                                //v   n     d     .     w     f     a     .     w     s     c
                             };

            rec_hs.MB = true;
            rec_hs.ME = false;
            rec_hs.setType(NdefRecord.TNF_TYPE.WKS, TYPE_HS);

            rec_ac.MB = true;
            rec_ac.ME = true;
            rec_ac.setType(NdefRecord.TNF_TYPE.WKS, TYPE_AC);
            rec_ac.Payload = new byte[] { 0x01, 0x01, (byte)'0', 0x00 };
            byte[] rec_ac_byte = rec_ac.getRecord();

            byte[] hs_pl = new byte[1 + rec_ac_byte.Length];
            hs_pl[0] = 0x12;		//version
            Buffer.BlockCopy(rec_ac_byte, 0, hs_pl, 1, rec_ac_byte.Length);
            rec_hs.Payload = hs_pl;

            rec_cr.MB = false;
            rec_cr.ME = true;
            rec_cr.setType(NdefRecord.TNF_TYPE.MIME, TYPE_CR_WIFI);
            rec_cr.ID = new byte[] { (byte)'0' };

            byte[] ssid_byte = System.Text.Encoding.UTF8.GetBytes(ssid);
            byte[] key_byte = System.Text.Encoding.UTF8.GetBytes(key);
            byte[] mac_byte = new byte[6];
            for(int i = 0; i < 6; i++) {
                mac_byte[i] = Convert.ToByte(mac.Substring(i * 2, 2), 16);
            }
            List<TLV> tlv_cred = new List<TLV> {
                new TLV(0x1026, 0x0001, new byte[] { 0x01 }),
                new TLV(0x1045, (ushort)ssid_byte.Length, ssid_byte),
                new TLV(0x1003, 0x0002, new byte[] { 0x00, auth_val }),
                new TLV(0x100f, 0x0002, new byte[] { 0x00, enc_val }),
                new TLV(0x1027, (ushort)key.Length, key_byte),
                new TLV(0x1020, 0x0006, mac_byte)
            };
            int tlv_cred_len = 0;
            foreach(TLV tlv in tlv_cred) {
                tlv_cred_len += tlv.getLength();
            }
            byte[] tlv_cred_byte = new byte[tlv_cred_len];
            int pos = 0;
            foreach(TLV tlv in tlv_cred) {
                byte[] tlv_byte = tlv.get();
                Buffer.BlockCopy(tlv_byte, 0, tlv_cred_byte, pos, tlv_byte.Length);
                pos += tlv_byte.Length;
            }

            List<TLV> tlvs = new List<TLV> {
                new TLV(0x104a, 0x0001, new byte[] { 0x10 }),
                new TLV(0x100e, (ushort)tlv_cred_byte.Length, tlv_cred_byte),
                new TLV(0x1049, 0x0006, new byte[] { 0x00, 0x37, 0x2a, 0x00, 0x01, 0x20 })
            };
            int tlvs_len = 0;
            foreach(TLV tlv in tlvs) {
                tlvs_len += tlv.getLength();
            }
            byte[] tlvs_byte = new byte[tlvs_len];
            pos = 0;
            foreach(TLV tlv in tlvs) {
                byte[] tlv_byte = tlv.get();
                Buffer.BlockCopy(tlv_byte, 0, tlvs_byte, pos, tlv_byte.Length);
                pos += tlv_byte.Length;
            }
            rec_cr.Payload = tlvs_byte;

            int len2 = rec_hs.getLength() + rec_cr.getLength();

            //Nexus7のバグなのか知らないが、113~127byteのNDEFデータは読めない
            if((113 <= len2) && (len2 <= 127)) {
                MessageBox.Show("Nexus7 cannot Read this NDEF data.\nSo padding with Empty NDEF Record");

                //MEはTNF_EMPTYにやらせる
                //msg.Add()の時点でバイトデータに変換しているので、それより前にfalse設定する
                rec_cr.ME = false;
            }

            msg.Add(rec_hs);
            msg.Add(rec_cr);
            if(!rec_cr.ME) {
                //とりあえず15byte足せば、113~127byteの範囲ではなくなるので逃げられると思う
                NdefRecord empty1 = new NdefRecord();	//3byte
                NdefRecord empty2 = new NdefRecord();	//3byte

                empty2.ME = true;

                //3byte x 5 = 15byte
                msg.Add(empty1);
                msg.Add(empty1);
                msg.Add(empty1);
                msg.Add(empty1);
                msg.Add(empty2);
            }

            byte[] msg_byte = msg.getMessage();

            bool ret = false;

            //ユーザブロック
            int blocks = (msg_byte.Length + nfc.BLOCK_SIZE - 1) / nfc.BLOCK_SIZE;
            for(ushort blk = 0; blk < blocks; blk++) {
                ret = mLite.Write(msg_byte, (ushort)(1 + blk), 16 * blk);
            }

            //MCブロック
            if(ret && (mWriteMc != null)) {
                ret = mLite.Write(mWriteMc, FelicaLite.BLOCK_MC);
            }

            //Type3ヘッダ
            if(ret) {
                ret = writeType3Head(msg_byte.Length);
                //ret = writeType3Head(128);
            }

            return ret;
        }