1. <i id='qeJNd'><tr id='qeJNd'><dt id='qeJNd'><q id='qeJNd'><span id='qeJNd'><b id='qeJNd'><form id='qeJNd'><ins id='qeJNd'></ins><ul id='qeJNd'></ul><sub id='qeJNd'></sub></form><legend id='qeJNd'></legend><bdo id='qeJNd'><pre id='qeJNd'><center id='qeJNd'></center></pre></bdo></b><th id='qeJNd'></th></span></q></dt></tr></i><div id='qeJNd'><tfoot id='qeJNd'></tfoot><dl id='qeJNd'><fieldset id='qeJNd'></fieldset></dl></div>
      <tfoot id='qeJNd'></tfoot>

      <small id='qeJNd'></small><noframes id='qeJNd'>

      • <bdo id='qeJNd'></bdo><ul id='qeJNd'></ul>

        <legend id='qeJNd'><style id='qeJNd'><dir id='qeJNd'><q id='qeJNd'></q></dir></style></legend>

        使用 OpenSSL 进行 Base64 编码和解码

        Base64 encoding and decoding with OpenSSL(使用 OpenSSL 进行 Base64 编码和解码)
        <legend id='FbJMp'><style id='FbJMp'><dir id='FbJMp'><q id='FbJMp'></q></dir></style></legend>

          1. <tfoot id='FbJMp'></tfoot>

            <small id='FbJMp'></small><noframes id='FbJMp'>

            <i id='FbJMp'><tr id='FbJMp'><dt id='FbJMp'><q id='FbJMp'><span id='FbJMp'><b id='FbJMp'><form id='FbJMp'><ins id='FbJMp'></ins><ul id='FbJMp'></ul><sub id='FbJMp'></sub></form><legend id='FbJMp'></legend><bdo id='FbJMp'><pre id='FbJMp'><center id='FbJMp'></center></pre></bdo></b><th id='FbJMp'></th></span></q></dt></tr></i><div id='FbJMp'><tfoot id='FbJMp'></tfoot><dl id='FbJMp'><fieldset id='FbJMp'></fieldset></dl></div>

                <bdo id='FbJMp'></bdo><ul id='FbJMp'></ul>

                    <tbody id='FbJMp'></tbody>

                1. 本文介绍了使用 OpenSSL 进行 Base64 编码和解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直试图找出有关 base64 解码和编码的 openssl 文档.我在下面找到了一些代码片段

                  I've been trying to figure out the openssl documentation for base64 decoding and encoding. I found some code snippets below

                  #include <openssl/sha.h>
                  #include <openssl/hmac.h>
                  #include <openssl/evp.h>
                  #include <openssl/bio.h>
                  #include <openssl/buffer.h>
                  
                  char *base64(const unsigned char *input, int length)
                  {
                    BIO *bmem, *b64;
                    BUF_MEM *bptr;
                  
                    b64 = BIO_new(BIO_f_base64());
                    bmem = BIO_new(BIO_s_mem());
                    b64 = BIO_push(b64, bmem);
                    BIO_write(b64, input, length);
                    BIO_flush(b64);
                    BIO_get_mem_ptr(b64, &bptr);
                  
                    char *buff = (char *)malloc(bptr->length);
                    memcpy(buff, bptr->data, bptr->length-1);
                    buff[bptr->length-1] = 0;
                  
                    BIO_free_all(b64);
                  
                    return buff;
                  }
                  
                  char *decode64(unsigned char *input, int length)
                  {
                    BIO *b64, *bmem;
                  
                    char *buffer = (char *)malloc(length);
                    memset(buffer, 0, length);
                  
                    b64 = BIO_new(BIO_f_base64());
                    bmem = BIO_new_mem_buf(input, length);
                    bmem = BIO_push(b64, bmem);
                  
                    BIO_read(bmem, buffer, length);
                  
                    BIO_free_all(bmem);
                  
                    return buffer;
                  }
                  

                  这似乎只适用于单行字符串,例如开始",当我引入带有换行符和空格等的复杂字符串时,它会失败得可怕.

                  This only seems to work for single line strings such as "Start", the moment I introduce complex strings with newlines and spaces etc it fails horribly.

                  它甚至不必是 openssl,一个简单的类或一组做同样事情的函数就可以了,解决方案的构建过程非常复杂,我试图避免进入那里进行多项更改.我选择 openssl 的唯一原因是该解决方案已经与库一起编译.

                  It doesn't even have to be openssl, a simple class or set of functions that do the same thing would be fine, theres a very complicated build process for the solution and I am trying to avoid having to go in there and make multiple changes. The only reason I went for openssl is because the solution is already compiled with the libraries.

                  推荐答案

                  就我个人而言,我发现 OpenSSL API 使用起来非常痛苦,我避免使用它,除非避免它的成本非常高.我发现它已经成为加密世界的标准 API 非常令人沮丧.

                  Personally, I find the OpenSSL API to be so incredibly painful to use, I avoid it unless the cost of avoiding it is extremely high. I find it quite upsetting that it has become the standard API in the crypto world.

                  我觉得无聊,我用 C++ 给你写了一个.这个甚至应该处理可能导致安全问题的边缘情况,例如,编码一个导致整数溢出的字符串,因为它太大了.

                  I was feeling bored, and I wrote you one in C++. This one should even handle the edge cases that can cause security problems, like, for example, encoding a string that results in integer overflow because it's too large.

                  我已经对它进行了一些单元测试,所以它应该可以工作.

                  I have done some unit testing on it, so it should work.

                  #include <string>
                  #include <cassert>
                  #include <limits>
                  #include <stdexcept>
                  #include <cctype>
                  
                  static const char b64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
                  
                  static const char reverse_table[128] = {
                     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
                     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
                     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
                     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
                     64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
                     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
                     64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64
                  };
                  
                  ::std::string base64_encode(const ::std::string &bindata)
                  {
                     using ::std::string;
                     using ::std::numeric_limits;
                  
                     if (bindata.size() > (numeric_limits<string::size_type>::max() / 4u) * 3u) {
                        throw ::std::length_error("Converting too large a string to base64.");
                     }
                  
                     const ::std::size_t binlen = bindata.size();
                     // Use = signs so the end is properly padded.
                     string retval((((binlen + 2) / 3) * 4), '=');
                     ::std::size_t outpos = 0;
                     int bits_collected = 0;
                     unsigned int accumulator = 0;
                     const string::const_iterator binend = bindata.end();
                  
                     for (string::const_iterator i = bindata.begin(); i != binend; ++i) {
                        accumulator = (accumulator << 8) | (*i & 0xffu);
                        bits_collected += 8;
                        while (bits_collected >= 6) {
                           bits_collected -= 6;
                           retval[outpos++] = b64_table[(accumulator >> bits_collected) & 0x3fu];
                        }
                     }
                     if (bits_collected > 0) { // Any trailing bits that are missing.
                        assert(bits_collected < 6);
                        accumulator <<= 6 - bits_collected;
                        retval[outpos++] = b64_table[accumulator & 0x3fu];
                     }
                     assert(outpos >= (retval.size() - 2));
                     assert(outpos <= retval.size());
                     return retval;
                  }
                  
                  ::std::string base64_decode(const ::std::string &ascdata)
                  {
                     using ::std::string;
                     string retval;
                     const string::const_iterator last = ascdata.end();
                     int bits_collected = 0;
                     unsigned int accumulator = 0;
                  
                     for (string::const_iterator i = ascdata.begin(); i != last; ++i) {
                        const int c = *i;
                        if (::std::isspace(c) || c == '=') {
                           // Skip whitespace and padding. Be liberal in what you accept.
                           continue;
                        }
                        if ((c > 127) || (c < 0) || (reverse_table[c] > 63)) {
                           throw ::std::invalid_argument("This contains characters not legal in a base64 encoded string.");
                        }
                        accumulator = (accumulator << 6) | reverse_table[c];
                        bits_collected += 6;
                        if (bits_collected >= 8) {
                           bits_collected -= 8;
                           retval += static_cast<char>((accumulator >> bits_collected) & 0xffu);
                        }
                     }
                     return retval;
                  }
                  

                  这篇关于使用 OpenSSL 进行 Base64 编码和解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                2. <legend id='aaKB7'><style id='aaKB7'><dir id='aaKB7'><q id='aaKB7'></q></dir></style></legend>

                    <tfoot id='aaKB7'></tfoot>
                      <tbody id='aaKB7'></tbody>

                        <small id='aaKB7'></small><noframes id='aaKB7'>

                        <i id='aaKB7'><tr id='aaKB7'><dt id='aaKB7'><q id='aaKB7'><span id='aaKB7'><b id='aaKB7'><form id='aaKB7'><ins id='aaKB7'></ins><ul id='aaKB7'></ul><sub id='aaKB7'></sub></form><legend id='aaKB7'></legend><bdo id='aaKB7'><pre id='aaKB7'><center id='aaKB7'></center></pre></bdo></b><th id='aaKB7'></th></span></q></dt></tr></i><div id='aaKB7'><tfoot id='aaKB7'></tfoot><dl id='aaKB7'><fieldset id='aaKB7'></fieldset></dl></div>
                          • <bdo id='aaKB7'></bdo><ul id='aaKB7'></ul>