Writing binary data from encrypted file to pointed memory location
First of all the code for aes cryptographic function :
void
xorcrypto(u_int8_t *key, u_int32_t keylen,
u_int8_t *data, u_int32_t datalen)
{
/*u_int8_t ....etc are alias for uint8_t...etc so don't bother about them*/
FILE *fp,*fq,*fr;
int i;
fp=fopen("key","wb");
fwrite((char *)key,keylen,1,fp);
fq=fopen("file.txt","wb");
fwrite((char *)da开发者_Go百科ta,datalen,1,fq);
fclose(fq);
fclose(fp);
system("sudo openssl enc -aes-256-cbc -salt -in file.txt
-out file.enc -pass file:key");
/* Here is the code section i need*/
}
What i need in the code section i have specified above is that it should be able to
fill/change the data (pointed by u_int8_t*data) with the contents of file file.encDon't worry about the data length actually the input it is taking is from a n/w ip
packet so it has provision for data upto 1024 bytes and file contents are never going to exceed this limit.Here is my attempt for it (also for debugging purpose i need to mention the contents of file.enc as well as data section to stdout)
fr=fopen("file.enc","rb");
memset(data,0,sizeof(data));
i=0;
while( (ch=fgetc(fr))==EOF) {
data[i]=ch;
i++;
}
data[i]='\0';
i=0;
puts((char *)data);
printf("\n");
fclose(fr);
Here are some output snapshots which may help .....
udit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $ cat key
thisisaeskey
udit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $ cat file.txt
w�uP����abcd
udit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $ cat file.enc
Salted__����a�dR�P��l�C-<��y�O^Z��/a��3����Q
udit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $ hexdump -C file.enc
00000000 53 61 6c 74 65 64 5f 5f b6 f2 b2 d0 61 d9 64 1c |Salted__....a.d.|
00000010 52 e0 50 96 e8 6c 0e c0 43 2d 3c c4 f6 79 1b d2 |R.P..l..C-<..y..|
00000020 4f 5e 5a b1 d6 2f 61 f8 15 f6 33 e1 88 f0 db 51 |O^Z../a...3....Q|
00000030
udit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $
The functon is unable to change the contents of pointed location (u_int8_t *data) and so was unable to write data on stdout puts(data)
.
Please help me on this ...if any further information needed about this i will add it.
Try changing
while( (ch=fgetc(fr))==EOF)
into
while( (ch=fgetc(fr))!=EOF)
精彩评论