使用 OpenSSL 进行 Base64 编码和解码

31

我一直在尝试弄清楚openssl文档中关于base64解码和编码的内容。我找到了以下一些代码片段。

#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;
}

这似乎只适用于单行字符串,例如 "Start",一旦我引入带有换行符和空格等的复杂字符串,它就会失败。

甚至不必使用openssl,一个简单的类或一组执行相同操作的函数也可以,解决方案的构建过程非常复杂,我试图避免去那里进行多次更改。我选择openssl的唯一原因是解决方案已经使用了这些库进行编译。


一个纯OpenSSL的答案: https://dev59.com/3W435IYBdhLWcg3wqB4x#33331627 :) - fsenart
这里有一个类似的问题(https://dev59.com/IXRC5IYBdhLWcg3wUfJ2),它是关于C语言的,但是那里有C++的答案:https://dev59.com/IXRC5IYBdhLWcg3wUfJ2#34201175 - anton_rh
10个回答

47

个人而言,我发现 OpenSSL API 实在是太难用了,除非避免它的代价非常高,否则我会尽量避免使用。我很不安它已成为密码学领域的标准 API。

我感到有点无聊,于是我用 C++ 为您写了一个加密程序。这个程序甚至可以处理可能导致安全问题的边缘情况,例如编码过大导致整数溢出的字符串。

我对它进行了一些单元测试,所以应该可以正常工作。

#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;
}

这只是一个新手问题!!当我们返回 retval 时,它不是一个栈变量,不应该被返回吗?因为一旦函数超出作用域,它就可以被移除? - bana
@bana - 这个函数返回一个新的std::string对象,该对象在堆栈对象被销毁之前被分配,因此是安全的。 - nevelis
16
即使这是一个完全可接受的解决方案,但问题是如何在OpenSSL中实现,因此这个答案与原始问题无关,对我没有帮助。你觉得OpenSSL是行业标准令人不安 - 你能建议一个替代方案吗(不需要自己去做)? OpenSSL库在许多领域都广泛使用机器特定的汇编以获得更好的性能,并且API是面向基于流的处理设计的;也许这就是你对编写基于块的包装器感到“不安”的原因。免责声明:我不是OpenSSL的维护者。 - nevelis
@Omnifarious 这是我的纯OpenSSL答案,能否请您审查一下。https://dev59.com/3W435IYBdhLWcg3wqB4x#33331627 - fsenart
@nevelis - 我会考虑编写使用OpenSSL API的C++版本。不幸的是,其它选项都是零散的,我不知道它们中的任何一个是否更好。并且,正是API的面向流的特性困扰了我。虽然,也有点困扰。OpenSSL根本不应该关心像文件描述符或流之类的东西。它应该能够在任何传输上使用,并且调用者应该处理细节。更多的是API的过度繁琐使我困扰。对于最小的事情,有如此多的步骤,没有明显的原因。 - Omnifarious
显示剩余2条评论

29

使用 BIO_ 接口比起使用EVP_接口更加困难。例如:

#include <iostream>
#include <stdlib.h>
#include <openssl/evp.h>

char *base64(const unsigned char *input, int length) {
  const auto pl = 4*((length+2)/3);
  auto output = reinterpret_cast<char *>(calloc(pl+1, 1)); //+1 for the terminating null that EVP_EncodeBlock adds on
  const auto ol = EVP_EncodeBlock(reinterpret_cast<unsigned char *>(output), input, length);
  if (pl != ol) { std::cerr << "Whoops, encode predicted " << pl << " but we got " << ol << "\n"; }
  return output;
}

unsigned char *decode64(const char *input, int length) {
  const auto pl = 3*length/4;
  auto output = reinterpret_cast<unsigned char *>(calloc(pl+1, 1));
  const auto ol = EVP_DecodeBlock(output, reinterpret_cast<const unsigned char *>(input), length);
  if (pl != ol) { std::cerr << "Whoops, decode predicted " << pl << " but we got " << ol << "\n"; }
  return output;
}

EVP函数还包括一个流接口,请参阅手册。


有很多例子都是用困难的方式来实现这个。感谢您发布了EVP_EncodeBlock方法! - Shibumi
谢谢!这是最好的方法。简单明了。 - rodolk
1
(1) decode64() 的结果是二进制的,因此需要一个长度 -- 例如可以返回 std::pair<unsigned char*, size_t>。 (2) 每次调用 calloc() 都需要调用 free()。 (3) 在存在输入填充的情况下,decode64() 的输出长度必须减少。 - badfd

10

这里是我写的 OpenSSL base64 编解码示例:

请注意,代码中有一些宏/类,但它们对示例并不重要。它们只是我编写的一些 C++ 包装器:

buffer base64::encode( const buffer& data )
{
    // bio is simply a class that wraps BIO* and it free the BIO in the destructor.

    bio b64(BIO_f_base64()); // create BIO to perform base64
    BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);

    bio mem(BIO_s_mem()); // create BIO that holds the result

    // chain base64 with mem, so writing to b64 will encode base64 and write to mem.
    BIO_push(b64, mem);

    // write data
    bool done = false;
    int res = 0;
    while(!done)
    {
        res = BIO_write(b64, data.data, (int)data.size);

        if(res <= 0) // if failed
        {
            if(BIO_should_retry(b64)){
                continue;
            }
            else // encoding failed
            {
                /* Handle Error!!! */
            }
        }
        else // success!
            done = true;
    }

    BIO_flush(b64);

    // get a pointer to mem's data
    char* dt;
    long len = BIO_get_mem_data(mem, &dt);

    // assign data to output
    std::string s(dt, len);

    return buffer(s.length()+sizeof(char), (byte*)s.c_str());
}

9

这对我有用,并且使用valgrind验证没有内存泄漏。

#include <openssl/bio.h>
#include <openssl/evp.h>
#include <cstring>
#include <memory>
#include <string>
#include <vector>

#include <iostream>

namespace {
struct BIOFreeAll { void operator()(BIO* p) { BIO_free_all(p); } };
}

std::string Base64Encode(const std::vector<unsigned char>& binary)
{
    std::unique_ptr<BIO,BIOFreeAll> b64(BIO_new(BIO_f_base64()));
    BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL);
    BIO* sink = BIO_new(BIO_s_mem());
    BIO_push(b64.get(), sink);
    BIO_write(b64.get(), binary.data(), binary.size());
    BIO_flush(b64.get());
    const char* encoded;
    const long len = BIO_get_mem_data(sink, &encoded);
    return std::string(encoded, len);
}

// Assumes no newlines or extra characters in encoded string
std::vector<unsigned char> Base64Decode(const char* encoded)
{
    std::unique_ptr<BIO,BIOFreeAll> b64(BIO_new(BIO_f_base64()));
    BIO_set_flags(b64.get(), BIO_FLAGS_BASE64_NO_NL);
    BIO* source = BIO_new_mem_buf(encoded, -1); // read-only source
    BIO_push(b64.get(), source);
    const int maxlen = strlen(encoded) / 4 * 3 + 1;
    std::vector<unsigned char> decoded(maxlen);
    const int len = BIO_read(b64.get(), decoded.data(), maxlen);
    decoded.resize(len);
    return decoded;
}

int main()
{
    const char* msg = "hello";
    const std::vector<unsigned char> binary(msg, msg+strlen(msg));
    const std::string encoded = Base64Encode(binary);
    std::cout << "encoded = " << encoded << std::endl;
    const std::vector<unsigned char> decoded = Base64Decode(encoded.c_str());
    std::cout << "decoded = ";
    for (unsigned char c : decoded) std::cout << c;
    std::cout << std::endl;
    return 0;
}

编译:

g++ -lcrypto main.cc

输出:

encoded = aGVsbG8=
decoded = hello

7

有许多糟糕的C代码例子使用缓冲区和malloc(),对于这个标记为C++的问题来说,正确地使用std::string怎么样?

#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/buffer.h>
#include <string>

std::string base64_encode(const std::string& input)
{
    const auto base64_memory = BIO_new(BIO_s_mem());
    auto base64 = BIO_new(BIO_f_base64());
    base64 = BIO_push(base64, base64_memory);
    BIO_write(base64, input.c_str(), static_cast<int>(input.length()));
    BIO_flush(base64);
    BUF_MEM* buffer_memory{};
    BIO_get_mem_ptr(base64, &buffer_memory);
    auto base64_encoded = std::string(buffer_memory->data, buffer_memory->length - 1);
    BIO_free_all(base64);
    return base64_encoded;
}

6

我喜欢 mtrw 对 EVP 的运用。

下面是我对他的回答的“现代C ++”版本,不需要手动内存分配 (calloc)。它接受一个std::string,但很容易重载以使用原始字节。

#include <openssl/evp.h>

#include <memory>
#include <stdexcept>
#include <vector>


auto EncodeBase64(const std::string& to_encode) -> std::string {
  /// @sa https://www.openssl.org/docs/manmaster/man3/EVP_EncodeBlock.html

  const auto predicted_len = 4 * ((to_encode.length() + 2) / 3);  // predict output size

  const auto output_buffer{std::make_unique<char[]>(predicted_len + 1)};

  const std::vector<unsigned char> vec_chars{to_encode.begin(), to_encode.end()};  // convert to_encode into uchar container

  const auto output_len = EVP_EncodeBlock(reinterpret_cast<unsigned char*>(output_buffer.get()), vec_chars.data(), static_cast<int>(vec_chars.size()));

  if (predicted_len != static_cast<unsigned long>(output_len)) {
    throw std::runtime_error("EncodeBase64 error");
  }

  return output_buffer.get();
}

auto DecodeBase64(const std::string& to_decode) -> std::string {
  /// @sa https://www.openssl.org/docs/manmaster/man3/EVP_DecodeBlock.html

  const auto predicted_len = 3 * to_decode.length() / 4;  // predict output size

  const auto output_buffer{std::make_unique<char[]>(predicted_len + 1)};

  const std::vector<unsigned char> vec_chars{to_decode.begin(), to_decode.end()};  // convert to_decode into uchar container

  const auto output_len = EVP_DecodeBlock(reinterpret_cast<unsigned char*>(output_buffer.get()), vec_chars.data(), static_cast<int>(vec_chars.size()));

  if (predicted_len != static_cast<unsigned long>(output_len)) {
    throw std::runtime_error("DecodeBase64 error");
  }

  return output_buffer.get();
}

可能有更加简洁和更好的方法来完成这个操作(我想要摆脱 reinterpret_cast )。你一定也需要一个 try/catch 块来处理潜在的异常。


在两个函数返回之后,你在释放指针后继续使用它们。要么“move”或“release”你的唯一指针;或者选择返回“vector<unit8_t>”/“string”,哪种方法更好。我不争论转换逻辑。你的 API 不好。更好的函数输入应该是“span”/“string_view”。我避免使用模板函数;否则应该使用“std::ranges::range”输入参数。 - Red.Wave

4

改进的TCS答案,移除了宏和数据结构

unsigned char *encodeb64mem( unsigned char *data, int len, int *lenoutput )
{
// bio is simply a class that wraps BIO* and it free the BIO in the destructor.

BIO *b64 = BIO_new(BIO_f_base64()); // create BIO to perform base64
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

BIO *mem = BIO_new(BIO_s_mem()); // create BIO that holds the result

// chain base64 with mem, so writing to b64 will encode base64 and write to mem.
BIO_push(b64, mem);

// write data
bool done = false;
int res = 0;
while(!done)
{
    res = BIO_write(b64, data, len);

    if(res <= 0) // if failed
    {
        if(BIO_should_retry(b64)){
            continue;
        }
        else // encoding failed
        {
            /* Handle Error!!! */
        }
    }
    else // success!
        done = true;
}

BIO_flush(b64);

// get a pointer to mem's data
unsigned char* output;
*lenoutput = BIO_get_mem_data(mem, &output);

// assign data to output
//std::string s(dt, len2);

return output;
}

写入文件

int encodeb64(unsigned char* input, const char* filenm, int leni)
{
BIO *b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);

BIO *file = BIO_new_file(filenm, "w");
BIO *mem = BIO_new(BIO_f_buffer());
BIO_push(b64, mem);
BIO_push(mem, file);

// write data
bool done = false;
int res = 0;
while(!done)
{
    res = BIO_write(b64, input, leni);

    if(res <= 0) // if failed
    {
        if(BIO_should_retry(b64)){
            continue;
        }
        else // encoding failed
        {
            /* Handle Error!!! */
        }
    }
    else // success!
        done = true;
}

BIO_flush(b64);
BIO_pop(b64);
BIO_free_all(b64);

    return 0;
}

将文件进行Base64编码并输出到另一个文件。由于文件限制,我们经常需要分块读取数据并进行编码。以下是代码:

int encodeb64FromFile(const char* input, const char* outputfilename)
{
BIO *b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
int leni = 3*64;
unsigned char *data[3*64];
BIO *file = BIO_new_file(outputfilename, "w");
BIO *mem = BIO_new(BIO_f_buffer());
BIO_push(b64, mem);
BIO_push(mem, file);

FILE *fp = fopen(input, "rb");
while ((leni = fread(data, 1, sizeof data, fp)) > 0) {
    // write data
    bool done = false;
    int res = 0;
    while(!done)
    {
        res = BIO_write(b64, data, leni);

        if(res <= 0) // if failed
        {
            if(BIO_should_retry(b64)){
                continue;
            }
            else // encoding failed
            {
                /* Handle Error!!! */
            }
        }
        else // success!
            done = true;
    }

 }

 BIO_flush(b64);
BIO_pop(b64);
BIO_free_all(b64);
fclose(fp);

return 0;
 }

2
encodeb64mem为什么没有调用BIO_free_all呢?这不是一个内存泄漏吗? - Jason Rice

3

Base64非常简单,你可以很容易地通过快速搜索获得任意数量的实现。例如,这里是一个来自Internet Software Consortium的C语言参考实现,其中有详细的注释解释该过程:网址链接

如果你只是进行编码/解码操作,那么openssl实现将使用"BIO"等复杂的东西,这些都不是(在我看来)非常有用。


3
  #include <openssl/bio.h>

  typedef unsigned char byte;      

  namespace base64 {
    static void Encode(const byte* in, size_t in_len,
                       char** out, size_t* out_len) {
      BIO *buff, *b64f;
      BUF_MEM *ptr;

      b64f = BIO_new(BIO_f_base64());
      buff = BIO_new(BIO_s_mem());
      buff = BIO_push(b64f, buff);

      BIO_set_flags(buff, BIO_FLAGS_BASE64_NO_NL);
      BIO_set_close(buff, BIO_CLOSE);
      BIO_write(buff, in, in_len);
      BIO_flush(buff);

      BIO_get_mem_ptr(buff, &ptr);
      (*out_len) = ptr->length;
      (*out) = (char *) malloc(((*out_len) + 1) * sizeof(char));
      memcpy(*out, ptr->data, (*out_len));
      (*out)[(*out_len)] = '\0';

      BIO_free_all(buff);
    }

    static void Decode(const char* in, size_t in_len,
                       byte** out, size_t* out_len) {
      BIO *buff, *b64f;

      b64f = BIO_new(BIO_f_base64());
      buff = BIO_new_mem_buf((void *)in, in_len);
      buff = BIO_push(b64f, buff);
      (*out) = (byte *) malloc(in_len * sizeof(char));

      BIO_set_flags(buff, BIO_FLAGS_BASE64_NO_NL);
      BIO_set_close(buff, BIO_CLOSE);
      (*out_len) = BIO_read(buff, (*out), in_len);
      (*out) = (byte *) realloc((void *)(*out), ((*out_len) + 1) * sizeof(byte));
      (*out)[(*out_len)] = '\0';

      BIO_free_all(buff);
    }
  }

抱歉这是你的审核请求几年后才回复,但是...在C++中尤其是,在C中也不要将类型转换为void *。同时,在C++中不要使用C风格的强制类型转换(即(type)类型转换)。应该始终使用static_castconst_castreinterpret_cast,并且使用最小强度的转换来完成任务。在C++代码中也不要使用malloc。也不应该让裸指针逃逸到必须处理OpenSSL的函数之外。out_len应该是一个引用而不是一个指针。基本上,这是一个C解决方案,而不是C++解决方案。 - Omnifarious
4
@Omnifarious进行了一次很好的代码审查。他说这只是一个OpenSSL方案,而不是C++方案。几年前我发现这个方案很有用,虽然我需要像你建议的那样清理它,但它仍然是一个适合一页的OpenSSL方案。如果你正在努力批评答案,为什么不像在Stack Overflow上一样建议对代码进行编辑呢? - nevelis
1
@nevelis - 这个问题使用了C++标签,所以我假设发帖人想要一个C++风格的答案。 - Omnifarious

1
我虽然有点晚到,但最近也遇到了这个问题。不过我对 BIO 解决方案感到不太满意,因为它过于复杂,而 'EncodeBlock' 也不是我想要的,因为它会在我的 Base64 编码字符串中引入换行符。
经过一番探索,我发现 OpenSSL 的头文件 openssl/include/crypto/evp.h 可以解决这个问题。这个头文件并不是默认安装的一部分(对我来说只导出了 include/openssl 文件夹),但它提供了解决方案。
void evp_encode_ctx_set_flags(EVP_ENCODE_CTX *ctx, unsigned int flags);

/* EVP_ENCODE_CTX flags */
/* Don't generate new lines when encoding */
#define EVP_ENCODE_CTX_NO_NEWLINES          1
/* Use the SRP base64 alphabet instead of the standard one */
#define EVP_ENCODE_CTX_USE_SRP_ALPHABET     2

使用此函数,可以使用EVP接口实现'无换行符'。
例如:
if (EVP_ENCODE_CTX *context = EVP_ENCODE_CTX_new())
{
    EVP_EncodeInit(context);
    evp_encode_ctx_set_flags(context, EVP_ENCODE_CTX_NO_NEWLINES);
    while (hasData())
    {
        uint8_t *data;
        int32_t length = fetchData(&data);
        int32_t size = (((EVP_ENCODE_CTX_num(context) + length)/48) * 65) + 1;
        uint8_t buffer[size];
        EVP_EncodeUpdate(context, buffer, &size, pData, length);
        //process encoded data.
    }
    uint8_t buffer[65];
    int32_t writtenBytes;
    EVP_EncodeFinal(context, buffer, &writtenBytes);
    //Do something with the final remainder of the encoded string.
    EVP_ENCODE_CTX_free(context);
}

这段代码将缓冲区编码为Base64,而不包含换行符。 请注意使用EVP_ENCODE_CTX_num来获取仍存储在上下文对象中的“剩余字节”,以计算正确的缓冲区大小。

只有当您的数据非常大或一次性不可用时,才需要多次调用EVP_EncodeUpdate


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接