Eine C#-Klasse zum: auslesen, ändern, zufälligen generieren, prüfen und konvertieren einer MAC-Adresse.
Version Nr. 2
using Microsoft.Win32;
using System;
namespace MAC_Change
{
class MAC
{
public object GetMac(string name)
{
RegistryKey rkey;
ComboBoxItem c_item = new ComboBoxItem();
rkey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\"+name, true);
if (rkey.GetValue("DriverDesc") != null)
{
c_item.Text = rkey.GetValue("DriverDesc").ToString();
}
else
{
c_item.Text = "";
}
if (rkey.GetValue("NetworkAddress") != null)
{
c_item.Value = rkey.GetValue("NetworkAddress").ToString();
}
else
{
c_item.Value = "";
}
c_item.Name = name;
return c_item;
}
public string CreateMac()
{
Random rand = new Random();
int zufall = 0;
string macadresseclear = "";
for (int i = 0; i < 12; i++)
{
zufall = rand.Next(0, 15);
switch (zufall)
{
case 10: macadresseclear += "A"; break;
case 11: macadresseclear += "B"; break;
case 12: macadresseclear += "C"; break;
case 13: macadresseclear += "D"; break;
case 14: macadresseclear += "E"; break;
case 15: macadresseclear += "F"; break;
default: macadresseclear += zufall.ToString(); break;
}
}
return ConvertMac(macadresseclear);
}
public string ConvertMac(string macadresseclear)
{
string macadresse = "";
int k = 1;
for (int i = 0; i < macadresseclear.Length; i++)
{
macadresse += macadresseclear[i];
if (k == 2)
{
k = 0;
macadresse += ":";
}
k++;
}
return macadresse.Substring(0, 17);
}
private string convert_mac_for_system(string macadresse)
{
string macadresseclear = "";
for (int i = 0; i < macadresse.Length; i++)
{
if (macadresse[i].ToString() != ":")
{
macadresseclear += macadresse[i];
}
}
return macadresseclear;
}
public string ChangeMac(string macadresse, string name)
{
macadresse = convert_mac_for_system(macadresse);
RegistryKey rkey;
rkey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + name, true);
rkey.SetValue("NetworkAddress", macadresse);
return ConvertMac(macadresse);
}
public string SetOriginalMac(string name)
{
RegistryKey rkey;
string macadresseclear;
rkey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\"+name, true);
if (rkey.GetValue("NetworkAddressOriginal") != null)
{
macadresseclear = rkey.GetValue("NetworkAddressOriginal").ToString();
}
else
{
macadresseclear = convert_mac_for_system(CreateMac());
}
return ConvertMac(macadresseclear);
}
public bool CheckMac(string macadresse)
{
bool macOkay = false;
if(macadresse.Length == 17)
{
for (int i = 0; i < macadresse.Length; i++ )
{
switch(macadresse[i])
{
case '0': macOkay = true; break;
case '1': macOkay = true; break;
case '2': macOkay = true; break;
case '3': macOkay = true; break;
case '4': macOkay = true; break;
case '5': macOkay = true; break;
case '6': macOkay = true; break;
case '7': macOkay = true; break;
case '8': macOkay = true; break;
case '9': macOkay = true; break;
case 'A': macOkay = true; break;
case 'B': macOkay = true; break;
case 'C': macOkay = true; break;
case 'D': macOkay = true; break;
case 'E': macOkay = true; break;
case 'F': macOkay = true; break;
case ':': macOkay = true; break;
default: macOkay = false; break;
}
if(i % 3 == 2)
{
if(macadresse[i] != ':')
{
macOkay = false;
}
}
if(!macOkay)
{
return macOkay;
}
}
}
return macOkay;
}
}
}
6 Kommentare zum Snippet