c# - How to incrementally iterate through all possible values of a byte array of size n? -
for question n=16, generic answer appreciated too.
so have byte array:
byte[] key;
my problem want iterate through possible values of each element in array, combined. know take ages, , i'm not looking complete loop, make loop @ least attempt this.
so e.g.:
first iteration:
//math.pow(2,128) max no. of iterations right? byte[] key; for(int = 0; < 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) max no. of iterations right? byte[] key; for(int = 0; < 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) max no. of iterations right? byte[] key; for(int = 0; < 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) max no. of iterations right? byte[] key; for(int = 0; < 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 have hardcoded array above. need way of doing in proper way. again, know there many different combinations. need way start iterating through possible values. how can in loop?
i.e. should replace body of loop with, in order iterate through possible values of byte array of size 16.
what have tried:
in body of loop have tried 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, test small subset of possible values. try i= 0,...,255 , start on when i=256 --> (byte)i = 0.
i suspect need more nesting. possibly 16 nested loops, sounds insane , wrong? can't head around problem, appreciated!
purpose: purpose of question demonstrate how inefficient brute force cryptanalysis in practice. rest of program works, i'm stuck in loop.
in case don't realize: 16 bytes size of guid or size of standard cryptographic key size. there many combinations cannot enumerate fraction. maybe can enumerate last 8 bytes if parallelize across 1000 machines , wait year.
you running loop 0 ulong.maxvalue
. i'm submitting answer because simple idea allows start enumerating , never come point finish.
for (ulong = 0; < 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, use 16 nested loops. don't think that's insane @ because simple it's correct.
Comments
Post a Comment