gpt4 book ai didi

c - 简单的 base64 解码不起作用 :(

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:01 25 4
gpt4 key购买 nike

我一直在尝试使用以下代码对输入的 char * 进行 base64 解码。“msg”的值似乎是一个空字符串:(

#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;

}

最佳答案

对不起!这是正确的方法。我刚刚清理了主要功能:

int main(int argc, char *argv[])
{
int i = 0;
char buff [] ="aGVsbG8K\n\0" ;
char* ptr ;

for(i =0;i < 4; i++){

printf("strlen buff = %d\n", strlen(buff));
ptr = dgstdecode(buff, strlen(buff));
printf("ptr = %s\n", ptr);
printf("\n");
}

return 0;
}

我现在的输出是(仍在执行删除/n 的操作):

strlen 增益 = 9在 dgstdecode 里面

pReadBuffer = aGVsbG8K

读取字节数:6在 dgstdecode 里面消息 = 你好

ptr = 你好

strlen 增益 = 9在 dgstdecode 里面

pReadBuffer = aGVsbG8K

读取字节数:6在 dgstdecode 里面消息 = 你好

ptr = 你好

strlen 增益 = 9在 dgstdecode 里面

pReadBuffer = aGVsbG8K

读取字节数:6在 dgstdecode 里面消息 = 你好

ptr = 你好

strlen 增益 = 9在 dgstdecode 里面

pReadBuffer = aGVsbG8K

读取字节数:6在 dgstdecode 里面消息 = 你好

ptr = 你好

请忽略我下面的原始回复:

bio = BIO_push(b64, bio); 行似乎是罪魁祸首。它似乎正在覆盖引用。这是我评论该行后的输出(编辑器似乎删除了一些/n):

strlen buff = 8
inside dgstdecode

pReadBuffer = aGVsbG8K
readbytes: 9
inside dgstdecode
msg = aGVsbG8K
ptr = aGVsbG8K

strlen buff = 8
inside dgstdecode

pReadBuffer = aGVsbG8K
readbytes: 9
inside dgstdecode
msg = aGVsbG8K
ptr = aGVsbG8K

strlen buff = 8
inside dgstdecode

pReadBuffer = aGVsbG8K
readbytes: 9
inside dgstdecode
msg = aGVsbG8K
ptr = aGVsbG8K

strlen buff = 8
inside dgstdecode

pReadBuffer = aGVsbG8K
readbytes: 9
inside dgstdecode
msg = aGVsbG8K
ptr = aGVsbG8K

关于c - 简单的 base64 解码不起作用 :(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5715795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com