How can I iterate through all possible values ​​of a byte array of size n?

For my question n = 16, but a general answer would be appreciated.

So I have a byte array:

byte[] key;

      

My problem is that I want to iterate over all possible values ​​of each element in this array in combination. I know this will take a long time and I am not going to actually fill this loop, just to create a loop that at least tries to do it.

For example:

First iteration:

//Math.Pow(2,128) is the max no. of iterations right?
byte[] key;
for(int i = 0; i < Math.Pow(2,128); i++)
{
   key = new byte[16] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
}

      

Second iteration:

//Math.Pow(2,128) is the max no. of iterations right?
byte[] key;
for(int i = 0; i < Math.Pow(2,128); i++)
{
   key = new byte[16] {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
}

      

Third iteration:

//Math.Pow(2,128) is the max no. of iterations right?
byte[] key;
for(int i = 0; i < Math.Pow(2,128); i++)
{
   key = new byte[16] {2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
}

      

Final iteration:

//Math.Pow(2,128) is the max no. of iterations right?
byte[] key;
for(int i = 0; i < Math.Pow(2,128); i++)
{
   key = new byte[16] {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
}

      

Obviously I just coded the array above. I need a way to get it right. Again, I know there are many different combinations. All I need is a way to start repeating all possible values. How do I do this in my loop?

those. that I have to replace the body of my loop to iterate over all possible values ​​of a 16 byte array.

What I have tried:

In the body of the loop, I've tried the following:

key = new byte[16] { (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i, (byte)i };

      

Obviously wrong, will only test a small subset of the possible values. Just try i = 0, ..., 255 and start over when i = 256 -> (bytes) i = 0.

I suspect I need some more nesting. Perhaps up to 16 nested loops, which seems crazy and probably wrong? I can't seem to fix this problem, any help would be greatly appreciated!

Purpose: The purpose of this question is to demonstrate how practical ineffective brute-force cryptanalysis is. The rest of my program is working, I just got stuck in this loop.

+3


source to share


2 answers


If you don’t understand: 16 bytes is the Guid size or the size of the standard cryptographic key size. There are so many combinations that you cannot even list a fraction. Maybe you can list the last 8 bytes if you parallelize 1000 machines and wait a year.

You can do this easily by running a for loop from 0 to ulong.MaxValue

. I present this as an answer because this very simple idea allows you to start enumerating and, in fact, never get to the point where you are done.



for (ulong i = 0; i < ulong.MaxValue; i++) {
 var bytes = new [] {
   0, 0, 0, 0, 0, 0, 0, 0
   , (byte)(i >> (7 * 8))
   , (byte)(i >> (6 * 8))
   , (byte)(i >> (5 * 8))
   //...
   , (byte)(i >> (0 * 8)) };
}

      

Or just use 16 nested loops. I don't think this is crazy because it is so simple that it is clearly correct.

+4


source


this is sample code without exception handling and kind of inefficiency to simulate a counter like the one you mentioned



public static void NextIteration(byte[] input)
{
    if (input.All(x => x == 255))
        throw new InvalidOperationException("there is no iteration left");

    var converted = input.Select(x => (int) x).ToArray();
    converted[0]++;
    for (var i = 0; i < converted.Length; i++)
    {
        if (converted[i] == 256)
        {
            converted[i] = 0;
            converted[i + 1]++;
        }
    }
    for (var i = 0; i < input.Length; i++)
    {
        input[i] = (byte) converted[i];
    }
}

      

+1


source







All Articles