與我以前的帖子,但它不是that.Now的副本我都試過的東西,將數據寫入文件,但文件仍然包含NULL
在這裏,我問你在代碼中的邏輯錯誤。
/*u_int8_t ....etc are alias for uint8_t...etc so don't bother about them*/
void crypt(u_int8_t *key, u_int32_t keylen,
u_int8_t *data, u_int32_t datalen)
{
FILE *fp,*fq;
fp=fopen("key","w");
fputs((char *)key,fp);
fq=fopen("file.txt","w");
d=0;
while(data[d]) {
fputc((int)data[d],fq);
d++;
}
fputc('\0',fq);
fclose(fp);
fclose(fq)
}
輸出:
[email protected] ~/Downloads/sendip-2.5-mec-2/mec $ cat key
[email protected] ~/Downloads/sendip-2.5-mec-2/mec $ cat file.txt
[email protected] ~/Downloads/sendip-2.5-mec-2/mec $
Key獲取打印到文件而不是數據。
現在,當我稍微修改了代碼:
void
crypt(u_int8_t *key, u_int32_t keylen,
u_int8_t *data, u_int32_t datalen)
{
int d,k;
FILE *fp,*fq;
fp=fopen("key","w");
fputs((char *)key,fp);
fq=fopen("file.txt","w");
for (d=0, k=0; d < datalen; ++d, k = (k+1)%keylen) {
data[d] ^= key[k];
fputc(data[d],fq);
}
fclose(fp);
fclose(fq);
}
Now鍵以及數據獲取打印......雖然數據是不完全正確的(但它能夠被記入文件)
[email protected] ~/Downloads/sendip-2.5-mec-2/mec $ cat key
[email protected] ~/Downloads/sendip-2.5-mec-2/mec $ cat file.txt
kthpOWWkahe;c��"�he
[email protected] ~/Downloads/sendip-2.5-mec-2/mec $
到crypt函數的調用是如下 -
bool
espcrypto(esp_private *epriv, sendip_data *data, sendip_data *pack)
{
u_int32_t keylen;
u_int8_t *key;
static u_int8_t fakekey;
struct ip_esp_hdr *esp = (struct ip_esp_hdr *)pack->data;
if (!epriv->keylen) { /* This isn't going to be very productive... */
key = &fakekey;
keylen = 1;
} else {
key = (u_int8_t *)epriv->key;
keylen = epriv->keylen;
}
/* Encrypt everything past the ESP header */
crypt(key, keylen,
(u_int8_t *)esp->enc_data,
pack->alloc_len + data->alloc_len
- sizeof(struct ip_esp_hdr));
return TRUE;
}
以下分組descri是什麼樣的數據我真的需要寫下來的文件...
[email protected] ~/Downloads/sendip-2.5-mec-2/mec $ sendip -v -p ipv6 -dabcd -6s ::1 -p
esp -es 0x20 -eq 0x40 -ek "kahe" -ec crypt.so -p tcp -ts 21 -td 21 ::2
Added 43 options
Initializing module ipv6
Initializing module esp
Initializing module tcp
Finalizing module tcp
Finalizing module esp
Finalizing module ipv6
Final packet data:
60 00 00 00 `...
00 24 32 20 .$2
00 00 00 00 ....
00 00 00 00 ....
00 00 00 00 ....
00 00 00 01 ....
00 00 00 00 ....
00 00 00 00 ....
00 00 00 00 ....
00 00 00 02 ....
00 00 00 20 ...
00 00 00 40 [email protected]
6B 74 68 70 kthp /*data portion starts from here*/
4F 57 1F 57 OW.W
6B 61 68 65 kahe
3B 63 97 9A ;c..
22 C0 68 65 ".he
0A 03 0B 01 ....
6B 61 6A 63 kajc /*data portion ends here*/
Freeing module ipv6
Freeing module esp
Freeing module tcp
請幫助我....我還沒有接收器在我以前的帖子任何令人滿意的實現還是那麼想我自己hand.Really需要它..
你應該''fclose'這些文件,它應該可能以二進制模式「wb」打開。 –
如果您暫時放一些printf()調用來顯示發生了什麼,可能會更容易發現發生了什麼。數據[d]和密鑰[k],在你之前^ =和fputs()到file.txt – marnir
@ K-ballo抱歉忘了在代碼中寫這裏,但現在我編輯了。我已經關閉了這兩個文件在這兩種情況下。 –