Exemplo n.º 1
0
        /// <summary>
        /// 获取加密狗中的支持的喷头类型
        /// </summary>
        /// <param name="infos"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public static bool GetCateInfo(out byte[] infos, int index)
        {
            infos = null;
            try
            {
                IO_PACKAGE stDataPkgIn  = new IO_PACKAGE();
                IO_PACKAGE stDataPkgOut = new IO_PACKAGE();
                stDataPkgIn.tag  = (byte)SubFunction_ID.GetProductCat;
                stDataPkgIn.len  = IO_PACKAGE_BUFF_SIZE;
                stDataPkgIn.buff = new byte[IO_PACKAGE_BUFF_SIZE];

                stDataPkgOut.len  = IO_PACKAGE_BUFF_SIZE;
                stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

                BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut, index);
                if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
                {
                    //MessageBox.Show("GetCateInfo failed!");
                    return(false);
                }

                infos = new byte[stDataPkgOut.len];
                Buffer.BlockCopy(stDataPkgOut.buff, 0, infos, 0, infos.Length);

                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 取得当前的软件狗密码
        /// </summary>
        /// <returns>当前的软件狗密码</returns>
        public static BYHX_SL_RetValue GetPassWord(ref string strkey)
        {
            IO_PACKAGE stDataPkgIn  = new IO_PACKAGE();
            IO_PACKAGE stDataPkgOut = new IO_PACKAGE();

            stDataPkgIn.tag  = (byte)SubFunction_ID.GetPassWord;
            stDataPkgIn.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgIn.buff = new byte[IO_PACKAGE_BUFF_SIZE];

            stDataPkgOut.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

            BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut);

#if DEBUG
            if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
            {
                MessageBox.Show("Get ValidDate failed!");
            }
#endif
            byte[] dt = new byte[PASSWORDLEN];
            Buffer.BlockCopy(stDataPkgOut.buff, 0, dt, 0, dt.Length);
            strkey = Encoding.ASCII.GetString(dt);
            //for (int i = 0; i < dt.Length; i++)
            //{
            //    strkey += dt[i].ToString("X").PadLeft(2, '0');
            //}
            return(ret);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 打印时检查是否过期,并处理打印数据
        /// </summary>
        /// <param name="jobheader">加密后的值</param>
        /// <returns>true=调用成功/false=调用失败</returns>
        public static BYHX_SL_RetValue CheckValidDateWithData(byte[] jobheader, ref byte[] retValue)
        {
            byte[] inputbuff = new byte[jobheader.Length];
            Buffer.BlockCopy(jobheader, 0, inputbuff, 0, jobheader.Length);

            IO_PACKAGE stDataPkgIn  = new IO_PACKAGE();
            IO_PACKAGE stDataPkgOut = new IO_PACKAGE();

            stDataPkgIn.tag  = (byte)SubFunction_ID.ValidDateAndDataCheck;
            stDataPkgIn.len  = (byte)inputbuff.Length;
            stDataPkgIn.buff = inputbuff;

            stDataPkgOut.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

            BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut);

#if DEBUG
            if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
            {
                MessageBox.Show("CheckValidDateWithData Run failed!");
            }
#endif
            Buffer.BlockCopy(stDataPkgOut.buff, 0, retValue, 0, stDataPkgOut.len);
            return(ret);
        }
Exemplo n.º 4
0
        //        /// <summary>
        //        /// 用TDES算法解密数据
        //        /// </summary>
        //        /// <param name="input">密文</param>
        //        /// <param name="output">明文</param>
        //        /// <returns>成功与否</returns>
        //        public static BYHX_SL_RetValue Decrypt_TDES(byte[] input, ref byte[] output)
        //        {
        //            IO_PACKAGE stDataPkgIn = new IO_PACKAGE();
        //            IO_PACKAGE stDataPkgOut = new IO_PACKAGE();
        //            stDataPkgIn.tag = (byte)SubFunction_ID.Decrypt_TDES;
        //            stDataPkgIn.len = IO_PACKAGE_BUFF_SIZE;
        //            stDataPkgIn.buff = new byte[IO_PACKAGE_BUFF_SIZE];

        //            stDataPkgOut.len = IO_PACKAGE_BUFF_SIZE;
        //            stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

        //            Buffer.BlockCopy(input, 0, stDataPkgIn.buff, 0, input.Length);
        //            BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut);
        //#if DEBUG
        //            if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
        //            {
        //                MessageBox.Show("CheckValidDateWithData Run failed!");
        //            }
        //#endif
        //            Buffer.BlockCopy(stDataPkgOut.buff, 0, output, 0, stDataPkgOut.len);
        //            return ret;
        //        }
        //        /// <summary>
        //        /// 用TDES算法加密数据
        //        /// </summary>
        //        /// <param name="input">明文</param>
        //        /// <param name="output">密文</param>
        //        /// <returns>成功与否</returns>
        //        public static BYHX_SL_RetValue Encrypt_TDES(byte[] input, ref byte[] output)
        //        {
        //            IO_PACKAGE stDataPkgIn = new IO_PACKAGE();
        //            IO_PACKAGE stDataPkgOut = new IO_PACKAGE();
        //            stDataPkgIn.tag = (byte)SubFunction_ID.Encrypt_TDES;
        //            stDataPkgIn.len = (byte)input.Length;
        //            stDataPkgIn.buff = input;

        //            stDataPkgOut.len = IO_PACKAGE_BUFF_SIZE;
        //            stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

        //            BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut);
        //#if DEBUG
        //            if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
        //            {
        //                MessageBox.Show("CheckValidDateWithData Run failed!");
        //            }
        //#endif
        //            Buffer.BlockCopy(stDataPkgOut.buff, 0, output, 0, output.Length);
        //            return ret;
        //        }

        /// <summary>
        /// 运行锁内可执行程序
        /// </summary>
        /// <param name="inbuf">输入</param>
        /// <param name="outbuf">输出</param>
        /// <returns>true=调用成功;false=调用失败</returns>
        private static BYHX_SL_RetValue ExecuteExeById(IO_PACKAGE inbuf, ref IO_PACKAGE outbuf, int index)
        {
            SENSE4_CONTEXT stS4Ctx         = new SENSE4_CONTEXT();
            uint           dwResult        = 0;
            uint           dwBytesReturned = 250;

            dwResult = (uint)Common.OpenS4ByIndex(index, ref stS4Ctx);
            if (dwResult != (uint)BYHX_SL_RetValue.SUCSESS)
            {
                return((BYHX_SL_RetValue)dwResult);
            }

            dwResult = S4_API.S4ChangeDir(ref stS4Ctx, S4_FolderName);            //"\\");
            if (dwResult != S4_API.S4_SUCCESS)
            {
#if DEBUG
                string msg = string.Format("Change directory failed! <error code: {0}>\n", dwResult);
                MessageBox.Show(msg);
#endif
                Common.ResetAndCloseS4(stS4Ctx);
                return(BYHX_SL_RetValue.ILLEGALDOG);
            }

            // Call S4VerifyPin(...) to verify User PIN so as to get the privilege to execute the program in EliteIV.
            dwResult = S4_API.S4VerifyPin(ref stS4Ctx, UserPin, (uint)UserPin.Length, S4_API.S4_USER_PIN);
            if (dwResult != S4_API.S4_SUCCESS)
            {
#if DEBUG
                string msg = string.Format("Verify Pin failed! <error code: {0}>\n", dwResult);
                MessageBox.Show(msg);
#endif
                Common.ResetAndCloseS4(stS4Ctx);
                return(BYHX_SL_RetValue.ILLEGALDOG);
            }

            byte[] input  = inbuf.ToByteArray();
            byte[] output = outbuf.ToByteArray();
            dwBytesReturned = outbuf.len;
            dwResult        = S4_API.S4Execute(ref stS4Ctx, S4_EXE_FILE_ID, input,
                                               IO_PACKAGE_HEADER_SIZE + (uint)inbuf.len,
                                               output, (uint)outbuf.len, ref dwBytesReturned);
            if (dwResult != S4_API.S4_SUCCESS)
            {
#if DEBUG
                MessageBox.Show("Call S4Execute failed!\n");
#endif
                return(BYHX_SL_RetValue.ILLEGALDOG);
            }
            else
            {
                outbuf.tag = output[0];
                outbuf.len = output[1];
                Buffer.BlockCopy(output, IO_PACKAGE_HEADER_SIZE, outbuf.buff, 0, IO_PACKAGE_BUFF_SIZE);
                //MessageBox.Show("Success!\n");
            }
            /* for better security,use the following instead of using S4close() directly */
            Common.ResetAndCloseS4(stS4Ctx);
            return(BYHX_SL_RetValue.SUCSESS);
        }
Exemplo n.º 5
0
 private static byte[] DecryptLeftTimeFromOutput(IO_PACKAGE stDataPkgOut)
 {
     byte[] toint = new byte[stDataPkgOut.len - 1];
     Buffer.BlockCopy(stDataPkgOut.buff, 1, toint, 0, toint.Length);
     for (int i = 0; i < toint.Length; i++)
     {
         toint[i] ^= (byte)stDataPkgOut.buff[0];
     }
     return(toint);
 }
Exemplo n.º 6
0
 private static byte[] DecryptNumFromOutput(IO_PACKAGE stDataPkgOut)
 {
     byte[] toint = new byte[stDataPkgOut.len];
     Buffer.BlockCopy(stDataPkgOut.buff, 0, toint, 0, toint.Length);
     for (int i = 0; i < toint.Length; i++)
     {
         toint[i] ^= (byte)(DateTime.Now.Year - 1900);
     }
     return(toint);
 }
Exemplo n.º 7
0
        /// <summary>
        /// 识别母狗 0:母狗 1:子狗 2:非法狗
        /// </summary>
        /// <param name="index"></param>
        /// <param name="UserPin"></param>
        /// <returns>0:母狗 1:子狗 2:非法狗</returns>
        public static byte RecognizeMasterKey(int index, byte[] UserPin)
        {
            try
            {
                IO_PACKAGE stDataPkgIn  = new IO_PACKAGE();
                IO_PACKAGE stDataPkgOut = new IO_PACKAGE();
                stDataPkgIn.tag  = (byte)SubFunction_ID.IsMasterDog;
                stDataPkgIn.len  = IO_PACKAGE_BUFF_SIZE;
                stDataPkgIn.buff = new byte[IO_PACKAGE_BUFF_SIZE];

                stDataPkgOut.len  = IO_PACKAGE_BUFF_SIZE;
                stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

                BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut, index);
                if (ret != BYHX_SL_RetValue.SUCSESS)
                {
                    return(2);
                }
                if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
                {
                    return(2);
                }
                //byte[] dt = new byte[Marshal.SizeOf(typeof(RTC_TIME_T))];
                //Buffer.BlockCopy(stDataPkgOut.buff, 0, dt, 0, dt.Length);
                //RTC_TIME_T rett = new RTC_TIME_T(dt);
                byte[] infos = new byte[stDataPkgOut.len];
                Buffer.BlockCopy(stDataPkgOut.buff, 0, infos, 0, infos.Length);
                if (infos.Length > 0)
                {
                    if (infos[0] == 1)
                    {
                        return(0);
                    }
                    else
                    {
                        return(1);
                    }
                }
                else
                {
                    return(2);
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBox.Show(ex.Message);
#endif
                return(2);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 检查是否过期
        /// </summary>
        /// <returns>false=已过期;true=未过期</returns>
        public static BYHX_SL_RetValue CheckValidDate(ref int leftT)
        {
            IO_PACKAGE stDataPkgIn  = new IO_PACKAGE();
            IO_PACKAGE stDataPkgOut = new IO_PACKAGE();

            stDataPkgIn.tag  = (byte)SubFunction_ID.ValidDateCheck;
            stDataPkgIn.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgIn.buff = new byte[IO_PACKAGE_BUFF_SIZE];

            stDataPkgOut.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];
            BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut);

            if (ret == BYHX_SL_RetValue.SUCSESS)
            {
                if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
                {
                    if (stDataPkgOut.tag == 0x86)                   //#define ErrorEnum_IllegalPWD 0x86
                    {
                        return(BYHX_SL_RetValue.EXPIRED);
                    }
                    else
                    {
                        return(BYHX_SL_RetValue.ILLEGALDOG);
                    }
                }
                else
                {
                    byte[] toint = new byte[stDataPkgOut.len];
                    Buffer.BlockCopy(stDataPkgOut.buff, 0, toint, 0, toint.Length);
                    leftT = BitConverter.ToInt32(toint, 0);
                }
                if (leftT <= 0)
                {
                    return(BYHX_SL_RetValue.EXPIRED);
                }
                else if (leftT < 100 * 60)
                {
                    return(BYHX_SL_RetValue.WILLEXPIREDWORNING_100);
                }
                else
                {
                    return(BYHX_SL_RetValue.SUCSESS);
                }
            }
            return(ret);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 获取加密狗的系统时钟时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetDateTime()
        {
            // 格林尼治时间和北京时间相差8小时
            DateTime retdt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified).AddHours(8);

            try
            {
                IO_PACKAGE stDataPkgIn  = new IO_PACKAGE();
                IO_PACKAGE stDataPkgOut = new IO_PACKAGE();
                stDataPkgIn.tag  = (byte)SubFunction_ID.GetDogTimer;
                stDataPkgIn.len  = IO_PACKAGE_BUFF_SIZE;
                stDataPkgIn.buff = new byte[IO_PACKAGE_BUFF_SIZE];

                stDataPkgOut.len  = IO_PACKAGE_BUFF_SIZE;
                stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

                BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut);
                if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
                {
                    //MessageBox.Show("GetCateInfo failed!");
                    LogWriter.SaveOptionLog("获取加密锁时钟失败!!使用系统时间替代");
                    return(DateTime.Now);// 获取失败则按系统时间
                }
                //加密狗返回的数据字节顺序和pc端是反向的,此处倒序处理
                byte[] buf = new byte[stDataPkgOut.len];
                for (int i = 0; i < buf.Length; i++)
                {
                    buf[i] = stDataPkgOut.buff[buf.Length - 1 - i];
                }
                retdt = retdt.AddSeconds(BitConverter.ToUInt32(buf, 0));
                return(retdt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LogWriter.SaveOptionLog("获取加密锁时钟失败!!使用系统时间替代:" + ex.Message);
                return(DateTime.Now);
            }
        }
Exemplo n.º 10
0
        public static BYHX_SL_RetValue SetProductInfo(byte[] info, int size)
        {
            IO_PACKAGE stDataPkgIn  = new IO_PACKAGE();
            IO_PACKAGE stDataPkgOut = new IO_PACKAGE();

            stDataPkgIn.tag  = (byte)SubFunction_ID.SetProductInfo;
            stDataPkgIn.len  = (byte)size;
            stDataPkgIn.buff = info;

            stDataPkgOut.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

            BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut);

#if DEBUG
            if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
            {
                MessageBox.Show("Set ValidDate failed!");
            }
#endif
            return(ret);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Fun_CMD_GetDongleInfo:DogId 4,  Left time 4 ,Dead time 4,vender 2,lang 1
        /// </summary>
        /// <returns>DogId 4,  Left time 4 ,Dead time 4,vender 2,lang 1</returns>
        public static BYHX_SL_RetValue GetDongleInfo(ref byte[] infos, int index)
        {
            IO_PACKAGE stDataPkgIn  = new IO_PACKAGE();
            IO_PACKAGE stDataPkgOut = new IO_PACKAGE();

            stDataPkgIn.tag  = (byte)SubFunction_ID.GetDongleInfo;
            stDataPkgIn.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgIn.buff = new byte[IO_PACKAGE_BUFF_SIZE];;

            stDataPkgOut.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

            BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut, index);

            if (ret == BYHX_SL_RetValue.SUCSESS)
            {
                if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
                {
#if DEBUG
                    MessageBox.Show("Get ValidDate failed!");
#endif
                    if (stDataPkgOut.tag == 0x86)                   //#define ErrorEnum_IllegalPWD 0x86
                    {
                        return(BYHX_SL_RetValue.EXPIRED);
                    }
                    else
                    {
                        return(BYHX_SL_RetValue.ILLEGALDOG);
                    }
                }

                //byte[] dt = new byte[Marshal.SizeOf(typeof(RTC_TIME_T))];
                //Buffer.BlockCopy(stDataPkgOut.buff, 0, dt, 0, dt.Length);
                //RTC_TIME_T rett = new RTC_TIME_T(dt);
                infos = new byte[stDataPkgOut.len];
                Buffer.BlockCopy(stDataPkgOut.buff, 0, infos, 0, infos.Length);
            }
            return(ret);
        }
Exemplo n.º 12
0
        /// <summary>
        /// 设置新的有效期
        /// </summary>
        /// <param name="mValidDate">新的有效期</param>
        /// <returns>true=设置成功;false=设置失败</returns>
        public static BYHX_SL_RetValue SetFeatureWords(string keyword)
        {
            byte[] keys = Encoding.ASCII.GetBytes(keyword);            //DonglekeyWordToBuffer(keyword);

            IO_PACKAGE stDataPkgIn  = new IO_PACKAGE();
            IO_PACKAGE stDataPkgOut = new IO_PACKAGE();

            stDataPkgIn.tag  = (byte)SubFunction_ID.SetFeatureWords;
            stDataPkgIn.len  = (byte)keys.Length;
            stDataPkgIn.buff = keys;

            stDataPkgOut.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

            BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut);

#if DEBUG
            if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
            {
                MessageBox.Show("Set ValidDate failed!");
            }
#endif
            return(ret);
        }
Exemplo n.º 13
0
        /// <summary>
        /// 设置新的加密数
        /// </summary>
        /// <param name="EncryptNum">新的加密数</param>
        /// <returns>true=设置成功;false=设置失败</returns>
        public static BYHX_SL_RetValue GetFeatureWords(ref byte[] fw)
        {
            IO_PACKAGE stDataPkgIn  = new IO_PACKAGE();
            IO_PACKAGE stDataPkgOut = new IO_PACKAGE();

            stDataPkgIn.tag  = (byte)SubFunction_ID.GetFeatureWords;
            stDataPkgIn.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgIn.buff = new byte[IO_PACKAGE_BUFF_SIZE];

            stDataPkgOut.len  = IO_PACKAGE_BUFF_SIZE;
            stDataPkgOut.buff = new byte[IO_PACKAGE_BUFF_SIZE];

            BYHX_SL_RetValue ret = ExecuteExeById(stDataPkgIn, ref stDataPkgOut);

#if DEBUG
            if (stDataPkgOut.tag != S4_API.S4_SUCCESS)
            {
                MessageBox.Show("write EncryptNum failed!");
            }
#endif
            fw = new byte[16];
            Buffer.BlockCopy(stDataPkgOut.buff, 0, fw, 0, fw.Length);
            return(ret);
        }
Exemplo n.º 14
0
 private static BYHX_SL_RetValue ExecuteExeById(IO_PACKAGE inbuf, ref IO_PACKAGE outbuf)
 {
     return(ExecuteExeById(inbuf, ref outbuf, FIRST_S4_INDEX));
 }