C - able to read an EVP encrypted file but when I store it in a char array, everything disappears -
i have evp encrypted file want send on socket. however, realised nothing being sent because seems nothing stored in array read file into, sure there stored since able decrypt after reading file.
the code snippet below part boggling me.
file *fp; long lsize; fp = fopen ( evp_file , "rb" ); fseek(fp, 0l, seek_end); int fsize = ftell(fp); fseek(fp, 0l, seek_set); unsigned char *indata = malloc(fsize); fread(indata,sizeof(char),fsize, fp); printf("%s\n", indata); // printing indata returns me nothing @ //then decryption //decrypting indata works! , want.
as commented, no output when print indata. there doing wrong here? need store in array can send on socket.
ps. when use plain text file, works fine.
thanks in advance!!
if it's text data (ascii characters, perhaps utf-8, not binary) need nul
terminate buffer, add 1 byte malloc()
ed size , then
indata[fsize] = '\0';
otherwhise can't print printf()
, instead hexadecimal dump visualize data, like
for (size_t = 0 ; < fsize ; ++i) { fprintf(stdout, "0x%02x ", (unsigned char) indata[i]); if ((i + 1) % 16 != 0) continue; fprintf(stdout, "\n"); }
whether it's useful or not depends on interest in data.
also:
- check
fopen()
did not fail, check return value againstnull
tht. - check
malloc()
did not fail, return value againstnull
.
Comments
Post a Comment