开发者

padding in base64 decode

I am trying to play around with openssl base64 decoding

I have tried to do the base64 decoding using this small example code here:

    // reads b64 encoded msg (pReadBuffer) and开发者_StackOverflow中文版 writes to pWriiteFile.
void decode(char *pReadBuffer, int pLength, char *pWriteFile)
{
    char *msg = (char *)malloc(pLength);
    memset(msg, 0x00, pLength);
    int readbytes = -1;

    printf("buffer write file %s\n", pWriteFile);

    // the decode msg is written to this bio
    BIO *fileWrBIO = BIO_new_file(pWriteFile, "w");

    BIO *b64 = BIO_new(BIO_f_base64());
    //BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
    BIO *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);
       BIO_write(fileWrBIO, msg, readbytes);
       BIO_flush(fileWrBIO);
       memset(msg, 0x00, sizeof(msg));
    }
    free(msg);
    BIO_free_all(bio);
    BIO_free_all(fileWrBIO);
}

Here is the hexdump of my decoded file:

0000000 6854 2065 7571 6369 206b 7262 776f 206e
0000010 6f66 2078 756a 706d 6465 6f20 6576 2072
0000020 6874 2065 6f64 2767 2073 6162 6b63 **0000**
0000030 6877 6c69 2065 6874 2065 6f64 2067 6f6c
0000040 6b6f 6465 6120 2074 6874 2065 6f66 2078
0000050 6562 6968 646e 6920 0074 6877 6c69 2065
0000060 6874 2065 6f64 2067 6f6c 6b6f 6465 6120
0000070 2074 6874 2065 6f66 2078 6562 6968 646e
0000080 6920 0074                              
0000083

Now if I do a base64 decode of the input file using openssl command:

openssl dgst -sha1 -binary file_in | hexdump

0000000 6854 2065 7571 6369 206b 7262 776f 206e
0000010 6f66 2078 756a 706d 6465 6f20 6576 2072
0000020 6874 2065 6f64 2767 2073 6162 6b63 6877
0000030 6c69 2065 6874 2065 6f64 2067 6f6c 6b6f
0000040 6465 6120 2074 6874 2065 6f66 2078 6562
0000050 6968 646e 6920 7774 6968 656c 7420 6568
0000060 6420 676f 6c20 6f6f 656b 2064 7461 7420
0000070 6568 6620 786f 6220 6865 6e69 2064 7469
0000080

As you can see there is an addition of 0000 (highlighted with **). How can I get rid of this in my code? Why do I see this?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜