public string ExtractInstrumentName()
        {
            string currentName = null;

            try
            {
                PublicClass.InstrumentSerial = "";
                vendorCode    = m_sTest;
                hasp          = new Hasp(new HaspFeature(10));
                status        = hasp.Login(vendorCode);
                featureStatus = status.ToString();
                Int32    offset = 0;
                Int32    size   = 48;
                byte[]   data   = new byte[size];
                HaspFile file   = hasp.GetFile(HaspFileId.ReadWrite);
                file.FilePos = offset;
                status       = file.Read(data, 0, data.Length);
                currentName  = Encoding.ASCII.GetString(data).TrimEnd(new char[] { '\0' });
                string[] insName = currentName.Split(new string[] { "|" }, StringSplitOptions.None);
                currentName = insName[0];
                PublicClass.InstrumentSerial = insName[1];
            }
            catch (Exception ex)
            {
            }
            return(currentName);
        }
Пример #2
0
 public bool WriteMessageFirst(Hasp hasp, HaspFileId fileId, string str)
 {
     try
     {
         HaspFile file = hasp.GetFile(fileId);
         file.Write(str);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Пример #3
0
    public string ReadToStr(Hasp hasp, HaspFileId fileId)
    {
        string dd = "";

        try
        {
            HaspFile file = hasp.GetFile(fileId);
            file.Read(ref dd);
            return(dd);
        }

        catch
        {
            return(null);
        }
    }
Пример #4
0
        /// <summary>
        /// 打开加密狗
        /// </summary>
        /// <param name="tz">厂商</param>
        /// <param name="pwr">天数</param>
        /// <param name="er">错误代码</param>
        /// <returns></returns>
        public HaspStatus ClassCS(int tz, out int leftDays, out int er)
        {
            leftDays = -1;

            er = -1;

            try
            {
                string pwr = string.Empty;

                scope = defaultScope;

                HaspFeature feature = HaspFeature.FromFeature(tz);

                hasp = new Hasp(feature);

                HaspStatus status = hasp.Login(strVendorCode, scope);

                if (status != HaspStatus.StatusOk)
                {
                    pwr = "0";

                    er = (int)status;

                    return(status);
                }

                DateTime time3 = new DateTime();


                hasp.GetRtc(ref time3);
                int      len   = 10;
                byte[]   bytes = new byte[len];
                HaspFile file  = hasp.GetFile(HaspFileId.ReadWrite);
                status = file.Read(bytes, 0, bytes.Length);
                pwr    = System.Text.Encoding.UTF8.GetString(bytes);
                pwr    = pwr.Replace("\0", "");

                DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                long     lTime   = long.Parse(pwr);

                TimeSpan toNow    = new TimeSpan(lTime);
                DateTime dtResult = dtStart.Add(toNow);

                int dayTime = (int)(Convert.ToInt32(pwr) / 3600 / 24);

                DateTime time4 = dtResult.AddDays(dayTime);

                TimeSpan sp = time4.Subtract(time3);

                leftDays = sp.Days;

                er = (int)status;

                return(status);
            }
            catch (Exception)
            {
                return(HaspStatus.SystemError);
            }
        }
Пример #5
0
        /// <summary>
        /// 返回天数
        /// </summary>
        /// <param name="tz">厂商</param>
        /// <param name="pwr">激活码</param>
        /// <param name="day">天数</param>
        /// <returns></returns>
        public HaspStatus ActivationTime(int tz, string pwr, out int day)
        {
            scope = defaultScope;

            HaspFeature feature = HaspFeature.FromFeature(tz);

            hasp = new Hasp(feature);

            HaspStatus status = hasp.Login(strVendorCode, scope);

            if (pwr.Length != 16)
            {
                status = HaspStatus.TimeError;
                day    = 0;
                return(status);
            }
            if (status != HaspStatus.StatusOk)
            {
                day = 0;
                return(status);
            }

            DateTime time = new DateTime();

            hasp.GetRtc(ref time);

            HaspFile file = hasp.GetFile(HaspFileId.ReadWrite);

            byte[] _bytes = new byte[16];

            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));

            if (pwr.Contains("6216"))
            {
                String[] arr   = new String[5];
                byte[]   bytes = new byte[80];
                file.FilePos = 16;
                status       = file.Read(bytes, 0, bytes.Length);
                string _pwr = System.Text.Encoding.UTF8.GetString(bytes);

                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = _pwr.Substring(i * 16, 16);
                    if (pwr.Equals(arr[i]))
                    {
                        file.FilePos = (16 * i) + 16;
                        Thread.Sleep(10);
                        status = file.Write(_bytes, 0, _bytes.Length);

                        DateTime time3 = time.AddDays(30);

                        long time2 = (long)(time3 - dtStart).TotalSeconds;

                        string _time = time2.ToString();

                        byte[] decoded = System.Text.Encoding.UTF8.GetBytes(_time);

                        file.FilePos = 0;

                        status = file.Write(decoded, 0, decoded.Length);

                        day = 30;

                        return(status);
                    }
                }
            }
            else if (pwr.Contains("6217"))
            {
                byte[] bytes = new byte[80];
                file.FilePos = 96;
                status       = file.Read(bytes, 0, bytes.Length);
                string _pwr = System.Text.Encoding.UTF8.GetString(bytes);


                String[] arr = new String[5];
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = _pwr.Substring(i * 16, 16);

                    if (pwr.Equals(arr[i]))
                    {
                        file.FilePos = (16 * i) + 96;
                        Thread.Sleep(10);
                        status = file.Write(_bytes, 0, _bytes.Length);

                        DateTime time3 = time.AddDays(60);

                        long time2 = (long)(time3 - dtStart).TotalSeconds;

                        string _time = time2.ToString();

                        byte[] decoded = System.Text.Encoding.UTF8.GetBytes(_time);

                        file.FilePos = 0;

                        status = file.Write(decoded, 0, decoded.Length);

                        day = 60;

                        return(status);
                    }
                }
            }
            else if (pwr.Contains("6218"))
            {
                byte[] bytes = new byte[80];
                file.FilePos = 176;
                status       = file.Read(bytes, 0, bytes.Length);

                string _pwr = System.Text.Encoding.UTF8.GetString(bytes);


                String[] arr = new String[8];
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = _pwr.Substring(i * 16, 16);

                    if (pwr.Equals(arr[i]))
                    {
                        file.FilePos = (16 * i) + 176;
                        Thread.Sleep(10);
                        status = file.Write(_bytes, 0, _bytes.Length);

                        DateTime time3 = time.AddDays(90);

                        long time2 = (long)(time3 - dtStart).TotalSeconds;

                        string _time = time2.ToString();

                        byte[] decoded = System.Text.Encoding.UTF8.GetBytes(_time);

                        file.FilePos = 0;
                        status       = file.Write(decoded, 0, decoded.Length);

                        day = 90;

                        return(status);
                    }
                }
            }
            else if (pwr.Contains("6219"))
            {
                byte[] bytes = new byte[16];
                file.FilePos = 256;
                status       = file.Read(bytes, 0, bytes.Length);

                string _pwr = System.Text.Encoding.UTF8.GetString(bytes);


                String[] arr = new String[1];
                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = _pwr.Substring(i * 16, 16);

                    if (pwr.Equals(arr[i]))
                    {
                        DateTime time3 = time.AddDays(999);

                        long time2 = (long)(time3 - dtStart).TotalSeconds;

                        string _time = time2.ToString();

                        byte[] decoded = System.Text.Encoding.UTF8.GetBytes(_time);

                        file.FilePos = 0;
                        status       = file.Write(decoded, 0, decoded.Length);

                        day = 999;

                        return(status);
                    }
                }
            }
            else
            {
                status = HaspStatus.TimeError;
                day    = 0;
                return(status);
            }

            status = HaspStatus.SystemError;
            day    = 0;
            return(status);
        }
Пример #6
0
    /// <summary>
    /// Demonstrates how to read and write to/from a key's
    /// file at a certain file position
    /// </summary>
    public void ReadWritePosDemo(Hasp hasp, HaspFileId fileId)
    {
        // sanity check
        if ((null == hasp) || !hasp.IsLoggedIn())
        {
            return;
        }

        Verbose("GetFileSize/FilePos Demo");

        // firstly get a file object to a key's file.
        HaspFile file = hasp.GetFile(fileId);

        if (!file.IsLoggedIn())
        {
            // Not logged into key - nothing left to do.
            Verbose("Failed to get file object\r\n");
            return;
        }

        Verbose("Reading contents of file: " + file.FileId.ToString());
        Verbose("Retrieving the size of the file");

        // we want to write an int at the end of the file.
        // therefore we are going to
        // - get the file's size
        // - set the object's read and write position to
        //   the appropriate offset.
        int        size   = 0;
        HaspStatus status = file.FileSize(ref size);

        ReportStatus(status);

        if (HaspStatus.StatusOk != status)
        {
            Verbose("");
            return;
        }

        Verbose("Size of the file is: " + size.ToString() + " Bytes");
        Verbose("Setting file position to last int and reading value");

        // set the file pos to the end minus the size of int
        file.FilePos = size - HaspFile.TypeSize(typeof(int));

        // now read what's there
        int aValue = 0;

        status = file.Read(ref aValue);
        ReportStatus(status);

        if (HaspStatus.StatusOk != status)
        {
            Verbose("");
            return;
        }

        Verbose("Writing to file: 0x" + int.MaxValue.ToString("X2"));

        // write some data.
        status = file.Write(int.MaxValue);
        ReportStatus(status);

        if (HaspStatus.StatusOk != status)
        {
            Verbose("");
            return;
        }

        // read back the written value.
        int newValue = 0;

        Verbose("Reading written data");
        status = file.Read(ref newValue);

        ReportStatus(status);
        if (HaspStatus.StatusOk == status)
        {
            Verbose("Data read: 0x" + newValue.ToString("X2"));
        }

        // restore the original data.
        file.Write(aValue);
        Verbose("");
    }
Пример #7
0
    /// <summary>
    /// Demonstrates how to perform read and write
    /// operations on a key's file
    /// </summary>
    public void ReadWriteDemo(Hasp hasp, HaspFileId fileId)
    {
        // sanity check
        if ((null == hasp) || !hasp.IsLoggedIn())
        {
            Debug.Log("llllllllllllllllllllll");
            return;
        }


        Verbose("Read/Write Demo");

        // Get a file object to a key's memory file.
        // please note: the file object is tightly connected
        // to its key object. logging out from a key also
        // invalidates the file object.
        // doing the following will result in an invalid
        // file object:
        // hasp.login(...)
        // HaspFile file = hasp.GetFile();
        // hasp.logout();
        // Debug.Assert(file.IsValid()); will assert
        HaspFile file = hasp.GetFile(fileId);

        if (!file.IsLoggedIn())
        {
            // Not logged into a key - nothing left to do.
            Verbose("Failed to get file object\r\n");
            return;
        }

        Verbose("Reading contents of file: " + file.FileId.ToString());

        Verbose("Retrieving the size of the file");

        // get the file size
        int        size   = 0;
        HaspStatus status = file.FileSize(ref size);

        ReportStatus(status);

        if (HaspStatus.StatusOk != status)
        {
            Verbose("");
            return;
        }

        Verbose("Size of the file is: " + size.ToString() + " Bytes");

        // read the contents of the file into a buffer
        byte[] bytes = new byte[size];

        Verbose("Reading data");
        status = file.Read(bytes, 0, bytes.Length);
        ReportStatus(status);

        if (HaspStatus.StatusOk != status)
        {
            Verbose("");
            return;
        }

        DumpBytes(bytes);

        Verbose("Writing to file");

        // now let's write some data into the file
        byte[] newBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7 };

        status = file.Write(newBytes, 0, newBytes.Length);
        ReportStatus(status);
        if (HaspStatus.StatusOk != status)
        {
            Verbose("");
            return;
        }

        DumpBytes(newBytes);

        // and read them again
        Verbose("Reading written data");
        status = file.Read(newBytes, 0, newBytes.Length);
        ReportStatus(status);
        if (HaspStatus.StatusOk == status)
        {
            DumpBytes(newBytes);
        }

        // restore the original contents
        file.Write(bytes, 0, bytes.Length);
        Verbose("");
    }
Пример #8
0
    public void ReadDynamicMemory(Hasp hasp, int fileId)
    {
        // sanity check
        if ((null == hasp) || !hasp.IsLoggedIn())
        {
            return;
        }
        HaspFile file = hasp.GetFile(fileId);

        if (!file.IsLoggedIn())
        {
            return;
        }

        string     fileIdhex = fileId.ToString("X");
        int        size      = 0;
        HaspStatus status    = file.FileSize(ref size);

        ReportStatus(status);
        if (HaspStatus.InvalidFile == status)
        {
            return;
        }

        if (HaspStatus.StatusOk != status)
        {
            return;
        }


        if (size != 0) // skip if no dynamic memory exist or is of size zero
        {
            if (size > dynamicMemoryBufferSize)
            {
                size = dynamicMemoryBufferSize;
            }

            // read the contents of the file into a buffer
            byte[] bytes = new byte[size];

            status = file.Read(bytes, 0, bytes.Length);

            if (HaspStatus.StatusOk != status)
            {
                return;
            }

            DumpBytes(bytes);

            //Verbose("Writing data");

            //// now let's write some data into the file
            //byte[] newBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7 };

            //status = file.Write(newBytes, 0, newBytes.Length);
            //ReportStatus(status);
            //if (HaspStatus.StatusOk != status)
            //{
            //    Verbose("");
            //    return;
            //}

            //DumpBytes(newBytes);

            //// and read them again
            //Verbose("Reading written data");
            //status = file.Read(newBytes, 0, newBytes.Length);
            //ReportStatus(status);
            //if (HaspStatus.StatusOk == status)
            //    DumpBytes(newBytes);

            //// restore the original contents
            //file.Write(bytes, 0, bytes.Length);
            //Verbose("");
        }
    }
Пример #9
0
    /// <summary>
    /// Demonstrates how to access Dynamic memory
    /// available in Sentinel HL (Driverless configuration)
    /// key. Use the defined Dynamic memory's file id to access it.
    /// </summary>
    public void ReadWriteDynamicMemory(Hasp hasp, int fileId)
    {
        // sanity check
        if ((null == hasp) || !hasp.IsLoggedIn())
        {
            return;
        }

        Verbose("Read/Write Dynamic Memory Demo");

        // Get a file object to a key's memory file.
        // please note: the file object is tightly connected
        // to its key object. logging out from a key also
        // invalidates the file object.
        // doing the following will result in an invalid
        // file object:
        // hasp.login(...)
        // HaspFile file = hasp.GetFile();
        // hasp.logout();
        // Debug.Assert(file.IsValid()); will assert
        HaspFile file = hasp.GetFile(fileId);

        if (!file.IsLoggedIn())
        {
            // Not logged into a key - nothing left to do.
            Verbose("Failed to get file object\r\n");
            return;
        }

        string fileIdhex = fileId.ToString("X");

        Verbose("Reading contents of dynamic memory fileid: 0x" + fileIdhex);


        Verbose("Retrieving the size ");

        // get the file size
        int        size   = 0;
        HaspStatus status = file.FileSize(ref size);

        ReportStatus(status);
        if (HaspStatus.InvalidFile == status)
        {
            // The specified dynamic memory fileid doesn't exists.
            Verbose("dynamic memory fileid 0x" + fileIdhex + "doesn't exists on the key");
            Verbose("");
            return;
        }

        if (HaspStatus.StatusOk != status)
        {
            Verbose("");
            return;
        }

        Verbose("Size of the dynamic memory fileid is: " + size.ToString() + " Bytes");

        if (size != 0)     // skip if no dynamic memory exist or is of size zero
        {
            if (size > dynamicMemoryBufferSize)
            {
                size = dynamicMemoryBufferSize;
            }

            // read the contents of the file into a buffer
            byte[] bytes = new byte[size];

            Verbose("Reading data");
            status = file.Read(bytes, 0, bytes.Length);
            ReportStatus(status);

            if (HaspStatus.StatusOk != status)
            {
                Verbose("");
                return;
            }

            DumpBytes(bytes);

            Verbose("Writing data");

            // now let's write some data into the file
            byte[] newBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7 };

            status = file.Write(newBytes, 0, newBytes.Length);
            ReportStatus(status);
            if (HaspStatus.StatusOk != status)
            {
                Verbose("");
                return;
            }

            DumpBytes(newBytes);

            // and read them again
            Verbose("Reading written data");
            status = file.Read(newBytes, 0, newBytes.Length);
            ReportStatus(status);
            if (HaspStatus.StatusOk == status)
            {
                DumpBytes(newBytes);
            }

            // restore the original contents
            file.Write(bytes, 0, bytes.Length);
            Verbose("");
        }
    }
        private void GetGeneralInfo()
        {
            // date time first
            // get size of


            DateTime hasptime = DateTime.Now;

            status = hasp.GetRtc(ref hasptime);
            if (status == HaspStatus.NoTime)
            {
                hasptime = DateTime.Now;
            }
            // create
            mDate = DaysSince2000FromSystemTime(hasptime);

            /*
             *
             *  if (hasp_hasptime_to_datetime(theTime, &theDay, &theMonth, &theYear, &theHour, &theMinute, &theSecond) == HASP_STATUS_OK)
             * {
             *  SYSTEMTIME theSystemTime;
             *  theSystemTime.wYear = theYear;
             *  theSystemTime.wMonth = theMonth;
             *  theSystemTime.wDay = theDay;
             *  theSystemTime.wHour = 0;
             *  theSystemTime.wMinute = 0;
             *  theSystemTime.wSecond = 0;
             *  theSystemTime.wMilliseconds = 0;
             *  mDate = DaysSince2000FromSystemTime(theSystemTime);
             * }
             */

            byte[] signaturebytes = null;
            byte[] clientbytes    = null;
            byte[] licensesbytes  = null;
            signaturebytes = new byte[4];
            clientbytes    = new byte[2];
            licensesbytes  = new byte[2];
            // read from Memory

            file = hasp.GetFile(HaspFiles.Main);

            uint signature;

            file.FilePos = 0;
            file.Read(signaturebytes, 0, 4);
            signature = BitConverter.ToUInt32(signaturebytes, 0);

            if (signature != 1400456047)
            {
                mLicenseCount = 0;
            }
            else
            {
                file.FilePos = 4;
                // check how many licenses we have
                file.Read(clientbytes, 0, 2);
                //mClient = BitConverter.ToUInt16(clientbytes, 0);

                file.FilePos = 6;
                // and the number of licenses
                file.Read(licensesbytes, 0, 2);
                mLicenseCount = BitConverter.ToUInt16(licensesbytes, 0);
            }
        }