asp.net C # Generate registration code Instance program

Source: Internet
Author: User
Tags md5 readline sha1 trim
The code is as follows Copy Code

Using System;
Using System.Management;
Using System.Security.Cryptography;
Using System.IO;
Using System.Collections.Generic;
Using System.Text;

Namespace Xingming_reg


{


Class Program


{


static void Main (string[] args)


{


BOOL sucsess = false;


String computerbit = "";


Regmutou reg = new Regmutou ();


Computerbit = Reg.getcomputerbit ("X");


Sucsess = Reg.regmutousoft (Computerbit, "Xinmin", "xinmin.usr");


Console.WriteLine (sucsess);


Console.ReadLine ();


}


}


Internal class Regmutou


{


Methods


public string Getbiosserialnumber ()


{


Try


{


ManagementObjectSearcher searcher = new ManagementObjectSearcher ("Select * from Win32_BIOS");


String str = "";


foreach (ManagementObject obj2 in searcher. Get ())


{


str = obj2["SerialNumber"]. ToString (). Trim ();


}


return str;


}


Catch


{


Return "";


}


}

public string Getcomputerbit (string softname)


{


String CpuID = this. Getcpuid ();


String Biosserialnumber = this. Getbiosserialnumber ();


String Harddiskserialnumber = this. Getharddiskserialnumber ();


String netcardmacaddress = this. Getnetcardmacaddress ();


if (CpuID!= "")


{


MD5 MD = new MD5CryptoServiceProvider ();


CpuID = bitconverter.tostring (Md.computehash (Encoding.Default.GetBytes (CpuID))). Replace ("-", ""). ToUpper (). Substring (8, 0x10);


Return (Softname + "C" + CpuID);


}


if (Biosserialnumber!= "")


{


MD5 MD2 = new MD5CryptoServiceProvider ();


Biosserialnumber = bitconverter.tostring (Md2.computehash (Encoding.Default.GetBytes (Biosserialnumber))). Replace ("-", ""). ToUpper (). Substring (8, 0x10);


Return (Softname + "B" + biosserialnumber);


}


if (Harddiskserialnumber!= "")


{


MD5 md3 = new MD5CryptoServiceProvider ();


Harddiskserialnumber = bitconverter.tostring (Md3.computehash (Encoding.Default.GetBytes (Harddiskserialnumber))). Replace ("-", ""). ToUpper (). Substring (8, 0x10);


Return (Softname + "H" + harddiskserialnumber);


}


if (netcardmacaddress!= "")


{


MD5 MD4 = new MD5CryptoServiceProvider ();


netcardmacaddress = bitconverter.tostring (Md4.computehash (Encoding.Default.GetBytes (netcardmacaddress))). Replace ("-", ""). ToUpper (). Substring (8, 0x10);


Return (Softname + "N" + netcardmacaddress);


}


Return (Softname + "Wf53a419db238bbad");


}

public string Getcpuid ()


{


Try


{


Managementobjectcollection instances = new ManagementClass ("Win32_Processor"). GetInstances ();


string str = NULL;


foreach (ManagementObject obj2 in instances)


{


str = obj2. properties["Processorid"]. Value.tostring ();


Break


}


return str;


}


Catch


{


Return "";


}


}

public string Getharddiskserialnumber ()


{


Try


{


ManagementObjectSearcher searcher = new ManagementObjectSearcher ("SELECT * from Win32_physicalmedia");


String str = "";


foreach (ManagementObject obj2 in searcher. Get ())


{


str = obj2["SerialNumber"]. ToString (). Trim ();


Break


}


return str;


}


Catch


{


Return "";


}


}

public string getnetcardmacaddress ()


{


Try


{


ManagementObjectSearcher searcher = new ManagementObjectSearcher ("SELECT * from Win32_NetworkAdapter WHERE (MACAddress is not NULL) and (manufacturer <> ' Microsoft '));


String str = "";


foreach (ManagementObject obj2 in searcher. Get ())


{


str = obj2["MACAddress"]. ToString (). Trim ();


}


return str;


}


Catch


{


Return "";


}


}

public bool Regmutousoft (string computerbit, String softname, string filename)


{


BOOL flag = TRUE;


String str = "";


SHA1 sha = new SHA1CryptoServiceProvider ();


str = bitconverter.tostring (Sha.computehash (Encoding.Default.GetBytes (Softname))). Replace ("-", ""). ToUpper ();


if (! File.exists (filename))


//{


Flag = false;


Stream stream = File.Open (filename, filemode.openorcreate);


StreamWriter writer = new StreamWriter (stream);


Writer. WriteLine (Computerbit);


Writer. Close ();


Stream. Close ();


return flag;


//}


Stream stream2 = File.Open (filename, filemode.open);


StreamReader reader = new StreamReader (STREAM2);


String str2 = "";


string str3 = Computerbit;


while (str2 = reader. ReadLine ())!= null)


//{


STR3 = str2;


//}


Reader. Close ();


Stream2. Close ();


if (STR3. Length!= 0x18)


//{


return false;


//}


SHA1 SHA2 = new SHA1CryptoServiceProvider ();


String STR4 = Bitconverter.tostring (Sha2.computehash (Encoding.Default.GetBytes (Computerbit))). Replace ("-", ""). ToUpper ();


String STR5 = "";


for (int i = 0; i < STR4. Length; i++)


{


if ((i% 2) = = 1)


{


STR5 = STR5 + str4[i];


}


}


String str6 = "";


for (int j = 0; J < str.) Length; J + +)


{


if ((j% 2) = = 0)


{


STR6 = STR6 + str[j];


}


}


int[] Numarray = new INT[20];


for (int k = 0; k < k++)


{


NUMARRAY[K] = Str6[k] + str5[k];


NUMARRAY[K] = numarray[k]% 0x24;


}


String str7 = "";


for (int m = 0; m < m++)


{


if ((M > 0) && ((m% 4) = = 0))


{


STR7 = Str7 + "-";


}


STR7 = str7 + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" [numarray[m]];


}


if (STR3 = = STR7)


{


Flag = true;


}


Console.WriteLine ("Machine code:" + STR3);


Console.WriteLine ("Registration Code:" +STR7);


return flag;


}


}


}

Related Article

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.