2011-10-01 42 views
0

與我以前的帖子,但它不是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需要它..

+1

你應該''fclose'這些文件,它應該可能以二進制模式「wb」打開。 –

+0

如果您暫時放一些printf()調用來顯示發生了什麼,可能會更容易發現發生了什麼。數據[d]和密鑰[k],在你之前^ =和fputs()到file.txt – marnir

+0

@ K-ballo抱歉忘了在代碼中寫這裏,但現在我編輯了。我已經關閉了這兩個文件在這兩種情況下。 –

回答

3

您正在使用字符串語義來處理二進制數據。這是行不通的。如果仔細觀察,會發現file.txt示例輸出中的第一個字符是k,它也是該鍵的第一個字符。這意味着您的數據以NUL字節開始,while循環將立即退出。

首先你需要在二進制模式來打開文件:

fp=fopen("key","wb"); 
fq=fopen("file.txt","wb"); 

要寫入密鑰使用

fwrite(key, keylen, 1, fp); 

,然後使用for循環在你的第二個例子,寫入數據。我看不出有什麼問題,你的問題可能只是二進制文本模式。

編輯:嘗試使用hexdump -C file.txt來查看您的文件,而不是貓。

+0

關於空字節的部分可能是他的問題。不過,關於'fopen'的部分可能並不相關(除了可移植性),因爲它看起來像是在Linux或Unix系統上,在這種情況下,「wb」和「w」可能是等價的。 – Dmitri

+0

也許吧。儘管他在加密輸出中確實有'0A'。 –

+0

thnx它幫了很多...... –

0

這裏

while(data[d]) { 
    fputc((int)data[d],fq); 
    d++; 
} 

數據[d]爲0(因爲它是二進制數據),所以它將離開循環 「太」 很快。