[Serializable] Application--generation, encryption and verification of registration code

Source: Internet
Author: User

1. First define the registration class Registerentity
    [Serializable]     class registerentity    {        stringbool publiclist<int> Public Public DateTime expiredate;}        


Registerkey, registration code (serial number)

Isregistered, have you registered

Registerorder, registration order, due to open the registry code generated by the time the GUID will be exposed, here is a simple encryption algorithm, scrambled into the order after the Registerkey, and the order will be scrambled to the list,

Registerdate, Registration date

ExpireDate, Expiration date

2. Generate Serial Number

        PrivateRegisterentity Generateregisterkey (registerentity registerentity) {StringBuilder Fakekey =NewStringBuilder ();StringKeypart; list<Int> Registerorder =New list<Int>();int splitcount =4;int CurrentOrder =0;for (int i =0; i < Splitcount; i++) {Keypart = Guid.NewGuid (). ToString (). Substring (0,6). ToUpper (); CurrentOrder =New Random (). Next (1, Splitcount +1);while (Registerorder.contains (CurrentOrder-1) {Thread.Sleep (1001, Splitcount + 1) ; } registerorder.add (CurrentOrder-1 "-" 1, 1); Message = Fakekey.tostring (); Registerentity.registerorder =  Registerorder; return registerentity;}      

Here randomly generated 4 six-bit GUID, with three "-" connected is the format of the serial number, the order of the random method, so that the order is only known to the computer, if you want to complicate the point can be splitcount set up a little bit, even 24 letters can be completely disrupted, It's more difficult to crack.

3. Encryption

        PrivateStringEncipherregisterkey (registerentity registerentity) {string[] Fakekeyargs = message. Split (‘-‘);string[] Realkeyargs =Newstring[4];for (int i =0; i < fakekeyargs.length; i++) {Realkeyargs[registerentity.registerorder[i]] = fakekeyargs[i];} StringBuilder Realkey = new StringBuilder (); string Keypart = stringfor (int i = 0; I < Realkeyargs.length; I++ "-" ; Realkey.append (Keypart); } realkey.remove (Realkey.length-1, 1); return realkey.tostring ();}     

In fact, encryption should include the 2nd step, where the serial numbers were rearranged in random order.

4. Write the generated key to the file

        PublicString Generateregisterkeytobinfile (StringRegisterfilename) {message =String. Empty;Try{registerentity =NewRegisterentity (); Generateregisterkey (registerentity);//Encipher Encryption Registerentity.registerkey =Encipherregisterkey (registerentity); registerentity.isregistered =false; IFormatter formatter = new BinaryFormatter (); Stream stream = new FileStream (Registerfilename, FileMode.Create, FileAccess.Write, fileshare.none); formatter . Serialize (stream, registerentity); Stream. Close (); } catch (Exception ex) {message = "Error: " + ex. Message; return message;} return message;}               

Here, each time the new key is generated due to overwrite the old file, while the verification is to read the key file for comparison, so here the isregistered set to False

5. Decryption

PrivateStringDecipherregisterkey (registerentity registerentity) {string[] Realkeyargs = RegisterEntity.RegisterKey.Split (‘-‘);string[] Fakekeyargs =Newstring[4];if (! Utility.isnulloremptylst (registerentity.registerorder) &&!Utility.isnullorempty (Realkeyargs)) {for (int i =0; i < realkeyargs.length; i++) {Fakekeyargs[i] =Realkeyargs[registerentity.registerorder[i]]; }} StringBuilder Decipherkey =new StringBuilder (); string keypart = string. Empty; For (int i = 0; i < fakekeyargs.length; i++) {Keypart = Fakekeyargs[i]. ToString () + "-"; Decipherkey.append (Keypart); } decipherkey.remove (Decipherkey.length- 1, 1); return decipherkey.tostring ();}              

Careful friends will find that decryption is actually similar to encryption, just Fakekeyargs and Realkeyargs in the order of the change, yes, the time to verify the key is compared fakekey rather than really stored in the file Realkey

6. Verify the validity of key and whether it expires

        PublicBOOL Checkregister (StringRegisterfilename) {Registerentity registerentity =NewRegisterentity ();Try{String Dbfilepath =String. Format ("{0}\\{1}", Directory.GetCurrentDirectory (), registerfilename);If(File.exists (Dbfilepath)) {IFormatter formatter =NewBinaryFormatter (); Stream stream =NewFileStream (Registerfilename, FileMode.Open, FileAccess.Read, FileShare.Read); Registerentity =(registerentity) Formatter. Deserialize (stream); Stream. Close ();if (registerentity.isregistered = =true && registerentity.expiredate < datetime.today) {registerentity.isregistered = false; Formatter = new BinaryFormatter (); stream = new FileStream (Registerfilename, FileMode.Create, FileAccess.Write, Fileshare.none); Formatter. Serialize (stream, registerentity); Stream. Close (); }}} catch (Exception ex) { return false;} return registerentity.isregistered;}             

Indicates whether the serial number has expired or has been registered when the software is opened

7. Register and verify

PublicBOOL Registerkey (String Inputkey,StringRegisterfilename) {Registerentity registerentity =NewRegisterentity ();Try{String Dbfilepath =String. Format ("{0}\\{1}", Directory.GetCurrentDirectory (), registerfilename);If(File.exists (Dbfilepath)) {IFormatter formatter =NewBinaryFormatter (); Stream stream =NewFileStream (Registerfilename, FileMode.Open, FileAccess.Read, FileShare.Read); Registerentity =(registerentity) Formatter. Deserialize (stream); Stream. Close ();String Fakekey =Decipherregisterkey (registerentity);if (Inputkey = =Fakekey) {registerentity.isregistered =True; Registerentity.registerdate = Datetime.today; registerentity.expiredate = RegisterEntity.RegisterDate.AddDays ( Span style= "color: #800080;" >30); //enable Register Key formatter = new BinaryFormatter (); stream = new FileStream (Registerfilename, FileMode.Create, FileAccess.Write, Fileshare.none); Formatter. Serialize (stream, registerentity); Stream. Close (); }}} catch (Exception ex) { return falsereturn registerentity.isregistered;}        

After entering the serial number, register, verify the serial number is valid, if valid, activate the software

[Serializable] Application--generation, encryption and verification of registration code

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.