我一直在努力为base64使用下面的代码来解码输入的char *。 “味精”的价值似乎是一个空字符串:(简单的base64不进行解码工作:(
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/bn.h>
#include <openssl/bio.h>
#include <openssl/objects.h>
#include <openssl/pem.h>
#include <openssl/objects.h>
// reads b64 encoded msg (pReadBuffer) and writes to pWriiteFile.
char * dgstdecode(char *pReadBuffer, int pLength)
{
char *msg = (char *)malloc(pLength);
memset(msg, 0x00, pLength);
int readbytes = -1;
printf("inside dgstdecode\n");
printf("\n pReadBuffer = %s \n", pReadBuffer);
BIO *b64, *bio = NULL;
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_mem_buf(pReadBuffer, pLength);
bio = BIO_push(b64, bio);
//BIO_set_flags(bio,BIO_FLAGS_BASE64_NO_NL);
while ((readbytes = BIO_read(bio, msg, pLength)) > 0)
{
printf("readbytes: %d\n", readbytes);
printf("inside dgstdecode\n");
}
BIO_flush(bio);
printf("msg = %s\n", msg);
BIO_free_all(bio);
//BIO_free_all(b64);
return msg;
}
int main(int argc, char *argv[])
{
int i = 0;
char buff [9] ="aGVsbG8K" ;
//memset(buff, 0, 9);
char* ptr ;
ptr = (char*)malloc(9);
for(i =0;i < 4; i++){
buff[9] = '\0';
printf("strlen buff = %d\n", strlen(buff));
ptr = dgstdecode(buff, 9);
printf("ptr = %s\n", ptr);
}
return 0;
}
@Joahim:谢谢! – Marc 2011-04-19 12:36:18
但是,如果你评论这一点,你只会得到相同的输入,而不是BASE64解码的输入。正如你所看到的,预期的输出应该是“你好”:( – pimmling 2011-04-19 13:26:33
@ pimmling:对不起,我开始编程,今天半睡着了,早上喝咖啡之前看看我上面的更正 – Marc 2011-04-19 13:51:29