在 ReadAllBytes 中搜索特定值
我正在编写一个程序,该程序读取“.exe”文件并将其十六进制值存储在字节数组中,以便与包含一系列值的数组进行比较。 (就像一个非常简单的病毒扫描程序)
byte[] buffer = File.ReadAllBytes(currentDirectoryContents[j]);
然后我使用 BitConverter 创建这些值的单个字符串
string hex = BitConverter.ToString(buffer);
下一步是在该字符串中搜索一系列值(定义)并返回正值匹配。这就是我遇到问题的地方。我的定义是十六进制值,但在记事本中创建并保存为defintions.xyz
string[] definitions = File.ReadAllLines(@"C:\definitions.xyz");
我一直在尝试将它们读入字符串数组并将数组的定义元素与字符串十六进制进行比较
bool[] test = new bool[currentDirectoryContents.Length];
test[j] = hex.Contains(definitions[i]);
这是作业中的一部分,这就是为什么我不会发布该程序的完整代码。上周五之前我没有使用过 C#,所以此时我很可能会犯一些愚蠢的错误。
非常感谢任何建议:)
I am writing a program that reads '.exe' files and stores their hex values in an array of bytes for comparison with an array containing a series of values. (like a very simple virus scanner)
byte[] buffer = File.ReadAllBytes(currentDirectoryContents[j]);
I have then used BitConverter to create a single string of these values
string hex = BitConverter.ToString(buffer);
The next step is to search this string for a series of values(definitions) and return positive for a match. This is where I am running into problems. My definitions are hex values but created and saved in notepad as defintions.xyz
string[] definitions = File.ReadAllLines(@"C:\definitions.xyz");
I had been trying to read them into a string array and compare the definition elements of the array with string hex
bool[] test = new bool[currentDirectoryContents.Length];
test[j] = hex.Contains(definitions[i]);
This IS a section from a piece of homework, which is why I am not posting my entire code for the program. I had not used C# before last Friday so am most likely making silly mistakes at this point.
Any advice much appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前还不清楚您到底使用哪种定义格式。 Base64 是一种很好的 byte[] 编码,您可以使用 Convert.ToBase64String 和 Convert.FromBase64String() 快速来回转换。但你的问题表明字节是以十六进制编码的。我们假设新的 byte[] { 1, 2, 3, 4} 看起来像“01020304”。然后这个辅助函数将这样的字符串转换回 byte[]:
您现在可以使用 Boyer-Moore 等算法进行快速模式搜索。
It is pretty unclear exactly what kind of format you use of the definitions. Base64 is a good encoding for a byte[], you can rapidly convert back and forth with Convert.ToBase64String and Convert.FromBase64String(). But your question suggests the bytes are encoded in hex. Let's assume it looks like "01020304" for a new byte[] { 1, 2, 3, 4}. Then this helper function converts such a string back to a byte[]:
You can now do a fast pattern search with an algorithm like Boyer-Moore.
我希望您明白这是一种非常低效的方法。但除此之外,您应该这样做:
如果您可以使用 LINQ,您可以这样做:
另外,请确保您的定义文件的格式与 BitConverter 的输出相匹配。 ToString():大写,用破折号分隔每个编码字节:
I expect you understand that this is a very inefficient way to do it. But except for that, you should just do something like this:
If you can use LINQ, you can do it like this:
Also, make sure that your definitions-file is formatted in a way that matches the output of
BitConverter.ToString()
: upper-case with dashes separating each encoded byte: