将加密文件中的二进制数据写入指定的内存位置
首先是 aes 加密函数的代码:
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 *)data,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*/
}
我在上面指定的代码部分中需要的是它应该能够
使用文件 file.enc 的内容填充/更改数据(由 u_int8_t*data 指向)
不要担心数据长度,实际上它所采用的输入来自 an/w ip
数据包,因此它可以提供高达 1024 字节的数据,并且文件内容永远不会 超过这个限制。
这是我的尝试(也是出于调试目的,我需要提及 file.enc 的内容以及 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);
这里有一些输出快照可能会有所帮助......
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 $
该功能无法更改内容指向位置 (u_int8_t *data),因此无法在 stdout puts(data)
上写入数据。
请帮助我...如果需要任何进一步的信息,我将添加它。
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 *)data,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.enc
Don'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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试改变
成
Try changing
into