public bool WriteMessageFirst(Hasp hasp, HaspFileId fileId, string str) { try { HaspFile file = hasp.GetFile(fileId); file.Write(str); return(true); } catch { return(false); } }
/// <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); }
/// <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(""); }
/// <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(""); }
/// <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(""); } }
public bool AddLicense(License inLicense) { byte[] signaturebytes = null; byte[] clientbytes = null; byte[] licensesbytes = null; signaturebytes = new byte[4]; clientbytes = new byte[2]; licensesbytes = new byte[2]; /* * // test quickly add license in dongle.. * // fill the struct first * MESSAGE_LOG_HEADER_STRUCT2 temp2; * temp2.mClient = Convert.ToUInt16(inLicense.mClient); * temp2.mCRC = inLicense.mCRC; * temp2.mEnd = Convert.ToUInt16(inLicense.mEnd); * temp2.mKey = Convert.ToByte(inLicense.mKey); * temp2.mProduct = Convert.ToUInt16(inLicense.mProduct); * temp2.mStart = Convert.ToUInt16(inLicense.mStart); * * byte[] all = null; * byte[] licensesbytes = null; * licensesbytes = new byte[2]; * all = new byte[12]; * * all=RawSerialize(temp2); * * file.FilePos = 24 + (mLicenseCount*24); * // client id * status = file.Write(all, 0, 12); * * // and put license count on 1 * file.FilePos = 6; * // and the number of licenses * licensesbytes = BitConverter.GetBytes((uint)mLicenseCount+1); * status = file.Write(licensesbytes, 0, 2); */ if (!inLicense.mValid) { MessageBox.Show("This license is not valid.", "Problem", MessageBoxButtons.OK); return(false); } if (!(mKey != 0 && mHaspHandle != -1)) { MessageBox.Show("Insert a valid dongle.", "Problem", MessageBoxButtons.OK); return(false); } if (mClient == 0 || mLicenseCount == 0) { //uint theSignature = 1400456047; signaturebytes = BitConverter.GetBytes(1400456047); //signaturebytes[0] = 0x6f; //signaturebytes[1] = 0x43; //signaturebytes[2] = 0x79; //signaturebytes[3] = 0x53; // mClient = inLicense.mClient; System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); //signaturebytes = encoding.GetBytes(theSignature.ToString()); file.FilePos = 0; status = file.Write(signaturebytes, 0, 4); clientbytes = BitConverter.GetBytes(inLicense.mClient); file.FilePos = 4; status = file.Write(clientbytes, 0, 2); licensesbytes = BitConverter.GetBytes(1); file.FilePos = 6; status = file.Write(licensesbytes, 0, 2); WriteLicense(inLicense, 0, false); return(true); } if (mClient != inLicense.mClient) { MessageBox.Show("This license has been prepared for a different dongle.", "Problem", MessageBoxButtons.OK); return(false); } for (int i = 0; i < mLicenses.Count; i++) { if ((mLicenses[i].mProduct == inLicense.mProduct) && (mLicenses[i].mClient == inLicense.mClient) && (mLicenses[i].mStart == inLicense.mStart) && (mLicenses[i].mEnd == inLicense.mEnd)) { MessageBox.Show("This license already exists on this dongle.", "Problem", MessageBoxButtons.OK); return(false); } } for (int i = 0; i < mLicenses.Count; i++) { if ((mLicenses[i].mProduct == inLicense.mProduct) && (mLicenses[i].mClient == inLicense.mClient)) { // same product and client... if (mLicenses[i].mEnd < inLicense.mEnd) { // write over expired license or update license! WriteLicense(inLicense, i, true); return(true); } else { MessageBox.Show("Newer license already exists on this dongle.", "Problem", MessageBoxButtons.OK); } return(false); /* * mLicenses[i] = inLicense; * hasp_write (mHaspHandle, HASP_FILEID_MAIN, 24+i*12, 12, &inLicense); * // mLicenseCount++; * hasp_write (mHaspHandle, HASP_FILEID_MAIN, 6, 2, &mLicenseCount); * Beep (440, 200); */ } } //if (mLicenseCount < (mSize - 24)/12) { WriteLicense(inLicense, mLicenseCount, false); return(true); //} //MessageBox.Show("There is no room in this dongle for the license.", "Problem", MessageBoxButtons.OK); //return; }