Base64用C#对PDF进行编码?

Base64用C#对PDF进行编码?,c#,encoding,base64,C#,Encoding,Base64,有人能告诉我们怎么做吗?我可以对常规文本或字节数组执行此操作,但不确定如何处理pdf。我是否要先将pdf填充到字节数组中?用于加载pdf文件,然后使用对字节数组进行常规编码。有一种方法可以分块执行,这样就不必一次性消耗大量内存 Net包含一个可以进行分块的编码器,但它的位置有点奇怪。他们把它放在System.Security.Cryptography命名空间中 我已经测试了下面的示例代码,并且使用我的方法或上面的Andrew方法获得了相同的输出 它的工作原理是:启动一个名为CryptoStrea

有人能告诉我们怎么做吗?我可以对常规文本或字节数组执行此操作,但不确定如何处理pdf。我是否要先将pdf填充到字节数组中?

用于加载pdf文件,然后使用对字节数组进行常规编码。

有一种方法可以分块执行,这样就不必一次性消耗大量内存

Net包含一个可以进行分块的编码器,但它的位置有点奇怪。他们把它放在System.Security.Cryptography命名空间中

我已经测试了下面的示例代码,并且使用我的方法或上面的Andrew方法获得了相同的输出

它的工作原理是:启动一个名为CryptoStream的类。这是一种插入另一个流的适配器。您将一个名为CryptoTransform的类插入CryptoStream(该类又附加到您的文件/内存/网络流),它在数据从流中读取或写入时对数据执行数据转换

通常情况下,转换是加密/解密,但.net也包括ToBase64和FromBase64转换,因此我们不进行加密,只进行编码

这是密码。我包括了Andrew建议的一个实现(可能名称不太好),以便您可以比较输出


    class Base64Encoder
    {
        public void Encode(string inFileName, string outFileName)
        {
            System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.ToBase64Transform();
            using(System.IO.FileStream inFile = System.IO.File.OpenRead(inFileName),
                                      outFile = System.IO.File.Create(outFileName))
            using (System.Security.Cryptography.CryptoStream cryptStream = new System.Security.Cryptography.CryptoStream(outFile, transform, System.Security.Cryptography.CryptoStreamMode.Write))
            {
                // I'm going to use a 4k buffer, tune this as needed
                byte[] buffer = new byte[4096];
                int bytesRead;

                while ((bytesRead = inFile.Read(buffer, 0, buffer.Length)) > 0)
                    cryptStream.Write(buffer, 0, bytesRead);

                cryptStream.FlushFinalBlock();
            }
        }

        public void Decode(string inFileName, string outFileName)
        {
            System.Security.Cryptography.ICryptoTransform transform = new System.Security.Cryptography.FromBase64Transform();
            using (System.IO.FileStream inFile = System.IO.File.OpenRead(inFileName),
                                      outFile = System.IO.File.Create(outFileName))
            using (System.Security.Cryptography.CryptoStream cryptStream = new System.Security.Cryptography.CryptoStream(inFile, transform, System.Security.Cryptography.CryptoStreamMode.Read))
            {
                byte[] buffer = new byte[4096];
                int bytesRead;

                while ((bytesRead = cryptStream.Read(buffer, 0, buffer.Length)) > 0)
                    outFile.Write(buffer, 0, bytesRead);

                outFile.Flush();
            }
        }

        // this version of Encode pulls everything into memory at once
        // you can compare the output of my Encode method above to the output of this one
        // the output should be identical, but the crytostream version
        // will use way less memory on a large file than this version.
        public void MemoryEncode(string inFileName, string outFileName)
        {
            byte[] bytes = System.IO.File.ReadAllBytes(inFileName);
            System.IO.File.WriteAllText(outFileName, System.Convert.ToBase64String(bytes));
        }
    }
我也在玩加密流连接的地方。在Encode方法中,我将它附加到输出(写入)流,因此当我实例化CryptoStream时,我使用它的Write()方法


当我读取时,我将它附加到输入(读取)流,因此我在加密流上使用read方法。我将它连接到哪个流并不重要。我只需要将适当的读或写枚举成员传递给CryptoStream的构造函数

为什么PDF与字节数组有什么不同呢?的确如此。但是现在机器有很多内存。如果有必要,从文件中读取缓冲块是一种非常标准的技术:)对于我目前所需要的非常有用。谢谢你的提示!这是非常浪费内存的。基于流的方法会更好。JMarsch提出的基于加密的方法可能更有效。您也可以通过一次读取少量字节(我猜是3的倍数)并独立编码,将它们写入需要它们的流中来实现。请参阅我之前的评论。缓冲并不难。而且,接吻原则也适用。为什么要使解决方案比需要的更复杂。如果上述内容符合他的目的(他说是的),那么为什么要让它更复杂呢?2行c#和30行?我还没有运行并验证过这一点,但这看起来非常棒。好主意+1.