base64解码长度大于8192的文件?

时间:2011-03-04 10:12:51

标签: c openssl base64

每当我尝试使用大于8192的base64解码文件(使用OpenSSL的BIO_f_base64())时,我似乎总是得到一些错误的值。

这个神奇的数字8192是什么?非常感谢任何教育我的帮助!

更新:

以下是我的代码的一部分:

int dgst(char *alg)
 {
 EVP_MD_CTX ctx;
 const EVP_MD *md;
 unsigned char md_value[EVP_MAX_MD_SIZE];
 unsigned int md_len, i;
 char *toB64val = NULL;
 char *data = NULL;

 OpenSSL_add_all_digests();

 md = EVP_get_digestbyname(alg);

 if(!md) {
        printf("Unknown message digest %s\n", alg);
        exit(1);
 }

 data = readFileBuffer("file_out");
 printf("strlen(data) %d\n", strlen(data));
 EVP_MD_CTX_init(&ctx);
 EVP_DigestInit_ex(&ctx, md, NULL);
 EVP_DigestUpdate(&ctx, data, strlen(data));

 EVP_DigestFinal_ex(&ctx, md_value, &md_len); //retrieve digest from ctx unto md_value and #bytes written is copied into   md_len
 EVP_MD_CTX_cleanup(&ctx);
 unsigned char *copy = malloc(md_len);
 memcpy(copy, md_value, md_len);
 char *buff = encbase64(copy, md_len);

 printf("Digest is:%s\n ", buff);

 free(buff);
 free(toB64val);
 free(data);
 return 0;
 }


char *readFileBuffer(char *name)
{
    FILE *file;
    char *buffer = NULL;
    unsigned long fileLen;
    //Open file
    file = fopen(name, "rb");
    if (!file)
    {
        fprintf(stderr, "Unable to open file %s", name);
        return;
    }
    //Get file length
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    printf("file length  = %ld\n", fileLen);
    fseek(file, 0, SEEK_SET);

    //printf("Allocate memory\n");
    buffer=(char *)malloc(fileLen+1);
    printf("length of write buffer = %d\n", strlen(buffer));
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");

    }

    long int n = fread(buffer,1, fileLen,file);
    buffer[n] = '\0';
    printf("Read no. of bytes = %ld into buffer \n", n);
    printf("len of buffer  %d  \n", strlen(buffer));
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");
        fclose(file);
    }
    fclose(file);
    return buffer;

}

char *encbase64( char *input, int length)
{
BIO *bmem, *b64;
BUF_MEM *bptr;

b64 = BIO_new(BIO_f_base64());
//BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
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;
}

1 个答案:

答案 0 :(得分:1)

8k边界似乎没有任何问题。你能告诉我们你怎么称呼它?

更新

int dgst(char *alg)
 {
 EVP_MD_CTX ctx;
 const EVP_MD *md;
 unsigned char md_value[EVP_MAX_MD_SIZE];
 unsigned int md_len, i;
 char *toB64val = NULL;
 char *data = NULL;

 OpenSSL_add_all_digests();

 md = EVP_get_digestbyname(alg);

 if(!md) {
        printf("Unknown message digest %s\n", alg);
        exit(1);
 }

 data = readFileBuffer("file_out");
 //printf("strlen(data) %d\n", strlen(data)); <- don't use strlen on binary data
 EVP_MD_CTX_init(&ctx);
 EVP_DigestInit_ex(&ctx, md, NULL);
 EVP_DigestUpdate(&ctx, data, strlen(data));

 EVP_DigestFinal_ex(&ctx, md_value, &md_len); //retrieve digest from ctx unto md_value and #bytes written is copied into   md_len
 EVP_MD_CTX_cleanup(&ctx);
 unsigned char *copy = malloc(md_len);
 memcpy(copy, md_value, md_len);
 char *buff = encbase64(copy, md_len);

 printf("Digest is:%s\n ", buff);

 free(buff);
 free(toB64val);
 free(data);
 return 0;
 }


char *readFileBuffer(char *name)
{
    FILE *file;
    char *buffer = NULL;
    unsigned long fileLen;
    //Open file
    file = fopen(name, "rb");
    if (!file)
    {
        fprintf(stderr, "Unable to open file %s", name);
        return;
    }
    //Get file length
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    printf("file length  = %ld\n", fileLen);
    fseek(file, 0, SEEK_SET);

    //printf("Allocate memory\n");
    buffer=(char *)malloc(fileLen/*+1*/); // <- not a string - no need to +1
    //printf("length of write buffer = %d\n", strlen(buffer)); <- again strlen on binary data (you just allocated it, so it contains random bytes)
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");

    }

    long int n = fread(buffer,1, fileLen,file);
    //buffer[n] = '\0';                  // not a string - no need to end it
    printf("Read no. of bytes = %ld into buffer \n", n);
    //printf("len of buffer  %d  \n", strlen(buffer));  <- buffer length is in 'n'
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");
        fclose(file);
    }
    fclose(file);
    return buffer;

}

char *encbase64( char *input, int length)
{
BIO *bmem, *b64;
BUF_MEM *bptr;

b64 = BIO_new(BIO_f_base64());
//BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
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*/); // removed '+1'
//buff[bptr->length-1] = 0;       // not a string


BIO_free_all(b64);

return buff;
}