Exemple #1
0
    /// <summary>
    /// Demonstrates how to retrieve information from a key.
    /// </summary>
    public void SessionInfoDemo(Hasp hasp)
    {
        // sanity check
        if ((null == hasp) || !hasp.IsLoggedIn())
        {
            return;
        }

        Verbose("Get Session Information Demo");

        Verbose("Retrieving Key Information");

        // firstly we will retrieve the key info.
        string     info   = null;
        HaspStatus status = hasp.GetSessionInfo(Hasp.KeyInfo,
                                                ref info);

        ReportStatus(status);
        if (HaspStatus.StatusOk == status)
        {
            Verbose("Key Information:");
            Verbose(info.Replace("\n", "\r\n          "));
        }
        else
        {
            Verbose("");
        }

        Verbose("Retrieving Session Information");

        // next the session info.
        status = hasp.GetSessionInfo(Hasp.SessionInfo, ref info);
        ReportStatus(status);
        if (HaspStatus.StatusOk == status)
        {
            Verbose("Session Information:");
            Verbose(info.ToString());
        }
        else
        {
            Verbose("");
        }

        Verbose("Retrieving Update Information");

        // last the update information.
        status = hasp.GetSessionInfo(Hasp.UpdateInfo, ref info);
        ReportStatus(status);
        if (HaspStatus.StatusOk == status)
        {
            Verbose("Update Information:");
            Verbose(info.ToString());
        }
        else
        {
            Verbose("");
        }
    }
Exemple #2
0
    /// <summary>
    /// Demonstrates how to login using a feature id.
    /// </summary>
    public Hasp LoginDemo(HaspFeature feature)
    {
        //Verbose("Login Demo with Feature: " +
        //        feature.FeatureId.ToString() +
        //        (feature.IsProgNum ? " (Program Number)" : ""));

        // create a key object using a feature
        // and perform a login using the vendor code.
        Hasp hasp = new Hasp(feature);

        HaspStatus status = hasp.Login(VendorCode.Code, scope);

        ReportStatus(status);

        //Verbose("");

        return(hasp.IsLoggedIn() ? hasp : null);
    }
Exemple #3
0
    /// <summary>
    /// Performs a logout operation on the key.
    /// </summary>
    public void LogoutDemo(ref Hasp hasp)
    {
        // sanity check
        if ((null == hasp) || !hasp.IsLoggedIn())
        {
            return;
        }

        Verbose("Logout Demo");

        HaspStatus status = hasp.Logout();

        ReportStatus(status);

        // get rid of the key immediately.
        hasp.Dispose();
        hasp = null;
        Verbose("");
    }
Exemple #4
0
    /// <summary>
    /// Demonstrates how to login into a key using the
    /// default feature. The default feature is ALWAYS
    /// available in every key.
    /// </summary>
    public Hasp LoginDefaultDemo()
    {
        Verbose("Login Demo with Default Feature (HaspFeature.Default)");

        HaspFeature feature = HaspFeature.Default;

        Hasp hasp = new Hasp(feature);

        HaspStatus status = hasp.Login(VendorCode.Code, scope);

        ReportStatus(status);
        Verbose("");

        // Please note that there is no need to call
        // a logout function explicitly - although it is
        // recommended. The garbage collector will perform
        // the logout when disposing the object.
        // If you need more control over the logout procedure
        // perform one of the more advanced tasks.
        return(hasp.IsLoggedIn() ? hasp : null);
    }
Exemple #5
0
    /// <summary>
    /// Demonstrates how to get the real time clock of
    /// a key when available.
    /// </summary>
    public void RtcDemo(Hasp hasp)
    {
        // sanity check
        if ((null == hasp) || !hasp.IsLoggedIn())
        {
            return;
        }

        Verbose("Reading the Real Time Clock Demo");

        DateTime   time   = DateTime.Now;
        HaspStatus status = hasp.GetRtc(ref time);

        ReportStatus(status);

        if (HaspStatus.StatusOk == status)
        {
            Verbose("Real Time Clock is " + time.ToString());
        }

        Verbose("");
    }
Exemple #6
0
    //public void Init()
    //{
    //    InitDog();
    //    //InvokeRepeating("CheckOutDog",10,300);
    //}

    //加密狗初始化//
    public void  InitDog()
    {
        if (ioo.gameController.Type == InputType.Mouse)
        {
            return;
        }

        haspDemo = new HaspDemo();
        hasp     = haspDemo.LoginDemo(HaspFeature.FromFeature(FeatureID));

        if (hasp == null || !hasp.IsLoggedIn())
        {
            Application.Quit();
            Debug.Log("登录加密狗失败!!!");
            return;
        }
        //haspDemo.WriteMessageFirst(hasp, HaspFileId.ReadWrite, "");
        device = SystemInfo.deviceUniqueIdentifier;
        //每次开启游戏时随机向加密狗中写入一个数字,防止监听及共享打印机破解加密狗\\
        long index = (long)UnityEngine.Random.Range(1000000000, 99999999999999);

        checkIndex = index.ToString();

        readStrs = haspDemo.ReadToStr(hasp, HaspFileId.ReadWrite);
        if (GetDeviceStr(readStrs) == "")
        {
            writeStrs += device;
        }
        else
        {
            writeStrs += GetDeviceStr(readStrs);
        }
        writeStrs += ",";
        writeStrs += checkIndex;

        haspDemo.WriteMessageFirst(hasp, HaspFileId.ReadWrite, writeStrs);
    }
Exemple #7
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("");
    }
Exemple #8
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("");
    }
Exemple #9
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("");
        }
    }
Exemple #10
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("");
        }
    }
Exemple #11
0
    /// <summary>
    /// Demonstrates the usage of the AES encryption and
    /// decryption methods.
    /// </summary>
    public void EncryptDecryptDemo(Hasp hasp)
    {
        // sanity check
        if ((null == hasp) || !hasp.IsLoggedIn())
        {
            return;
        }

        Verbose("Encrypt/Decrypt Demo");

        // the string to be encryted/decrypted.
        string text = "Sentinel LDK is great";

        Verbose("Encrypting \"" + text + "\"");

        // convert the string into a byte array.
        byte[] data = UTF8Encoding.Default.GetBytes(text);

        // encrypt the data.
        HaspStatus status = hasp.Encrypt(data);

        ReportStatus(status);

        if (HaspStatus.StatusOk == status)
        {
            text = UTF8Encoding.Default.GetString(data);
            Verbose("Encrypted string: \"" + text + "\"");
            Verbose("");
            Verbose("Decrypting \"" + text + "\"");

            // decrypt the data.
            // on success we convert the data back into a
            // human readable string.
            status = hasp.Decrypt(data);
            ReportStatus(status);

            if (HaspStatus.StatusOk == status)
            {
                text = UTF8Encoding.Default.GetString(data);
                Verbose("Decrypted string: \"" + text + "\"");
            }
        }

        Verbose("");

        // Second choice - encrypting a string using the
        // native .net API
        text = "Encrypt/Decrypt String";
        Verbose("Encrypting \"" + text + "\"");

        status = hasp.Encrypt(ref text);
        ReportStatus(status);

        if (HaspStatus.StatusOk == status)
        {
            Verbose("Encrypted string: \"" + text + "\"");

            Verbose("");
            Verbose("Decrypting \"" + text + "\"");

            status = hasp.Decrypt(ref text);
            ReportStatus(status);

            if (HaspStatus.StatusOk == status)
            {
                Verbose("Decrypted string: \"" + text + "\"");
            }
        }

        Verbose("");
    }
Exemple #12
0
    public void  CheckOutDog()
    {
        if (ioo.gameController.Type == InputType.Mouse)
        {
            return;
        }

        hasp = haspDemo.LoginDemo(HaspFeature.FromFeature(FeatureID));
        if (hasp == null || !hasp.IsLoggedIn())
        {
            Application.Quit();
            Debug.Log("登录加密狗失败!!!");
            return;
        }
        readStrs = haspDemo.ReadToStr(hasp, HaspFileId.ReadWrite);
        string devicestr     = GetDeviceStr(readStrs);
        string checkindexStr = GetCheckIndexStr(readStrs);

        //每隔一段时间检验写入的数据是否是游戏一开始的数据//
        // Debug.Log(checkIndex + "  :  " + checkindexStr);
        //Debug.Log(device + "  :  " + devicestr);
        if (checkIndex != checkindexStr)
        {
            Application.Quit();
            Debug.Log("检验失败!!!");
            return;
        }
        if (device != devicestr)
        {
            Application.Quit();
            Debug.Log("检验失败!!!");
            return;
        }
        //DateTime time = DateTime.Now;
        //HaspStatus status = hasp.GetRtc(ref time);
        ////检验加密数据,总共120组,每个月随机其中的40组,3个月一个循环//
        //try
        //{
        //    int index = (UnityEngine.Random.Range(time.Month % 3 * 40, time.Month % 3 * 40 + 40));
        //    //Debug.Log(index+" : "+time.Month);
        //    string checkCode =VendorCode.DeCodeStrs[index];
        //    hasp.Decrypt(ref checkCode);
        //   // Debug.Log(checkCode + "  :  " + VendorCode.EncryptStrs[index]);
        //    if (checkCode != VendorCode.EncryptStrs[index])
        //    {
        //        Debug.Log("检验失败!!!");
        //        Application.Quit();
        //    }
        //}
        //catch
        //{
        //    int index = UnityEngine.Random.Range(0, VendorCode.DeCodeStrs.Length);
        //    string checkCode = VendorCode.DeCodeStrs[index];
        //    hasp.Decrypt(ref checkCode);
        //    Debug.Log(checkCode + "  :  " + VendorCode.EncryptStrs[index]);
        //    if (checkCode != VendorCode.EncryptStrs[index])
        //    {
        //        Debug.Log("检验失败!!!");
        //        Application.Quit();
        //    }
        //}
    }