I am looking for a method in C# which generates a random MAC number. Google is pretty thin on that one.
Thx a lot
SOLUTION:
With the help of Yahia I was able to code the following solution. Thx again!
public static string GenerateMACAddress()
{
var sBuilder = new StringBuilder();
var r = new Random();
int number;
byte b;
for (int i = 0; i < 6; i++)
{
number = r.Next(0, 255);
b = Convert.ToByte(number);
if (i == 0)
{
b = setBit(b, 6); //--> set locally administered
b = unsetBit(b, 7); // --> set unicast
}
sBuilder.Append(number.ToString("X2"));
}
return sBuilder.ToString().ToUpper();
}
private static byte setBit(byte b, int BitNumber)
{
if (BitNumber < 8 && BitNumber > -1)
{
return (byte)(b | (byte)(0x01 << BitNumber));
}
else
{
throw new InvalidOperationException(
"Der Wert für BitNumber " + BitNumber.ToString() + " war nicht im zulässigen Bereich! (BitNumber = (min)0 - (max)7)");
}
}
private static byte unsetBit(byte b, int BitNumber)
{
if (BitNumber < 8 && BitNumber > -1)
{
return (byte)(b | (byte)(0x00 << BitNumber));
}
else
{
throw new InvalidOperationException(
"Der Wert für BitNumber " + BitNumber.ToString() + " war nicht im zulässigen Bereich! (BitNumber = (min)0 - (max)7)");
}
}
Connect to a Wi-Fi network. Tap the network to go to the Network details page. Verify that MAC randomization is turned on. Verify that the MAC address displayed is a randomized MAC, which has the locally generated bit set to 1 and the multicast bit set to 0.
MAC spoofing is a technique for changing a factory-assigned Media Access Control (MAC) address of a network interface on a networked device. The MAC address that is hard-coded on a network interface controller (NIC) cannot be changed. However, many drivers allow the MAC address to be changed.
In the Properties window of the network adapter, go to the Advanced tab. Locate and select Network Address in the Property list and type the new MAC address Value you want on the right side. Once you're done, press the OK button.
macOS. iOS 14, iPad OS 14, and WatchOS 7 all support randomized MAC addresses, except the macOS. Now, there's no way to get randomized MAC address on macOS per se. However, you can force your Wi-Fi to connect to a wireless network with a fake MAC address.
A slightly less verbose solution (which I think still achieves the same outcome):
public static string GetRandomMacAddress()
{
var random = new Random();
var buffer = new byte[6];
random.NextBytes(buffer);
var result = String.Concat(buffer.Select(x => string.Format("{0}:", x.ToString("X2"))).ToArray());
return result.TrimEnd(':');
}
This gives a formatted MAC, remove string.Format and Trim if unformatted is required
There is no such method in the .NET framework...
You will need to write one yourself - read the format description, use a random generator to get 6 random numbers between 0 and 255, setup the 2 relevant bits (for globally unique/locally administered) as need be and then transform the number to hex (i.e. X2
, 2 digits per number, left padded with 0) and join these together with :
as delimiter...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With