2011-03-04 20 views
0

每當我試着爲base64解碼文件(使用OpenSSL的BIO_f_base64()),比8192更大,我似乎總是得到一些錯誤的值。base64解碼長度大於8192的文件?

這有什麼神奇的數字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; 
} 
+0

什麼是'EVP_MAX_MD_SIZE'的價值? – jswolf19 2011-03-04 10:34:28

+0

消化大小,36個字節(20 + 16) – user489152 2011-03-04 13:06:17

+1

什麼是Openssl_add_all_digests頭文件()。? – Balamurugan 2011-07-20 13:56:53

回答

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; 
} 
+0

你好fazo,我已經用代碼更新了我的帖子。請看看 – user489152 2011-03-04 10:30:01

+0

是你正在閱讀的這個二進制或文本文件嗎? – fazo 2011-03-04 10:50:12

+0

那正在讀這些文件都是二進制 – user489152 2011-03-04 11:02:33