简单的 Base64 解码不起作用:(
我一直在尝试使用以下代码对输入 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;
}
I have been trying to base64 decode the input char * using the following code.
The value of "msg" seems to be a null string :(
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉皮姆林!这是正确的方法。我刚刚清理了主要功能:
我的输出现在是(仍在做删除 /n 的事情):
strlen buff = 9
内部 dgstdecode
pReadBuffer = aGVsbG8K
读取字节:6
内部 dgst 解码
消息 = 你好
ptr = 你好
strlen buff = 9
内部 dgstdecode
pReadBuffer = aGVsbG8K
读取字节:6
内部 dgst 解码
消息 = 你好
ptr = 你好
strlen buff = 9
内部 dgstdecode
pReadBuffer = aGVsbG8K
读取字节:6
内部 dgst 解码
消息 = 你好
ptr = 你好
strlen buff = 9
内部 dgstdecode
pReadBuffer = aGVsbG8K
读取字节:6
内部 dgst 解码
msg = hello
ptr = hello
忽略下面我原来的回复:
bio = BIO_push(b64, bio);
行似乎是罪魁祸首。它似乎正在覆盖参考。这是我评论该行后的输出(编辑器似乎删除了一些 /n):Sorry pimmling! Here's the correct approach. I just cleaned up the main function:
My output is now (still doing that dropping the /n thing):
strlen buff = 9
inside dgstdecode
pReadBuffer = aGVsbG8K
readbytes: 6
inside dgstdecode
msg = hello
ptr = hello
strlen buff = 9
inside dgstdecode
pReadBuffer = aGVsbG8K
readbytes: 6
inside dgstdecode
msg = hello
ptr = hello
strlen buff = 9
inside dgstdecode
pReadBuffer = aGVsbG8K
readbytes: 6
inside dgstdecode
msg = hello
ptr = hello
strlen buff = 9
inside dgstdecode
pReadBuffer = aGVsbG8K
readbytes: 6
inside dgstdecode
msg = hello
ptr = hello
Disregard my original response below:
The
bio = BIO_push(b64, bio);
line seems to be the culprit. It seems to be overwriting the reference. Here's my output once I commented that line out (editor seems to be dropping some /n's):