c语言base64解码
时间: 2023-11-08 08:04:32 浏览: 269
C语言中可以使用Base64解码的函数是`base64_decode`,该函数可以将Base64编码的字符串解码成原始数据。需要使用`#include <openssl/bio.h>`和`#include <openssl/evp.h>`来包含所需的头文件,并链接OpenSSL库。
以下是一个示例代码,演示如何使用C语言解码Base64编码的字符串:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
int base64_decode(const char *encoded, unsigned char **decoded) {
BIO *bio, *b64;
int len = strlen(encoded);
int padding = 0;
// 计算解码后的数据长度
for (int i = len - 1; encoded[i] == '='; i--)
padding++;
int decoded_len = (len * 3) / 4 - padding;
// 分配内存
*decoded = (unsigned char *)malloc(decoded_len + 1);
// 创建BIO对象
bio = BIO_new_mem_buf(encoded, len);
b64 = BIO_new(BIO_f_base64());
bio = BIO_push(b64, bio);
// 解码
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
BIO_read(bio, *decoded, len);
// 终止字符串
(*decoded)[decoded_len] = '\0';
// 清理资源
BIO_free_all(bio);
return decoded_len;
}
int main() {
const char *encoded = "SGVsbG8gd29ybGQh";
unsigned char *decoded;
int decoded_len = base64_decode(encoded, &decoded);
printf("Decoded: %s\n", decoded);
free(decoded);
return 0;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)