[英]How to incrementally iterate through all possible values of a byte array of size n?
For my question n=16, but a generic answer would be appreciated too. 对于我的问题n = 16,但一般的答案也将受到赞赏。
So I have a byte array: 所以我有一个字节数组:
byte[] key;
My problem is that I want to iterate through all possible values of each element in this array, combined. 我的问题是我希望迭代这个数组中每个元素的所有可能值。 I know this will take ages, and I'm not looking to actually complete this loop, just to make a loop which will at least attempt this.
我知道这需要很长时间,而且我不打算实际完成这个循环,只是为了做一个至少会尝试这个循环的循环。
So eg: 例如:
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 have just hardcoded the array above. 显然我刚刚对上面的数组进行了硬编码。 I need a way of doing this in a proper way.
我需要一种以适当的方式做到这一点的方法。 Again, I know there are many different combinations.
我再次知道有很多不同的组合。 All I need is a way to start iterating through all possible values.
我只需要一种开始迭代所有可能值的方法。 How can I do this in my loop?
我怎么能在循环中做到这一点?
Ie what should I replace the body of my loop with, in order to iterate through all possible values of a byte array of size 16. 即,我应该用我的循环体取代,以便遍历大小为16的字节数组的所有可能值。
What I have tried: 我尝试过的:
In the body of the loop I have 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 possible values. 显然错了,只会测试一小部分可能的值。 Will just try i= 0,...,255 and then start over for when i=256 --> (byte)i = 0.
只需尝试i = 0,...,255,然后在i = 256 - >(字节)i = 0时重新开始。
I suspect I need some more nesting. 我怀疑我需要更多的嵌套。 Possibly up to 16 nested loops, which sounds insane and probably wrong?
可能多达16个嵌套循环,这听起来很疯狂,可能是错的? I can't get my head around this problem, any help would be much appreciated!
我无法理解这个问题,任何帮助都会非常感激!
Purpose: The purpose of this question is to demonstrate how inefficient brute force cryptanalysis is in practice. 目的:这个问题的目的是证明蛮力密码分析在实践中是多么低效。 The rest of my program works, I'm just stuck in this loop.
我的程序的其余部分工作,我只是停留在这个循环中。
In case you don't realize: 16 bytes is the size of a Guid or the size of a standard cryptographic key size. 如果您没有意识到:16个字节是Guid的大小或标准加密密钥大小的大小。 There are so many combinations that you cannot enumerate even a fraction.
有这么多组合,你甚至不能枚举一小部分。 Maybe you can enumerate the last 8 bytes if you parallelize across 1000 machines and wait a year.
如果您在1000台计算机上并行化并等待一年,也许您可以枚举最后8个字节。
You could do that easily by running a for loop from 0 to ulong.MaxValue
. 你可以通过运行从0到
ulong.MaxValue
的for循环来ulong.MaxValue
。 I'm submitting this as an answer because this very simple idea allows you to start enumerating and essentially never come to a point where you finish. 我提交这个作为答案,因为这个非常简单的想法允许你开始枚举,并且基本上永远不会达到你完成的程度。
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 for loops. 或者,只使用16个嵌套for循环。 I don't think that's insane at all because it is so simple that it's clearly correct.
我认为这根本不是疯了,因为它很简单,显然是正确的。
this is a sample code without any exception handling and kind of inefficient 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];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.