Physical NIC address

The following is to get the network card MAC addresses.

///  <Summary> 
  /// The following is a method of obtaining the MAC address.
   /// only obtain a first block 

  ///  </ Summary> 
  ///  <Returns> </ Returns> 
  public  static  String getmacaddress () 
  { 
  ManagementClass Adapters = new new ManagementClass ( " the Win32_NetworkAdapterConfiguration " );
   String MACAddress = " Unknown " ;
   the foreach (ManagementObject Adapter in adapters.GetInstances ()) 
  { 
  IF (( BOOL ) Adapter [ " IPEnabled" ] == to true ) 
  { 
  MACAddress = adapter.Properties [ " MACAddress " ] .Value.ToString ();
   BREAK ; 
  } 
  } 
  return MACAddress; 
  } 



. Then it is sent back to write a database stored in the client program in 
 public  String GetNetCard () 
  { 
  String STR = "" ; 
  ManagementClass McMAC = new new ManagementClass ( " the Win32_NetworkAdapterConfiguration " ); 
  the ManagementObjectCollection mocMAC = mcMAC.GetInstances ();
   the foreach (ManagementObject m in mocMAC)
  {
  if ((bool)m["IPEnabled"])
  {
  str = m["MacAddress"].ToString();
  break;
  }
  }
  return str;
  }









using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Management;

namespace POS
{
  class GetVol
  {
  [DllImport("kernel32.dll")]
  private static extern int GetVolumeInformation(
  string lpRootPathName,
  string lpVolumeNameBuffer,
  int nVolumeNameSize,
  ref int lpVolumeSerialNumber,
  int lpMaximumComponentLength,
  int lpFileSystemFlags,
  string lpFileSystemNameBuffer,
  int nFileSystemNameSize
  );

  public string GetVolOf(string drvID)
  {
  const int MAX_FILENAME_LEN = 256;
  int retVal = 0;
  int a = 0;
  int b = 0;
  string str1 = null;
  string str2 = null;
  int i = GetVolumeInformation(
  drvID + @":\",
  str1,
  MAX_FILENAME_LEN,
  ref retVal,
  a,
  b,
  str2,
  MAX_FILENAME_LEN
  );

  return retVal.ToString("x");
  }

  public string GetNetCard()
  {
  string str = "";
  ManagementClass mcMAC = new ManagementClass("Win32_NetworkAdapterConfiguration");
  ManagementObjectCollection mocMAC = mcMAC.GetInstances();
  foreach (ManagementObject m in mocMAC)
  {
  if ((bool)m["IPEnabled"])
  {
  str = m["MacAddress"].ToString();
  break;
  }
  }
  return str;
  }
  }
}
View Code

c # How to add software registration code to .NET

This paper aims to propose a method to add software registration code. As for the proposed method is valid, whether we can withstand some of the masters of the reverse break, have proven. I just made my personal opinion. 
First, the target. 
    The goal is clear, is based on personal information need to register the software, generate registration code. And the software itself must be able to verify the registration code is valid. And others can be prevented from reverse calculating check algorithm, generating RI; others can be prevented from cracking force method, modify the software code is executed directly, bypassing the register. 
   
Second, the method discussed 
    to achieve the above objectives, there are two aspects of particular importance. First, the registration code generated selection algorithm; Second, having to make the software self-checking mechanism, not executable prevent tampering. Below we discuss these two points. 
1 . Registration code generated selection algorithm 
   and now there is a lot of software Fillmore, a big reason is because the software registration code generation algorithm itself is too simple. Cracks will readily plucked from the disassembled code registration method, and can be easily reduced to the C code. These do not really crack the master, but can also be considered cattle people. I have also had to understand the relevant knowledge, understand the disassembled code is not difficult, the difficulty is to find each other in relation to call a large number of assembly code, and can reverse write the corresponding code; course, some lazy direct cutout where the assembly code, do not understand the details of the implementation process will be able to achieve the purpose of crack. 
   Closer to home, no matter how others NB, he should at least disassemble, at least to see that the assembly code. And if our algorithm is strong enough, they will work out. Looking at today's standard encryption algorithm, it is nothing more than symmetric encryption and asymmetric encryption. We do not take own algorithm, do not do a thankless thing, their own design algorithm, security is now far less than the commonly used algorithms. I briefly list the commonly used algorithms, and why should we want selection algorithm.
   a. symmetric encryption algorithm now popular are DES, AES two kinds, DES algorithm has been widely used in the banking industry, AES appears relatively late, but security is better than DES. It is only a so-called symmetric key encryption and decryption using the same password, so called symmetric encryption. If we use it to generate the registration code, then our software must be saved in this key. Regardless of where the key is always to exist in one place, and we have no way to ensure the security of the key. The reason is simple, as long as the attacker with under the code, we know that we read out where the key is. 
   b. Non have the RSA asymmetric encryption, elliptic curve, and the like, the most popular or RSA. Although the efficiency and security of elliptic curve is better than RSA, but is more complex, I still did not understand this principle, huh, huh. The so-called asymmetric because it has two keys, one called the public key, called the private key. The public key is released, while the private key is saved himself. Using public key encryption, it must, on the contrary, must be decrypted with the private key encrypted with the public key with a private key to decrypt. So, we know that we can save the contents of the public key in the software, this is public (of course, it is hidden inside software is needed, in short, can cause difficulty to break things we can try). Then, we can keep their own private key. Others can not get private key, there is no registration code to calculate. 
  
2 . So far, we know we can use the RSA algorithm, and it can guarantee the security of the software. At least not produce RI, because someone else without the private key. But how to do, we can follow the process below. 
   . A registration code generation process: 
      the registration information (including the user name information, etc.) ---> --- the RSA private key encryption> License. 
     
   . b software verification process License: 
      License ---> RSA public key to decrypt ---> registration information. 
  
3 
   . A check value we generate for their own software, where you want to use digest algorithm. The most commonly used digest algorithm is MD5, I believe we have heard here will not go into details, we only need when executing software reads executable (in Windows, its own executable file is readable, but if they want to calculate the checksum value rewrite their own, that would not work) and.Solve the first goal, we still difficult to prevent others from tampering with direct binary executable file, thereby bypassing check the registration code. However, the method is always there. To solve this problem, I thought of a way: self-checking software. Specific approach is this: 
   . B Then, we can determine the checksum value is correct, if not correct, we can direct the software to commit suicide. 
  
   But there is a problem, how do we save the correct checksum value in the software do? Can not be saved in a file or registry, so sooner or later we will be aware of. Others can directly modify the check value to be equal to the error check value, spoofing check software. Here is the method: to return to the process (point 1 and 2) is described. How to return? We went on to say. 

4 to save the software checksum: Let checksum value into the registration code. We modify (point 2) is as follows: 
   . A registration code generation process: 
      the registration information (including the user name information, etc.) + Software check value ---> --- the RSA private key encryption> License. 
  
   . License software verification process B: 
      License ---> --- the RSA public key to decrypt> + registration information software check value. 
   This way, we can determine whether this software has been successfully registered. According to the above first step, the software calculates the check value and the registration information, and then put the two current user information, and the calculated check value is compared, if match, the software instructions have been successfully registered. 
  
5 Try to crack.
   If you know the details of the process, how to crack the software? First, we know, we have been unable to write RI come. The only way is to break the profits, but profits cracked, the software added a self-check. However, there is always a sparse hundred secret, profits can still reach the break. Let's recap, if we add the registration code verification in accordance with the above approach. So almost our code is so written: 
  
   int RSA_Check ( char *  sn) {
       check the value of the function computing software and decrypt the checksum value based on the user information registration code sn, then compare, whether the check is returned through. 
   } 
   ... . 
   when you start the software, we call the above function. 
   IF (RSA_Check (sn)) {<-------------------------- 
       in here, if the check fails, it directly Exit ( 0 ), huh, huh ~ 
   } 
  
   , but the Achilles heel in the above compilation ① ① position because this position is similar: 
   je XXXXX     
   .... 
   .... 
  
   just is a jump statement, then it will break the staff directly into the jmp instruction jne or else get away, our whole effort a waste. that being the case, we must not write the code this way, or our approach should not be so simple at least, we can do this: 
   1 . Do not check algorithm written in a unified interface
    2Do not just a place to make a judgment. This point is very simple and very effective, if you wish, you can place a few more judgments on efficiency is not critical in the software, which cracked people are quite painful, at least at the same time he could not find the key to all position. As long as there is a local success did not change, then our self-check on the work, then we have a right to enforce the follow-up action. For example, direct delete the program itself (of course, there must shut itself down, and then calls another process to remove the software), and so on.
   3More "cheap" approach is, since an additional process, the main process and monitor each other, each check. If the check is registered to the other side of the anomaly, it would kill each other, kill each other executable programs. Sounds cheap, but not useful, I do not know.
   4 . If you want to prevent a registration code used everywhere (all Chinese people help each other, huh, huh), the target machine can add information in the registration information, added to the registration information.
   4 . These methods, etc., on the lot, you can play for free. 
  
III. Summarize 
   After the above discussion, I think there is no absolute security, sometimes if you do not want to crack the software, you can only curse the people who are immoral. But we can make them even more painful, at least the rookie is not casually sabotage. In China, write soft on open source, free it, do not expect to take it to support the family. 
  
IV. Annex 
    RSA encryption implementation code
 
View Code

C # access to the machine physical and other computer card address (MAC)

MAC address validation computer software license is a general method, C # can easily obtain the MAC address of the computer, this paper describes the actual source code card acquired in two ways, first method uses ManagementClass class, this can only get computer network card address of the physical machine, the second method uses SendARP Iphlpapi.dll method can obtain the MAC address of the machine and the other computers. 
    Method 1: Use class ManagementClass 
          Example: 
///  <Summary> 
/// Get the physical address of the card
 ///  </ Summary> 
///  <Returns> </ Returns> 
publicstaticstringgetMacAddr_Local () 
{ 
    stringmadAddr = null ; 
    ManagementClass MC = newManagementClass ( " the Win32_NetworkAdapterConfiguration " ); 
    the ManagementObjectCollection moc2 = mc.GetInstances ();
     the foreach(ManagementObject moinmoc2) 
    { 
        IF (Convert.ToBoolean (Mo [ " IPEnabled " ]) == to true ) 
        { 
            madAddr = Mo [ " MacAddress " ] .ToString (); 
            madAddr = madAddr.Replace ( ' : ' , ' - ' ) ; 
        } 
        mo.Dispose (); 
    } 
    returnmadAddr; 
} 
Note:    a need to increase the reference item: System.Management, FIG:
     2 beginning of the program package to add import statement: the using the System.Management; 

    . 3The present embodiment can obtain the MAC address of the machine; 
    Method 2: Use class SendARP 
          Example: 
// The following is a way to obtain the MAC address of the remote 
[the DllImport ( " the Iphlpapi.dll " )] 
staticexternintSendARP (DestIP Int32, Int32 SrcIP, MACADDR refInt64, refInt32 PhyAddrLen); 
[the DllImport ( " Ws2_32.dll " )] 
staticexternInt32 the inet_addr (stringipaddr);        
///  <Summary> 
/// SendArp obtain the MAC address
 ///  </ Summary> 
///  <param name = "RemoteIP"> the IP address of the target machine, such as (192.168.1.1) </ param> 
///  <Returns> target machine mac address </ returns>
publicstaticstringgetMacAddr_Remote(stringRemoteIP)
{
    StringBuilder macAddress =newStringBuilder();
    try
    {
        Int32 remote = inet_addr(RemoteIP);
        Int64 macInfo =newInt64();
        Int32 length = 6;
        SendARP(remote, 0,refmacInfo,reflength);
        stringtemp = Convert.ToString(macInfo, 16).PadLeft(12,'0').ToUpper();
        intx = 12;
        for(inti = 0; i < 6; i++)
        {
            if(i == 5)
            {
                macAddress.Append(temp.Substring(x - 2, 2));
            }
            else
            {
                macAddress.Append(temp.Substring(x - 2, 2) +"-");
            }
            x -= 2;
        }
        returnmacAddress.ToString();
    }
    catch
    {
        returnmacAddress.ToString (); 
    } 
} 
Note:     1 beginning of the program package to add import statement:. the using the System.Runtime.InteropServices;
     2 . The method can obtain the MAC address of the remote computer;
 
View Code

 

Guess you like

Origin www.cnblogs.com/blogpro/p/11462965.html