我有以下代码:
char *encoded = "dGhpcyBpcyBhIHRlc3Qgc3RyaW5n";
char *unbase = unbase64(encoded,strlen(encoded));
printf("original: %s\n",unbase);
free(unbase);
char *unbase64(unsigned char *input,int length)
{
BIO *b64,*bmem;
char *buff = (char *)malloc(length);
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new_mem_buf(input,length);
bmem = BIO_push(b64,bmem);
BIO_read(bmem,buff,length);
BIO_free_all(bmem);
return buff;
}
char *base64(const unsigned char *input,int length)
{
BIO *bmem,*b64 = NULL;
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;
}
它不显示解码的字符串。
base64 编码工作得很好,那我做错了什么?
编辑:找到答案... base64 解码需要 '\n'