static void ShiftFshKey()
        {
            FlashAddr = 0X2074;
            R0X41     = ReadFshData(R0XC1);

            if (R0X41 != 0)
            {
                for (;;)
                {
                    //			asm("BANKSEL _A.R0XAB");
                    //			asm("RRCF _A.R0XAB, F, B");
                    //			asm("RRCF _A.R0XAC, F, B");
                    //			asm("RRCF _A.R0XAD, F, B");
                    //			asm("RRCF _A.R0XAE, F, B ");
                    ulR0XAB >>= 1;

                    BytesOP.ClrBit(ref ulR0XAB, 7);
                    if (BytesOP.GetBit(ulR0XAB, 27))
                    {
                        BytesOP.SetBit(ref ulR0XAB, 7);
                    }
                    ;

                    //           asm("BANKSEL _A.R0XAF");
                    //		   asm("RRCF _A.R0XAF, F, B");
                    //		   asm("RRCF _A.R0XB0, F, B");
                    //		   asm("RRCF _A.R0XB1, F, B");
                    //		   asm("RRCF _A.R0XB2, F, B");
                    ulR0XAF >>= 1;

                    BytesOP.ClrBit(ref ulR0XAF, 7);
                    if (BytesOP.GetBit(ulR0XAF, 27))
                    {
                        BytesOP.SetBit(ref ulR0XAF, 7);
                    }
                    ;

                    R0X41--;
                    if (R0X41 == 0)
                    {
                        break;
                    }
                }
            }

            R0XBB = 0XAB;
            R0XBC = 0XB3;
            R0X52 = 0X7F;
            R0X53 = 0X3F;

            BytesOP.SetBit(ref R0XCB, 6);
            GetFshKey();
        }
        static void ShiftMixKey()
        {
            byte t;
            int  p0;

            R0XBD = 0XE0;

            for (;;)
            {
                FlashAddr = 0X1F74;
                p0        = R0XBB;
                t         = RAMBUF[p0];

                t = (byte)((t >> 1) | R0XBD);

                R0X52 = ReadFshData(t);

                t = RAMBUF[p0];

                if ((t & 1) == 0)
                {
                    R0X52 = (byte)(R0X52 >> 4);
                }

                R0X52 &= 0XF;
                p0     = R0XBC;

                if (!BytesOP.GetBit(R0XBD, 5))
                {
                    R0XBC++;

                    t   = RAMBUF[p0];
                    t <<= 4;

                    R0X52 |= t;
                }

                RAMBUF[p0] = R0X52;

                R0XBB++;

                t     = R0XBD;
                R0XBD = (byte)(R0XBD - 0X20);

                //if (CARRY==0) break;
                if (t < 0x20)
                {
                    break;
                }
            }
        }
Exemple #3
0
        static public ushort GetVerify_IP(byte[] pBuff, int start, int count)
        {
            uint cksum = 0;

            if (count % 2 != 0)
            {
                cksum += BytesOP.MakeShort(pBuff[count - 1], 0);
            }
            for (int i = 0; i < count - 1; i = i + 2)
            {
                ushort x = BytesOP.MakeShort(pBuff[i], pBuff[i + 1]);
                cksum += x;
            }
            cksum = (cksum >> 16) + (cksum & 0xffff);
            return((ushort)(~cksum));
        }
        public static byte[] Decode(byte[] sin)
        {
            byte[] sout = new byte[8];

            for (int i = 0; i < 8; i++)
            {
                RAMBUF[i + 0x83] = sin[i];
                RAMBUF[i + 0x8b] = sin[i];
            }

            R0XBB = 0X8B;
            R0XBC = 0XA3;
            R0X52 = 0X3F;
            R0X53 = 0X3F;

            BytesOP.ClrBit(ref R0XCB, 6);

            GetFshKey();

            R0XBB = 0X93;
            R0XBC = 0XAB;
            R0X52 = 0X3F;
            R0X53 = 0X3F;

            BytesOP.SetBit(ref R0XCB, 6);

            GetFshKey();

            for (int i = 16; i != 0; i--)     // 16次循环
            {
                MixFshKey();
            }

            R0XBB = 0XA3;
            R0XBC = 0X8B;
            R0X52 = 0X7F;
            R0X53 = 0X3F;

            BytesOP.ClrBit(ref R0XCB, 6);

            GetFshKey();
            for (int i = 0; i < 8; i++)
            {
                sout[i] = RAMBUF[0x8b + i];
            }
            return(sout);
        }
        static byte ReadFshData(byte c)
        {
            int addr;

            addr = FlashAddr + c - 0x1e14;
            int i = addr / 2;
            int j = addr % 2;

            if (j == 0)
            {
                return(BytesOP.GetLowByte(TCodeKey[i]));
            }
            else
            {
                return(BytesOP.GetHighByte(TCodeKey[i]));
            }
        }
        static void GetFshKey()
        {
            byte t;
            int  p0, p1;

            R0XBD = R0X52;
            R0XBE = R0X53;

            for (;;)
            {
                R0X40 = R0XBE;

                R0X40 = (byte)((R0X40 >> 3) & 0X1F);

                t  = (byte)(R0XBC + R0X40);
                p0 = t;

                FlashAddr = 0x1E14;
                if (BytesOP.GetBit(R0XCB, 6))
                {
                    FlashAddr = 0X1EF4;
                }

                R0X41 = ReadFshData(R0XBD);

                if ((R0X41 & 0x80) == 0)
                {
                    R0XC0 = (byte)(R0X41 & 0X7);

                    FlashAddr = 0X1E14;
                    if (BytesOP.GetBit(R0XCB, 6))
                    {
                        FlashAddr = 0X1EF4;
                    }
                    R0X40 = ReadFshData(R0XBD);

                    R0X40 = (byte)((R0X40 >> 3) & 0x1F);                  // 去掉了高三位,即去掉了循环右移影响

                    t  = (byte)(R0XBB + R0X40);
                    p1 = t;

                    FlashAddr = 0X2084;
                    t         = ReadFshData(R0XC0);//(A.R0XC0);
                    t         = (byte)(t & (RAMBUF[p1]));

                    if (t != 0)
                    {
                        t = 0x80;
                    }
                }
                else
                {
                    t = 0x80;
                }

                RAMBUF[p0] = (byte)(((RAMBUF[p0]) >> 1) | t);

                R0XBD--;
                R0XBE--;
                if ((R0XBE & 0X80) != 0)
                {
                    break;
                }
            }
        }
        static void MixFshKey()
        {
            int p0, p1, p2;

            R0XBB = 0XA7;
            R0XBC = 0X9B;
            R0X52 = 0XBF;
            R0X53 = 0X3F;

            BytesOP.ClrBit(ref R0XCB, 6);
            GetFshKey();
            ShiftFshKey();

            R0X53 = 8;
            p0    = 0x9b;
            p1    = 0xb3;

            for (; ;)
            {
                RAMBUF[p0] ^= (RAMBUF[p1]);
                p0++;
                p1++;

                R0X53--;
                if (R0X53 == 0)
                {
                    break;
                }
            }

            R0XBB = 0X9B;
            R0XBC = 0X9B;
            ShiftMixKey();

            R0XBB = 0X9B;
            R0XBC = 0XB3;
            R0X52 = 0XDF;
            R0X53 = 0X1F;

            BytesOP.ClrBit(ref R0XCB, 6);
            GetFshKey();

            R0X53 = 4;
            p0    = 0xa6;
            p1    = 0xaa;
            p2    = 0xb6;

            for (; ;)
            {
                RAMBUF[p2] ^= (RAMBUF[p0]);
                RAMBUF[p0]  = RAMBUF[p1];
                RAMBUF[p1]  = RAMBUF[p2];

                p0--;
                p1--;
                p2--;

                R0X53--;
                if (R0X53 == 0)
                {
                    break;
                }
            }
        }