如何仅使用 boost 将字符串编码为 Base64?
我正在尝试将简单的 ASCII 字符串快速编码为 base64(使用 boost::asio 的基本 HTTP 身份验证),并且不粘贴任何新代码或使用 boost 之外的任何库。
简单的签名如下所示: string Base64Encode(const string& text);
我再次意识到该算法很简单,并且有许多库/示例这样做,但我正在寻找一个干净的 boost 示例。我发现了 boost 序列化,但那里或谷歌没有明确的例子。 http://www.boost.org/doc/libs /1_46_1/libs/serialization/doc/dataflow.html
如果不将实际的 Base64 算法显式添加到我的代码中,这是否可能?
I'm trying to quickly encode a simple ASCII string to base64 (Basic HTTP Authentication using boost::asio) and not paste in any new code code or use any libraries beyond boost.
Simple signature would look like:
string Base64Encode(const string& text);
Again I realize the algorithm is easy and there are many libraries/examples doing this but I'm looking for a clean boost example. I found boost serialization but no clear examples there or from Google.
http://www.boost.org/doc/libs/1_46_1/libs/serialization/doc/dataflow.html
Is this possible without adding the actual base64 algorithm explicitly to my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是我的解决方案。它使用与本页上的其他解决方案相同的基本技术,但以我认为更优雅的方式解决了填充问题。该解决方案还使用了 C++11。
我认为大部分代码都是不言自明的。编码函数中的数学位计算我们需要添加的“=”字符的数量。 val.size() 取模 3 得到余数,但我们真正想要的是 val.size() 和下一个能被 3 整除的数字之间的差。因为我们有余数,所以我们可以用 3 减去余数,但是如果我们想要 0,那么就剩下 3,所以我们必须再对 3 取模一次。
Here is my solution. It uses the same basic technique as the other solutions on this page, but solves the problem of the padding in what I feel is a more elegant way. This solution also makes use of C++11.
I think that most of the code is self explanatory. The bit of math in the encode function calculates the number of '=' characters we need to add. The modulo 3 of val.size() the remainder, but what we really want is the difference between val.size() and the next number divisible by three. Since we have the remainder we can just subtract the remainder from 3, but that leaves 3 in the case that we want 0, so we have to modulo 3 one more time.
我对您提供的链接中的示例进行了一些改进:
这会将编码为 base64 的字符串打印出来,格式很好,每 72 个字符就有一个换行符到控制台上,准备好放入电子邮件中。如果您不喜欢换行符,请继续这样做:
I improved the example in the link you provided a little:
This prints the string encoded base64 nicely formated with a line break every 72 characters onto the console, ready to be put into an email. If you don't like the linebreaks, just stay with this:
您可以使用野兽的实现。
对于 boost 版本 1.71,函数为:
#include <boost/beast/core/detail/base64.hpp>
对于旧版本,回到1.66中包含的beast,函数是:
来自#include <boost/beast/core/detail/base64.hpp>
You could use beast's implementation.
For boost version 1.71, the functions are:
From #include <boost/beast/core/detail/base64.hpp>
For older versions back to beast's inclusion in 1.66, the functions are:
From #include <boost/beast/core/detail/base64.hpp>
使用 boost base64 编码解码的另一种解决方案:
以下是测试用例:
希望这有帮助
Another solution using boost base64 encode decode:
And here are the test cases:
Hope this helps
对于来自 Google 的任何人,这是我基于 boost 的 Base64 编码/解码函数。根据上面 DanDan 的评论,它可以正确处理填充。解码函数在遇到非法字符时停止,并返回指向该字符的指针,如果您要解析 json 或 xml 中的 base64,这非常有用。
For anyone coming here from Google, here's my base64 encode/decode functions based off boost. It handles padding correctly as per DanDan's comment above. The decode functions stops when it encounters an illegal character, and returns a pointer to that character, which is great if you're parsing base64 in json or xml.
这是另一个答案:
This is another answer:
Base64 编码文本和数据
Base64 encode text and data
我修改了答案 8,因为它在我的平台上不起作用。
I modified the Answer 8 because it's not functional on my platform.