Use C # To develop dongle programs

Source: Internet
Author: User

Some business management software often uses dongle to encrypt the software to prevent piracy. The following two examples describe how to write passwords to dongle and use dongle to design encryption.Program.

When using a Dongle, you need to write or read data to or from the dongle. For example, how can I write or read a password from a dongle? Run this example. After setting the password in the text box, click write to write the password to the dongle. After writing the password, Click Read, you can read the written password and display it in the text box. 13.4.

When purchasing a Dongle, the manufacturer usually comes with a development manual and a CD. The development manual describes how to use and develop dongles. In this example, we use the dongle product of cylinder Information Technology Co., Ltd., which provides a class library not hosted by. Net to read and write data of the dongle. The following describes the Read and Write Functions in the dongle class library.

● Dogwrite Function

This function writes the data pointed to by pdogdata to the dongle, starts from the dogaddr address, and stops at the dogbytes address.

The function declaration is as follows:

[Dllimport ("win32dll. dll", charset = charset. ANSI)]

Public static unsafe extern uint dogwrite (uint idogbytes, uint idogaddr, byte * pdogdata );

The parameters are described as follows.

L idogaddr: the first address in the user zone when performing read/write operations on software dogs. Value Range: 0 ~ 99.

L idogbytes: the length of the byte used to perform read/write operations on software dogs. The value range is 1 ~ 100, and the sum of the value cannot exceed 100.

L pdogdata: pointer type variable. Point to the read/write operation or the transformed data buffer.

L return value: 0 indicates that the operation is successful, and other values are error codes.

● Dogread Function

This function reads data from the storage area starting with the gaddr In the dongle and stores the data in the buffer zone specified by pdogdata. The number of read bytes is idogbytes. Remember, the buffer size should be long enough.

The function declaration is as follows:

[Dllimport ("win32dll. dll", charset = charset. ANSI)]

Public static unsafe extern uint dogread (uint idogbytes, uint idogaddr, byte * pdogdata );

The parameters are described as follows.

L idogaddr: the first address in the user zone when performing read/write operations on software dogs. Value Range: 0 ~ 99.

L idogbytes: the length of the byte used to perform read/write operations on software dogs. The value range is 1 ~ 100, and the sum of the value cannot exceed 100.

L pdogdata: pointer type variable. Point to the read/write operation or the transformed data buffer.

L return value: 0 indicates that the operation is successful, and other values are error codes.

Pay attention to the following points.

Before using this function, you must complete the installation program that comes with the dongle and copy the win32dll. dll file in the installation directory to the system directory. For example:

"\ SafeNet China \ softdog SDK v3.1 \ Win32 \ win32dll \ highdll \ win32dll" under the installation directory in Windows 2003. copy the DLL file to the "C: \ windows \ system32 \" folder.

Implementation Process

(1) create a new project named ex13_03. The default form is form1.

(2) In the form1 form, two button controls are added to execute writing and reading data to and from dongle data, and two textbox controls are added, enter the data written to the dongle and display the data read from the dongle.

(3) Main ProgramCode.

Set dongles and improve the reading and writing functions of dongles. The Code is as follows:

[Structlayout (layoutkind. Sequential)]

// This class is used to read and write dongle

Public unsafe class dog

{

Public uint dogbytes and dogaddr; // set the length and start address of the dongle.

Public byte [] dogdata; // set the Data Length

Public uint retcode;

[Dllimport ("win32dll. dll", charset = charset. ANSI)]

Public static unsafe extern uint dogread (uint idogbytes, uint idogaddr, byte * pdogdata );

[Dllimport ("win32dll. dll", charset = charset. ANSI)]

Public static unsafe extern uint dogwrite (uint idogbytes, uint idogaddr, byte * pdogdata );

Public unsafe dog (ushort num)

{

Dogbytes = num;

Dogdata = new byte [dogbytes]; // set the Data Length

}

Public unsafe void readdog ()

{

Fixed (byte * pdogdata = & dogdata [0])

{

Retcode = dogread (dogbytes, dogaddr, pdogdata); // read data from dongle

}

}

Public unsafe void writedog ()

{

Fixed (byte * pdogdata = & dogdata [0])

{

Retcode = dogwrite (dogbytes, dogaddr, pdogdata); // write data to dongle

}

}

}

Call the dongle class to read and write the dongle. The Code is as follows:

Private void button1_click_1 (Object sender, eventargs E)

{

Dog dog = new dog (100 );

Dog. dogaddr = 0;

Dog. dogbytes = 10;

String STR = textbox1.text;

For (INT I = 0; I <Str. length; I ++)

{

Dog. dogdata [I] = (byte) STR [I];

}

Dog. writedog ();

MessageBox. Show ("the password has been successfully written into the dongle! "," Success prompt! ", Messageboxbuttons. OK, messageboxicon. information );

Textbox1.readonly = true;

Button1.enabled = false;

Button2.enabled = true;

}

Private void button2_click_1 (Object sender, eventargs E)

{

Dog dog = new dog (100 );

Dog. dogaddr = 0;

Dog. dogbytes = 10;

Dog. readdog ();

If (dog. retcode = 0) // start to read dongle data

{

Char [] chtemp = new char [textbox1.text. Length];

For (INT I = 0; I <textbox1.text. length; I ++)

{

Chtemp [I] = (char) dog. dogdata [I];

}

String STR = new string (chtemp );

Textbox2.text = STR;

}

Else

{

Textbox2.text = "2:" + dog. retcode;

}

Textbox1.readonly = false;

Button2.enabled = false;

Button1.enabled = true;

}

Note: The code used in this program is unsafe and normal compilation fails. Therefore, you must set the development environment to allow Insecure code to run. The procedure is as follows: select the "project", "properties", and "generate" sub-menus in the menu bar, and select the "allow Insecure code" option on the "generate" tab.

Authentication with dongle

During program development, developers need to effectively protect confidential data. For example, if a user's password is verified from the database, it can be easily discovered or cracked by illegal personnel. In this example, the dongle is used for identity authentication. Instance running result 13.5 shows that the key of this example is to read data from the dongle, which can be implemented using the readdog function. For more information about this function, see the "technical points" section in "password writing and reading dongle.

Implementation Process

(1) create a new project named ex13_04. The default form is form1.

(2) In the form1 form, two buttons are added for data verification and exit, and two textbox controls are added for entering the user name and password respectively.

(3) Main program code.

Private void button#click (Object sender, eventargs E)

{

Dog dog = new dog (100 );

Dog. dogaddr = 0;

Dog. dogbytes = 6;

Dog. readdog ();

If (dog. retcode = 0)

{

Char [] chtemp = new char [6];

For (INT I = 0; I <6; I ++)

{

Chtemp [I] = (char) dog. dogdata [I];

}

String STR = new string (chtemp );

If (textbox2.text = Str)

{

MessageBox. Show ("OK ");

}

Else

{

MessageBox. Show ("error ");

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.