如何可靠地正则化一个Base64String

本文关键字:一个 Base64String 何可 正则化 | 更新日期: 2023-09-27 18:15:14

基本上我在写一个应用程序在二进制文件中查找PNG文件。它通过将整个二进制文件读入字节数组,然后使用Convert将其转换为字符串来实现这一点。ToBase64String方法,然后使用匹配PNG的头部信息和结束块的正则表达式来查找图像。问题是使用ToBase64String方法根据字节数组的长度产生截然不同的输出,MSDN上的文档似乎没有详细说明。无论如何,这里有一个例子来说明我的意思。

 byte[] somebytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
 Console.WriteLine(Convert.ToBase64String(somebytes));

在这种情况下的输出是"AQIDBAUGBwg="现在如果我跳过一个字节…

 byte[] somebytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
 somebytes = somebytes.Skip(1).ToArray();
 Console.WriteLine(Convert.ToBase64String(somebytes));

现在的输出是"AgMEBQYHCA==",所以几乎每个字符都与前面的示例不同。

所以我无可救药地遵循错误的路径在这里正则化二进制文件或有一个方法(也许是填充?)我能保证这些转换之间的一致性吗?

更新:根据我收集的反馈,似乎我应该离开正则表达式解决方案,手动搜索开始和结束字节序列。不知道为什么我被否决,因为我只是想了解为什么我的其他解决方案是有效的,似乎没有关于这个话题的任何其他帖子。无论如何,感谢大家的快速反馈。当我完成后,我将发布我用于查找图像的算法,以防它可能对其他人有益。

如何可靠地正则化一个Base64String

您在评论中确认您正在尝试从c#结构化文件(EXE或DLL)中提取资源。你可以使用反射方法把它们拉出来:GetManifestResourceStream, GetManifestResourceNames, GetManifestResourceInfo是一个很好的起点。

如前所述,这里是我编写的逻辑,用于在二进制文件中查找图像,以防它可能对其他人有所帮助。然而,我最终可能会使用SledgeHammers方法,但对我来说重要的是,我也能够使用这个方法来处理它。

public class BinarySearch
{
    public static IEnumerable<byte[]> Match(byte[] source, byte[] beginningSequence, byte[] endSequence)
    {
        int index = 0;
        IList<byte[]> matches = new List<byte[]>();
        while (index < source.Length)
        {
            var startIndex = FindSequence(source, beginningSequence, index);
            if (startIndex >= 0)
            {
                var endIndex = FindSequence(source, endSequence, startIndex + beginningSequence.Length);
                if (endIndex >= 0)
                {
                    var length = (endIndex - startIndex) + endSequence.Length;
                    var buffer = new byte[length];
                    Array.Copy(source, startIndex, buffer, 0, length);
                    matches.Add(buffer);
                    index = endIndex + endSequence.Length;
                }
                else
                {
                    index = source.Length;
                }
            }
            else
            {
                index = source.Length;
            }
        }
        return matches;
    }
    private static int FindSequence(byte[] bytes, byte[] sequence, int startIndex = 0)
    {
        int currentIndex = startIndex;
        int sequenceIndex = 0;
        bool found = false;
        while (!found && currentIndex < bytes.Length)
        {
            if (bytes[currentIndex] == sequence[sequenceIndex])
            {
                if (sequenceIndex == (sequence.Length - 1))
                {
                    found = true;
                }
                else
                {
                    sequenceIndex++;
                }
            }
            else
            {
                currentIndex -= sequenceIndex;
                sequenceIndex = 0;
            }
            currentIndex++;
        }
        return found ? (currentIndex - sequence.Length) : -1;
    }
}

下面是PNG文件的用法示例。

var imageHeaderStart = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00 };
var imageEOF = new byte[] { 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 };
var matches = BinarySearch.Match(binaryData, imageHeaderStart, imageEOF);

如果有人对我的"完整"实现感兴趣,我会在完成后添加一个链接到Github项目。