c 语言 openssl base64 编码
时间: 2023-09-30 17:08:39 浏览: 41
以下是使用 OpenSSL 库进行 base64 编码的 C 语言示例代码:
```c
#include <openssl/bio.h>
#include <openssl/evp.h>
char* base64_encode(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 + 1);
memcpy(buff, bptr->data, bptr->length);
buff[bptr->length] = '\0';
BIO_free_all(b64);
return buff;
}
```
使用方法:
```c
char* input = "hello world";
char* encoded = base64_encode(input, strlen(input));
printf("%s", encoded);
free(encoded);
```
输出结果:
```
aGVsbG8gd29ybGQ=
```