写一个Linux下base64解码代码
时间: 2024-05-12 17:17:48 浏览: 191
base64源码.rar_BASE64 解码 VC_c base64_vc base64_vc++base64_解码
好的,以下是Linux下的base64解码代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BASE64_ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
int base64_decode(char *src, char *dst, int len)
{
int i, j, k, n;
char *p;
if (len % 4 != 0) {
return -1;
}
p = dst;
for (i = 0; i < len; i += 4) {
k = 0;
for (j = 0; j < 4; j++) {
n = strchr(BASE64_ALPHABET, src[i+j]) - BASE64_ALPHABET;
if (n < 0 || n > 63) {
return -1;
}
k = (k << 6) | n;
}
*p++ = (k >> 16) & 0xff;
*p++ = (k >> 8) & 0xff;
*p++ = k & 0xff;
}
return p - dst;
}
int main()
{
char *src = "SGVsbG8gV29ybGQh"; // "Hello World!"
char dst[100] = {0};
int len;
len = base64_decode(src, dst, strlen(src));
if (len < 0) {
printf("Invalid base64 string\n");
return -1;
}
printf("%s\n", dst);
return 0;
}
```
希望能帮到你!接下来你可以问我其他问题。
阅读全文