Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a random mac address

Tags:

c#

networking

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)");
        }
    }
like image 580
MUG4N Avatar asked Apr 15 '12 10:04

MUG4N


People also ask

How do I get a random MAC address?

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.

Can you make a fake MAC address?

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.

How do I create a custom MAC address?

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.

How do you randomize a MAC address on a MAC?

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.


2 Answers

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

like image 83
tocallaghan Avatar answered Oct 22 '22 17:10

tocallaghan


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...

like image 32
Yahia Avatar answered Oct 22 '22 16:10

Yahia