Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以C为单位创建最大长度的所有可能缓冲区#_C#_.net - Fatal编程技术网

C# 以C为单位创建最大长度的所有可能缓冲区#

C# 以C为单位创建最大长度的所有可能缓冲区#,c#,.net,C#,.net,对于我们的嵌入式设备,我们定义了一个带有RS232(COM端口)的通信协议 现在我想做一些攻击/漏洞测试,发送所有可能的最大长度缓冲区 所以我需要一些代码来创建给定长度的所有可能字节模式的列表 例如,对于给定长度2,我需要以下列表: 0x00 0x01 ... 0xFF 0x0000 0x0001 ... 0xFFFF 有没有关于如何在C#中创建此列表的建议 正如韦斯顿指出的,这基本上是一个排列问题 所以你可以使用他链接到的帖子,但是他链接到的代码不会重复使用用过的字母,所以你不会得到你想要的

对于我们的嵌入式设备,我们定义了一个带有RS232(COM端口)的通信协议

现在我想做一些攻击/漏洞测试,发送所有可能的最大长度缓冲区

所以我需要一些代码来创建给定长度的所有可能字节模式的列表

例如,对于给定长度2,我需要以下列表:

0x00
0x01
...
0xFF
0x0000
0x0001
...
0xFFFF
有没有关于如何在C#中创建此列表的建议


正如韦斯顿指出的,这基本上是一个排列问题

所以你可以使用他链接到的帖子,但是他链接到的代码不会重复使用用过的字母,所以你不会得到你想要的完整排列列表。这可以通过修改该帖子中链接到的优秀代码(在这里找到-)来解决

using System;
using System.Collections.Generic;

public class PermuteUtils
{
    // Returns an enumeration of enumerators, one for each permutation
    // of the input.
    public static IEnumerable<IEnumerable<T>> Permute<T>(IEnumerable<T> list, int count)
    {
        if (count == 0)
        {
            yield return new T[0];
        }
        else
        {
            int startingElementIndex = 0;
            foreach (T startingElement in list)
            {
                IEnumerable<T> remainingItems = list;// - Removed to get all permutations AllExcept(list, startingElementIndex);

                foreach (IEnumerable<T> permutationOfRemainder in Permute(remainingItems, count - 1))
                {
                    yield return Concat<T>(
                        new T[] { startingElement },
                        permutationOfRemainder);
                }
                startingElementIndex += 1;
            }
        }
    }

    // Enumerates over contents of both lists.
    public static IEnumerable<T> Concat<T>(IEnumerable<T> a, IEnumerable<T> b)
    {
        foreach (T item in a) { yield return item; }
        foreach (T item in b) { yield return item; }
    }

}
使用系统;
使用System.Collections.Generic;
公共类permututils
{
//返回枚举数的枚举,每个置换一个枚举数
//输入的一部分。
公共静态IEnumerable置换(IEnumerable列表,int计数)
{
如果(计数=0)
{
收益率-收益率新T[0];
}
其他的
{
int startingElementIndex=0;
foreach(列表中的T startingElement)
{
IEnumerable remainingItems=list;//-删除以获取所有置换AllExcept(list,startingElementIndex);
foreach(置换中保留项的IEnumerable置换(剩余项,计数-1))
{
收益率(
新的T[]{startingElement},
存储器的排列;
}
startingElementIndex+=1;
}
}
}
//枚举两个列表的内容。
公共静态IEnumerable Concat(IEnumerable a,IEnumerable b)
{
foreach(T项在a){yield return item;}
foreach(b中的T项){yield return item;}
}
}
然后要做你想做的,我们只需要得到所有十六进制数字的2,3和4的列表的排列

静态void Main(字符串[]参数) { 字符[]项=新字符[]{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}

    for (int length = 2; length <= 4; length++)
    {
        foreach (IEnumerable<char> permutation in PermuteUtils.Permute(items, length))
        {
            StringBuilder output = new StringBuilder("0x");
            foreach (char c in permutation)
            {
                output.Append(c);
            }

            Console.WriteLine(output.ToString());
        }
    }
    Console.WriteLine("Done");
    Console.ReadLine();
}
for(int-length=2;长度解决方案:
private static void sequencer(字节[]字节,整数i,动作)
{
if(i==bytes.Length)
{
动作(字节);
}
其他的
{
int j=i+1;
对于(int v=0;v<256;v++)
{
字节[i]=转换为字节(v);
SequencesSinner(字节,j,动作);
}
}
}
私有静态无效序列(整数长度、动作)
{
对于(int n=1;nx.ToString(“X2”,CultureInfo.InvariantCulture()())));
sw.Flush();
}
var process=process.Start(“notepad.exe”,outputFilePath);
process.WaitForExit();
File.Delete(outputFilePath);
}
}
}

基本代码始终相同:for循环。要发送所有可能的值,只需循环它们

for (int i=0; i < numberOfBytes; ++i) 
{
    for (BigInteger j=0; j < BigInteger.Pow(2, i * 8); ++j) 
    {
        SendData(j.ToByteArray());
    }
}
for(int i=0;i
我可能弄错了,但当前的问题似乎要求所有可能的长度不超过N的字节序列,假设:

  • 115200波特率
  • 每字节10位
  • 长度N=256^N的可能字节序列数
  • 我的数学是正确的
长度为1到4的可能序列的数量为4311810304,总字节计数为1723033160,这意味着需要发送17天

因此,最好的算法可能是(注意,需要直接发送缓冲区,而不是存储它们,我想):

publicstaticlist GetBuffers(int-maxLength)
{
如果(maxLength>4)抛出新的InvalidOperationException(“失败”);
var result=新列表();

对于(int i=1;i A
for
使用
ushort
变量从
0
循环到
0xFFFF
?您的示例输出比帮助更令人困惑。@Adriano:可能重复:但例如,我在输出中没有得到0x00和0x0000。@weston:链接中的问题是关于排列。我不确定是否可以使用它也是。我将更详细地研究它。
Action
不是一个好的函数名,因为它已经存在于.net framework中(您甚至可以在代码中使用它).从阅读问题来看,OP似乎希望长度为2、长度为3、长度为4等的所有内容都达到最大长度,因此我认为您需要两个循环,一个循环用于改变numberOfBytes,另一个循环用于计算可能性。然后当然要确保将字节数组填充到适当的长度。@Chris这就是他将得到的结果(当然SendData()不应该修剪前导零)。他可能需要更改第二个函数(我将更新)。在第一个示例中,您只发送范围0x0000到0xFFFE。您需要在循环外部对
SendData(0xFFFF)
进行最后一次调用,或者使用更大的数据类型,并包含循环的最大值
(int i=0;i@Adriano:要么这是在变魔术,要么我不清楚。我认为它需要返回
0x01
0x0001
,作为两个独立的东西。在我看来,这两个值都对应于相同的i值(给予或接受endianness,这总是让我困惑)所以你的代码只会返回其中一个。@Chris你说得对!我完全误解了他的问题!在这种情况下,它更简单,谢谢!你的字节比平时大。大多数人的字节中只有8位…其余的听起来似乎是合理的,但这是彻底安全检查的代价…抱歉,这是一个错误RS232的东西,10位=8个数据位+停止位+奇偶校验啊,酷。我确实觉得奇怪,有人在谈论波特率之类的问题时会出错。我将把它归档以备将来参考。:)
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        private static void SequencesInner(byte[] bytes, int i, Action<byte[]> action)
        {
            if (i == bytes.Length)
            {
                action(bytes);
            }
            else
            {
                int j = i + 1;
                for (int v = 0; v < 256; v++)
                {
                    bytes[i] = Convert.ToByte(v);
                    SequencesInner(bytes, j, action);
                }
            }
        }

        private static void Sequences(int length, Action<byte[]> action)
        {
            for (int n = 1; n <= length; n++)
            {
                SequencesInner(new byte[n], 0, action);
            }
        }

        static void Main()
        {
            var maxBufferSize = 2;
            var outputFilePath = Path.GetTempFileName();
            var sb = new StringBuilder();
            using (var sw = new StreamWriter(outputFilePath))
            {
                Sequences(
                    maxBufferSize,
                    a => sw.WriteLine(sb.Clear().Append("0x").Append(String.Join("", Array.ConvertAll(a, x => x.ToString("X2", CultureInfo.InvariantCulture))))));
                sw.Flush();
            }
            var process = Process.Start("notepad.exe", outputFilePath);
            process.WaitForExit();
            File.Delete(outputFilePath);
        }
    }
}
for (int i=0; i < numberOfBytes; ++i) 
{
    for (BigInteger j=0; j < BigInteger.Pow(2, i * 8); ++j) 
    {
        SendData(j.ToByteArray());
    }
}
public static List<byte[]> GetBuffers(int maxLength)
{
    if (maxLength > 4) throw new InvalidOperationException("Fail");
    var result = new List<byte[]>();
    for (int i = 1; i <= maxLength; i++)
    {
        var max = 1 << (i * 8);
        for (long j = 0; j < max; j++)
        {
            // Can remove the Reverse() if endianness/ordering doesn't matter
            var buffer = BitConverter.GetBytes(j).Take(i).Reverse().ToArray());
            result.Add(buffer);
        }
    }
    return result;
}