Add Original SDK
This commit is contained in:
874
Dinkey Pro 7.6.1/Samples/C#/Runtime/Visual Studio 2002/Form1.cs
Normal file
874
Dinkey Pro 7.6.1/Samples/C#/Runtime/Visual Studio 2002/Form1.cs
Normal file
@@ -0,0 +1,874 @@
|
||||
// !! We strongly recommend that you read the "readme.txt" file in the sample code folder.
|
||||
// !! It contains important instructions on how to use the sample code
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Forms;
|
||||
using System.Data;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DllTest
|
||||
{
|
||||
/// <summary>
|
||||
/// Summary description for Form1.
|
||||
/// </summary>
|
||||
public class Form1 : System.Windows.Forms.Form
|
||||
{
|
||||
private System.Windows.Forms.Label label1;
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.Container components = null;
|
||||
|
||||
public Form1()
|
||||
{
|
||||
//
|
||||
// Required for Windows Form Designer support
|
||||
//
|
||||
InitializeComponent();
|
||||
|
||||
//
|
||||
// TODO: Add any constructor code after InitializeComponent call
|
||||
//
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
protected override void Dispose( bool disposing )
|
||||
{
|
||||
if( disposing )
|
||||
{
|
||||
if (components != null)
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
}
|
||||
base.Dispose( disposing );
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.label1 = new System.Windows.Forms.Label();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// label1
|
||||
//
|
||||
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
|
||||
this.label1.Location = new System.Drawing.Point(72, 72);
|
||||
this.label1.Name = "label1";
|
||||
this.label1.Size = new System.Drawing.Size(160, 40);
|
||||
this.label1.TabIndex = 0;
|
||||
this.label1.Text = "It Worked!";
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
|
||||
this.ClientSize = new System.Drawing.Size(296, 205);
|
||||
this.Controls.AddRange(new System.Windows.Forms.Control[] {
|
||||
this.label1});
|
||||
this.Name = "Form1";
|
||||
this.Text = "Form1";
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
public const int MY_SDSN = 10101; // !!!! change this value to be the value of your SDSN (demo = 10101)
|
||||
public const string MY_PRODCODE = "DEMO"; // !!!! change this value to match the Product Code in the dongle
|
||||
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
/* The sample code contains 11 functions. You will not need to use all these
|
||||
functions in your code. Just use which ever functions are most appropriate
|
||||
and customise in your own way. The sample code is just a guide. You should
|
||||
implement the protection in a stronger way using the techniques described in
|
||||
the "Increasing your Protection" chapter of the manual.
|
||||
|
||||
ProtCheck // a standard protection check
|
||||
ProtCheckWithAlg // a protection check & executing an Algorithm
|
||||
WriteBytes // a protection check & write data to the dongle
|
||||
ReadBytes // a protection check & read data from the dongle
|
||||
EncryptUserData // a protection check & encrypting data and then decrypting the data
|
||||
// these functions are the same as the functions listed above but encrypting all parameters passed to our API
|
||||
ProtCheckEnc // a standard protection check
|
||||
ProtCheckWithAlgEnc // a protection check & executing an Algorithm
|
||||
WriteBytesEnc // a protection check & write data to the dongle
|
||||
ReadBytesEnc // a protection check & read data from the dongle
|
||||
EncryptUserDataEnc // a protection check & encrypting data and then decrypting the data
|
||||
// this function displays the current network users
|
||||
DisplayNetUsers
|
||||
|
||||
If you are using Dinkey Lite then you can only use 4 functions:
|
||||
ProtCheck, ProtCheckWithAlg, ProtCheckEnc, ProtCheckWithAlgEnc
|
||||
*/
|
||||
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
// call the function(s) of your choice here
|
||||
// in this example I have chosen a standard protection check
|
||||
if (ProtCheck() != 0)
|
||||
return; // check fails - exit program
|
||||
Application.Run(new Form1());
|
||||
}
|
||||
|
||||
// ************************* our 10 functions follow *************************
|
||||
|
||||
// **************************** ProtCheck *********************************
|
||||
// standard protection check
|
||||
public static int ProtCheck()
|
||||
{
|
||||
int ret_code;
|
||||
DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.PROTECTION_CHECK; // standard protection check
|
||||
dris.flags = 0; // no extra flags, but you may want to specify some if you want to start a network user or decrement execs,...
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, null);
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// later in your code you can check other values in the DRIS...
|
||||
if (dris.sdsn != MY_SDSN)
|
||||
{
|
||||
MessageBox.Show("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dris.prodcode != MY_PRODCODE)
|
||||
{
|
||||
MessageBox.Show("Incorrect Product Code! Please modify your source code so that MY_PRODCODE is set to be the Product Code in the dongle.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// later on in your program you can check the return code again
|
||||
if (dris.ret_code != 0)
|
||||
{
|
||||
MessageBox.Show("Dinkey Dongle protection error", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// if you are using a network dongle and you want to list the network users then use this function:
|
||||
// DisplayNetUsers(dris);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// **************************** ProtCheckWithAlg ****************************
|
||||
|
||||
// !!!! You should replace this function with the one generated by the
|
||||
// "generate source code" button in Algorithm tab for DinkeyAdd (if you have specified an algorithm)
|
||||
// or from DinkeyLook if you are using a Dinkey Lite dongle.
|
||||
private static int MyAlgorithm(int a, int b, int c, int d, int e, int f, int g, int h)
|
||||
{
|
||||
return a + b + c + d + e + f + g + h;
|
||||
}
|
||||
|
||||
// NB for this to work you must program at least "user algorithm" into the dongle
|
||||
// NB We have used a very simple algorithm here (in the MyAlgorithm function). You must patch this with the
|
||||
// sample code generated by DinkeyAdd for the algorithm that you are using. For Dinkey Lite copy the sample code from DinkeyLook
|
||||
public static int ProtCheckWithAlg()
|
||||
{
|
||||
int ret_code;
|
||||
DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.EXECUTE_ALGORITHM; // standard protection check & execute algorithm
|
||||
dris.flags = 0; // no extra flags, but you may want to specify some if you want to start a network user or decrement execs,...
|
||||
dris.alg_number = 1; // execute algorithm 1 (you do not need to specify this field if you are only using Lite models).
|
||||
// you should remove these entries if you are using Dinkey Lite so that algorithm arguments are random
|
||||
dris.var_a = 1; // sample values
|
||||
dris.var_b = 2;
|
||||
dris.var_c = 3;
|
||||
dris.var_d = 4;
|
||||
dris.var_e = 5;
|
||||
dris.var_f = 6;
|
||||
dris.var_g = 7;
|
||||
dris.var_h = 8;
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, null);
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// later in your code you can check other values in the DRIS...
|
||||
if (dris.sdsn != MY_SDSN)
|
||||
{
|
||||
MessageBox.Show("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// later on in your program you can check the return code again
|
||||
if (dris.ret_code != 0)
|
||||
{
|
||||
MessageBox.Show("Dinkey Dongle protection error", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// if you want you can check the algorithm answer - however if algorithm is from your code then best to just use the answer in your code
|
||||
// !!!! Make sure you have replaced the MyAlgorithm routine with the algorithm you have specified in DinkeyAdd
|
||||
if (MyAlgorithm(dris.var_a, dris.var_b, dris.var_c, dris.var_d, dris.var_e, dris.var_f, dris.var_g, dris.var_h) != dris.alg_answer)
|
||||
{
|
||||
MessageBox.Show("Dinkey protection error!\nYou have not patched your algorithm in the MyAlgorithm routine", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ************************** WriteBytes ******************************
|
||||
// This writes the Data 0,1,2,3,4,5,6,7,8,9 to the dongle data area at offset 7
|
||||
// In order for this function to work you will need to have a data area in your dongle that is at least 18 bytes long.
|
||||
// if you want to write a string then you need to convert it to a byte array first:
|
||||
// encoding.GetBytes(your_string, 0, your_string.Length, data, 0) where encoding is of ASCIIEncoding type.
|
||||
public static int WriteBytes()
|
||||
{
|
||||
int ret_code;
|
||||
DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
byte[] DataToWrite = {0,1,2,3,4,5,6,7,8,9};
|
||||
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.WRITE_DATA_AREA; // standard protection check & write data to dongle
|
||||
dris.flags = DRIS.USE_FUNCTION_ARGUMENT; // you have to do it like this in C#
|
||||
dris.rw_offset = 7;
|
||||
dris.rw_length = DataToWrite.GetLength(0);
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, DataToWrite);
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// later in your code you can check other values in the DRIS...
|
||||
if (dris.sdsn != MY_SDSN)
|
||||
{
|
||||
MessageBox.Show("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// later on in your program you can check the return code again
|
||||
if (dris.ret_code != 0)
|
||||
{
|
||||
MessageBox.Show("Dinkey Dongle protection error", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ************************ ReadBytes ************************************
|
||||
// This reads 10 bytes of data from offset 7 in the dongle data area
|
||||
// In order for this function to work you will need to have a data area in your dongle that is at least 18 bytes long.
|
||||
// if you want to read a string then you need to convert it from a byte array (via an array of char)
|
||||
// CharArray = encoding.GetChars(data); where CharArray is of type char[] and encoding is of type ASCIIEncoding
|
||||
// your_string = new String(CharArray, 0, your_string_length);
|
||||
public static int ReadBytes()
|
||||
{
|
||||
int ret_code;
|
||||
DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
byte[] DataToRead = new byte[10];
|
||||
string DisplayString;
|
||||
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.READ_DATA_AREA; // standard protection check & read data
|
||||
dris.flags = DRIS.USE_FUNCTION_ARGUMENT; // you have to do it like this in C#
|
||||
dris.rw_offset = 7;
|
||||
dris.rw_length = DataToRead.GetLength(0);
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, DataToRead);
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// later in your code you can check other values in the DRIS...
|
||||
if (dris.sdsn != MY_SDSN)
|
||||
{
|
||||
MessageBox.Show("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// later on in your program you can check the return code again
|
||||
if (dris.ret_code != 0)
|
||||
{
|
||||
MessageBox.Show("Dinkey Dongle protection error", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
DisplayString = "Dinkey Dongle data area, offset 7: " + String.Format("{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2} {7:D2} {8:D2} {9:D2}",
|
||||
DataToRead[0], DataToRead[1], DataToRead[2], DataToRead[3], DataToRead[4], DataToRead[5], DataToRead[6], DataToRead[7], DataToRead[8], DataToRead[9]);
|
||||
MessageBox.Show(DisplayString, "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ************************** EncryptUserData ************************************
|
||||
// This function will do a protection check and ask the dongle to encrypt some data using encryption key 1
|
||||
// It will then do another protection check & decrypt the data
|
||||
// if you want to encrypt of decrypt string then see comment for WriteBytes and ReadBytes
|
||||
public static int EncryptUserData()
|
||||
{
|
||||
int ret_code;
|
||||
DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
byte[] Data = {0,1,2,3,4,5,6,7,8,9};
|
||||
string DisplayString;
|
||||
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.ENCRYPT_USER_DATA; // standard protection check & encrypt data
|
||||
dris.flags = DRIS.USE_FUNCTION_ARGUMENT;
|
||||
dris.rw_length = Data.GetLength(0);
|
||||
dris.data_crypt_key_num = 1;
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, Data);
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// ... could add code to check other elements of the DRIS, e.g. ret_code, sdsn, ...
|
||||
|
||||
// Now decrypt the same data to get the original values
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.DECRYPT_USER_DATA; // standard protection check & read data
|
||||
dris.flags = DRIS.USE_FUNCTION_ARGUMENT; // you have to do it like this in C#
|
||||
dris.rw_length = Data.GetLength(0);
|
||||
dris.data_crypt_key_num = 1;
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, Data);
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// later in your code you can check other values in the DRIS...
|
||||
if (dris.sdsn != MY_SDSN)
|
||||
{
|
||||
MessageBox.Show("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// later on in your program you can check the return code again
|
||||
if (dris.ret_code != 0)
|
||||
{
|
||||
MessageBox.Show("Dinkey Dongle protection error", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
DisplayString = "decrypted data is: " + String.Format("{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2} {7:D2} {8:D2} {9:D2}",
|
||||
Data[0], Data[1], Data[2], Data[3], Data[4], Data[5], Data[6], Data[7], Data[8], Data[9]);
|
||||
MessageBox.Show(DisplayString, "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// !!!!!!!!!!! all the functions listed below encrypt all parameters to our API !!!!!!!!!!!!!!!!!!!!!
|
||||
// In order for them to work properly you need to choose "Encrypt DRIS" and "Encrypt Data passed to API"
|
||||
// in the Extra Security tab of DinkeyAdd. In this example, for Data Encryption I use the r/w algorithm
|
||||
// but the code can be modified easily to use the 3 encryption parameters.
|
||||
|
||||
// !!!! please overwrite this function with the one generated by DinkeyAdd for R/W Algorithm
|
||||
private static int MyRWAlgorithm(int a, int b, int c, int d, int e, int f, int g, int h)
|
||||
{
|
||||
return a ^ b ^ c ^ d ^ e ^ f;
|
||||
}
|
||||
|
||||
|
||||
// !!!! please overwrite this function with the one generated by DinkeyAdd
|
||||
private static void CryptDRIS(DRIS dris)
|
||||
{
|
||||
int i, j, k;
|
||||
byte[] bigseed = new byte[256];
|
||||
byte[] S = new byte[256];
|
||||
byte temp, t;
|
||||
byte[] dris_bytes = new byte[Marshal.SizeOf(dris)];
|
||||
|
||||
dris.DrisToByteArray(dris, dris_bytes); // convert DRIS to byte array so we can encrypt it
|
||||
|
||||
for (i=0; i<256; i+=8)
|
||||
{
|
||||
dris.Set4Bytes(bigseed, i, dris.seed1);
|
||||
dris.Set4Bytes(bigseed, i+4, dris.seed2);
|
||||
}
|
||||
for (i=0; i<256; i++)
|
||||
S[i] = (byte)i;
|
||||
for (i=0, j=0; i<256; i++)
|
||||
{
|
||||
j = (j + S[i] + bigseed[i] + 123) % 256;
|
||||
temp = S[i];
|
||||
S[i] = S[j];
|
||||
S[j] = temp;
|
||||
}
|
||||
for (i=0, j=0, k=16; k<Marshal.SizeOf(dris); k++)
|
||||
{
|
||||
i = (i + 1) % 256;
|
||||
j = (j + S[i] + 212) % 256;
|
||||
temp = S[i];
|
||||
S[i] = S[j];
|
||||
S[j] = temp;
|
||||
t = (byte)(S[i] + S[j] + 97);
|
||||
dris_bytes[k] ^= S[t];
|
||||
}
|
||||
dris.ByteArrayToDris(dris_bytes, dris); // convert it back again
|
||||
return;
|
||||
}
|
||||
|
||||
// !!!! please overwrite this function with the one generated by DinkeyAdd
|
||||
private static void CryptApiData(DRIS dris, byte[] data, int length, int alg_answer)
|
||||
{
|
||||
int i, j, k;
|
||||
byte[] bigseed = new byte[256];
|
||||
byte[] S = new byte[256];
|
||||
byte temp, t;
|
||||
|
||||
for (i=0; i<256; i+=8)
|
||||
{
|
||||
dris.Set4Bytes(bigseed, i, dris.seed1);
|
||||
dris.Set4Bytes(bigseed, i+4, dris.seed2);
|
||||
}
|
||||
for (i=0; i<256; i++)
|
||||
S[i] = (byte)i;
|
||||
for (i=0, j=0; i<256; i++)
|
||||
{
|
||||
j = (j + S[i] + bigseed[i] + (alg_answer & 0xff)) % 256;
|
||||
temp = S[i];
|
||||
S[i] = S[j];
|
||||
S[j] = temp;
|
||||
}
|
||||
for (i=0, j=0, k=0; k<length; k++)
|
||||
{
|
||||
i = (i + 1) % 256;
|
||||
j = (j + S[i] + ((alg_answer >> 8) & 0xff)) % 256;
|
||||
temp = S[i];
|
||||
S[i] = S[j];
|
||||
S[j] = temp;
|
||||
t = (byte)(S[i] + S[j] + ((alg_answer >> 16) & 0xff));
|
||||
data[k] ^= S[t];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// **************************** ProtCheckEnc ******************************
|
||||
// We encrypt the DRIS passed to the API. Patch the CryptDRIS function in this file from source code generated by DinkeyAdd.
|
||||
public static int ProtCheckEnc()
|
||||
{
|
||||
int ret_code;
|
||||
DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.PROTECTION_CHECK; // standard protection check
|
||||
dris.flags = 0; // no extra flags, but you may want to specify some if you want to start a network user or decrement execs,...
|
||||
|
||||
CryptDRIS(dris); // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, null);
|
||||
|
||||
CryptDRIS(dris); // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// later in your code you can check other values in the DRIS...
|
||||
if (dris.sdsn != MY_SDSN)
|
||||
{
|
||||
MessageBox.Show("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dris.prodcode != MY_PRODCODE)
|
||||
{
|
||||
MessageBox.Show("Incorrect Product Code! Please modify your source code so that MY_PRODCODE is set to be the Product Code in the dongle.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// later on in your program you can check the return code again
|
||||
if (dris.ret_code != 0)
|
||||
{
|
||||
MessageBox.Show("Dinkey Dongle protection error", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// **************************** ProtCheckWithAlgEnc *************************
|
||||
// NB for this to work you must program at least "user algorithm" into the dongle
|
||||
// NB We have used a very simple algorithm here (in the MyAlgorithm function). You must patch this with the
|
||||
// sample code generated by DinkeyAdd for the algorithm that you are using. For Dinkey Lite copy source from DinkeyLook.
|
||||
// We encrypt the DRIS passed to the API. Patch the CryptDRIS function in this file from source code generated by DinkeyAdd.
|
||||
public static int ProtCheckWithAlgEnc()
|
||||
{
|
||||
int ret_code;
|
||||
DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.EXECUTE_ALGORITHM; // standard protection check & execute algorithm
|
||||
dris.flags = 0; // no extra flags, but you may want to specify some if you want to start a network user or decrement execs,...
|
||||
dris.alg_number = 1; // execute algorithm 1 (you do not need to specify this field if you are only using Lite models).
|
||||
// you should remove these entries if you are using Dinkey Lite so that algorithm arguments are random
|
||||
dris.var_a = 1; // sample values
|
||||
dris.var_b = 2;
|
||||
dris.var_c = 3;
|
||||
dris.var_d = 4;
|
||||
dris.var_e = 5;
|
||||
dris.var_f = 6;
|
||||
dris.var_g = 7;
|
||||
dris.var_h = 8;
|
||||
|
||||
CryptDRIS(dris); // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, null);
|
||||
|
||||
CryptDRIS(dris); // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// later in your code you can check other values in the DRIS...
|
||||
if (dris.sdsn != MY_SDSN)
|
||||
{
|
||||
MessageBox.Show("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// later on in your program you can check the return code again
|
||||
if (dris.ret_code != 0)
|
||||
{
|
||||
MessageBox.Show("Dinkey Dongle protection error", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// if you want you can check the algorithm answer - however if algorithm is from your code then best to just use the answer in your code
|
||||
// !!!! Make sure you have replaced the MyAlgorithm routine with the algorithm you have specified in DinkeyAdd
|
||||
if (MyAlgorithm(dris.var_a, dris.var_b, dris.var_c, dris.var_d, dris.var_e, dris.var_f, dris.var_g, dris.var_h) != dris.alg_answer)
|
||||
{
|
||||
MessageBox.Show("Dinkey protection error!\nYou have not patched your algorithm in the MyAlgorithm routine", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ************************** WriteBytesEnc ***************************
|
||||
// This writes the Data 0,1,2,3,4,5,6,7,8,9 to the dongle data area at offset 7
|
||||
// In order for this function to work you will need to have a data area in your dongle that is at least 18 bytes long.
|
||||
// We encrypt the DRIS and Data passed to the API. Patch the CryptDRIS and CryptApiData functions in this file from source code generated by DinkeyAdd.
|
||||
// if you want to write strings then see comment for WriteBytes
|
||||
public static int WriteBytesEnc()
|
||||
{
|
||||
int ret_code, alg_ans;
|
||||
DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
byte[] DataToWrite = {0,1,2,3,4,5,6,7,8,9};
|
||||
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.WRITE_DATA_AREA; // standard protection check & write data to dongle
|
||||
dris.flags = DRIS.USE_FUNCTION_ARGUMENT; // you have to do it like this in C#
|
||||
dris.rw_offset = 7;
|
||||
dris.rw_length = DataToWrite.GetLength(0);
|
||||
|
||||
// calculate r/w algorithm answer - NB need to replace MyRWAlgorithm code with code for r/w algorithm
|
||||
alg_ans = MyRWAlgorithm(dris.var_a, dris.var_b, dris.var_c, dris.var_d, dris.var_e, dris.var_f, dris.var_g, dris.var_h);
|
||||
|
||||
// encrypt data we want to write.
|
||||
CryptApiData(dris, DataToWrite, DataToWrite.GetLength(0), alg_ans);
|
||||
|
||||
CryptDRIS(dris); // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, DataToWrite);
|
||||
|
||||
CryptDRIS(dris); // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// later in your code you can check other values in the DRIS...
|
||||
if (dris.sdsn != MY_SDSN)
|
||||
{
|
||||
MessageBox.Show("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// later on in your program you can check the return code again
|
||||
if (dris.ret_code != 0)
|
||||
{
|
||||
MessageBox.Show("Dinkey Dongle protection error", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ************************ ReadBytesEnc *********************************
|
||||
// This reads 10 bytes of data from offset 7 in the dongle data area
|
||||
// In order for this function to work you will need to have a data area in your dongle that is at least 18 bytes long.
|
||||
// We encrypt the DRIS and Data passed to the API. Patch the CryptDRIS and CryptApiData functions in this file from source code generated by DinkeyAdd.
|
||||
// if you want to read strings then see comment for ReadBytes
|
||||
public static int ReadBytesEnc()
|
||||
{
|
||||
int ret_code, alg_ans;
|
||||
DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
byte[] DataRead = new byte[10];
|
||||
string DisplayString;
|
||||
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.READ_DATA_AREA; // standard protection check & read data
|
||||
dris.flags = DRIS.USE_FUNCTION_ARGUMENT; // you have to do it like this in C#
|
||||
dris.rw_offset = 7;
|
||||
dris.rw_length = DataRead.GetLength(0);
|
||||
|
||||
// calculate r/w algorithm answer - NB need to replace MyRWAlgorithm code with code for r/w algorithm
|
||||
alg_ans = MyRWAlgorithm(dris.var_a, dris.var_b, dris.var_c, dris.var_d, dris.var_e, dris.var_f, dris.var_g, dris.var_h);
|
||||
|
||||
CryptDRIS(dris); // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, DataRead);
|
||||
|
||||
CryptDRIS(dris); // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// later in your code you can check other values in the DRIS...
|
||||
if (dris.sdsn != MY_SDSN)
|
||||
{
|
||||
MessageBox.Show("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// later on in your program you can check the return code again
|
||||
if (dris.ret_code != 0)
|
||||
{
|
||||
MessageBox.Show("Dinkey Dongle protection error", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// decrypt data that was read.
|
||||
CryptApiData(dris, DataRead, DataRead.GetLength(0), alg_ans);
|
||||
|
||||
DisplayString = "Dinkey Dongle data area, offset 7: " + String.Format("{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2} {7:D2} {8:D2} {9:D2}",
|
||||
DataRead[0], DataRead[1], DataRead[2], DataRead[3], DataRead[4], DataRead[5], DataRead[6], DataRead[7], DataRead[8], DataRead[9]);
|
||||
MessageBox.Show(DisplayString, "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ************************** EncryptUserDataEnc *********************************
|
||||
// This function will do a protection check and ask the dongle to encrypt some data using encryption key 1
|
||||
// It will then do another protection & decrypt the data
|
||||
// We encrypt the DRIS and Data passed to/from the API. Patch the CryptDRIS and CryptApiData functions in this file from source code generated by DinkeyAdd.
|
||||
// if you want to encrypt of decrypt strings then see comment for WriteBytes and ReadBytes
|
||||
public static int EncryptUserDataEnc()
|
||||
{
|
||||
int ret_code, alg_ans;
|
||||
DRIS dris = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
DRIS dris2 = new DRIS(); // initialise the DRIS with random values & set the header
|
||||
byte[] Data = {0,1,2,3,4,5,6,7,8,9};
|
||||
string DisplayString;
|
||||
|
||||
dris.size = Marshal.SizeOf(dris);
|
||||
dris.function = DRIS.ENCRYPT_USER_DATA; // standard protection check & encrypt data
|
||||
dris.flags = DRIS.USE_FUNCTION_ARGUMENT;
|
||||
dris.rw_length = Data.GetLength(0);
|
||||
dris.data_crypt_key_num = 1;
|
||||
|
||||
// calculate r/w algorithm answer - NB need to replace MyRWAlgorithm code with code for r/w algorithm
|
||||
alg_ans = MyRWAlgorithm(dris.var_a, dris.var_b, dris.var_c, dris.var_d, dris.var_e, dris.var_f, dris.var_g, dris.var_h);
|
||||
|
||||
// encrypt data we pass to our API.
|
||||
CryptApiData(dris, Data, Data.GetLength(0), alg_ans);
|
||||
|
||||
CryptDRIS(dris); // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris, Data);
|
||||
|
||||
CryptDRIS(dris); // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// ... could add code to check other elements of the DRIS, e.g. ret_code, sdsn, ...
|
||||
|
||||
// NB use another dris so that we can ensure elements are initialised to different random numbers
|
||||
dris2.size = Marshal.SizeOf(dris2);
|
||||
dris2.function = DRIS.DECRYPT_USER_DATA; // standard protection check & read data
|
||||
dris2.flags = DRIS.USE_FUNCTION_ARGUMENT; // you have to do it like this in C#
|
||||
dris2.rw_length = Data.GetLength(0);
|
||||
dris2.data_crypt_key_num = 1;
|
||||
|
||||
CryptDRIS(dris2); // encrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
ret_code = DinkeyPro.DDProtCheck(dris2, Data);
|
||||
|
||||
CryptDRIS(dris2); // decrypt DRIS (!!!!you should separate from DDProtCheck for greater security)
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
DisplayError(ret_code, dris2.ext_err);
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// later in your code you can check other values in the DRIS...
|
||||
if (dris2.sdsn != MY_SDSN)
|
||||
{
|
||||
MessageBox.Show("Incorrect SDSN! Please modify your source code so that MY_SDSN is set to be your SDSN.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// later on in your program you can check the return code again
|
||||
if (dris2.ret_code != 0)
|
||||
{
|
||||
MessageBox.Show("Dinkey Dongle protection error", "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
alg_ans = MyRWAlgorithm(dris2.var_a, dris2.var_b, dris2.var_c, dris2.var_d, dris2.var_e, dris2.var_f, dris2.var_g, dris2.var_h);
|
||||
// decrypt data that was passed to us by the API.
|
||||
CryptApiData(dris2, Data, Data.GetLength(0), alg_ans);
|
||||
|
||||
DisplayString = "decrypted data is: " + String.Format("{0:D2} {1:D2} {2:D2} {3:D2} {4:D2} {5:D2} {6:D2} {7:D2} {8:D2} {9:D2}",
|
||||
Data[0], Data[1], Data[2], Data[3], Data[4], Data[5], Data[6], Data[7], Data[8], Data[9]);
|
||||
MessageBox.Show(DisplayString, "DllSample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// *********************** DisplayNetUsers *******************************
|
||||
// This function displays the current network users (for DinkeyNet only).
|
||||
// The DDProtCheck function must be called before this function is called.
|
||||
// (Normally you would use DinkeyServer to view the network users but this enables you to do it from the client if you want)
|
||||
public static int DisplayNetUsers(DRIS dris)
|
||||
{
|
||||
string DisplayString;
|
||||
int num_net_users, extended_error, ret_code, i, max_net_users;
|
||||
NU_INFO[] nu_info;
|
||||
|
||||
if (dris.net_users == -1)
|
||||
max_net_users = 100; // if unlimited network users are allowed then display up to 100 users (for example)
|
||||
else
|
||||
max_net_users = dris.net_users;
|
||||
|
||||
if (max_net_users == 0) // no network users are allowed, so nothing to do!
|
||||
return 0;
|
||||
|
||||
if (dris.model < 3) {
|
||||
MessageBox.Show("A network dongle has not been detected. Therefore you cannot display the network users.", "Net Users", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret_code = DinkeyPro.DDGetNetUserList(null, out num_net_users, out nu_info, max_net_users, out extended_error);
|
||||
|
||||
if (ret_code != 0)
|
||||
{
|
||||
MessageBox.Show(String.Format("Error {0}/{1} finding network user information.", ret_code, extended_error), "Net Users", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
}
|
||||
else
|
||||
{
|
||||
DisplayString = String.Format("number of net users: {0}", num_net_users) + Environment.NewLine;
|
||||
for (i=0; i<num_net_users; i++)
|
||||
{
|
||||
DisplayString += String.Format("{0}. licence: {1}, user: {2}, computer: {3}, ip address: {4}", i+1,
|
||||
nu_info[i].licenceName, nu_info[i].userName, nu_info[i].computerName, nu_info[i].ipAddress);
|
||||
DisplayString += Environment.NewLine;
|
||||
}
|
||||
MessageBox.Show(DisplayString, "Net Users", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// to display common errors that occur - !!!! you will want to change these messages
|
||||
public static void DisplayError(int ret_code, int ext_err)
|
||||
{
|
||||
string DisplayString;
|
||||
|
||||
switch(ret_code)
|
||||
{
|
||||
case 401:
|
||||
MessageBox.Show("Error! No dongles detected!", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
case 403:
|
||||
MessageBox.Show("Error! A dongle was detected but it was a different type to the one specified in DinkeyAdd.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
case 404:
|
||||
MessageBox.Show("Error! A dongle was detected but it was a different model to those specified in DinkeyAdd.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
case 409:
|
||||
MessageBox.Show("Error! The dongle detected has not been programmed by DinkeyAdd.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
case 410:
|
||||
MessageBox.Show("Error! A dongle was detected but it has a different Product Code to the one specified in DinkeyAdd.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
case 411:
|
||||
MessageBox.Show("Error! The dongle detected does not contain the licence associated with this program.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
case 413:
|
||||
MessageBox.Show("Error! This program has not been protected by DinkeyAdd. For guidance please read the DinkeyAdd chapter of the Dinkey manual.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
case 417:
|
||||
MessageBox.Show("Error! One or more of the parameters set in the DRIS is incorrect.\nIt could also be caused if you are encrypting the DRIS in your code but did not specify DRIS encryption in DinkeyAdd - or vice versa.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
case 423:
|
||||
MessageBox.Show("Error! The number of network users has been exceeded.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
case 435:
|
||||
MessageBox.Show("Error! DinkeyServer has not been detected on the network.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
case 922:
|
||||
MessageBox.Show("Error! The Software Key has expired.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
// internal error - cannot load DLL
|
||||
case -1:
|
||||
MessageBox.Show("Error! Cannot call protection check because DLL is not loaded.", "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
|
||||
default:
|
||||
DisplayString = "An error occurred checking the dongle. Error: " + String.Format("{0:D}", ret_code) + "\nExtended Error: " + String.Format("{0:D}", ext_err);
|
||||
MessageBox.Show(DisplayString, "Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user