开发者

Writing data to file but file still contain NULL

Related to my previous post but its not a duplicate of that.Now I have tried something and

Here I am asking you about the logical error in code.

/*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)
}

Output :

udit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $ cat key 
kaheudit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $ cat file.txt 
udit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $

Key gets printed to file but not the data.

Now when I slightly modify the code :

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 key as well as data gets printed...although data is not exactly correct(but it is able to be written down into the file)

udit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $ cat key 
kaheudit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $ cat file.txt 
kthpOWWkahe;c��"�he
kajcudit@udit-Dabba ~/Downloads/sendip-2.5-mec-2/mec $

The call to the crypt function is as follows -

  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 everyt开发者_Python百科hing 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;
  }

The following packet describe what data I actually need to write down to the file...

udit@udit-Dabba ~/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   ...@
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

Please help me.... I haven't receiver any satisfactory implementation on my previous post still so trying my own hand.Really need it ..


You're using string semantics to handle binary data. That's not going to work. If you look closely you'll see that the first character in your file.txt example output is k, which is also the first character of the key. That means your data is starting with a NUL byte and the while loop will instantly exit.

First of all you need to open the files in binary mode:

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

To write the key use

fwrite(key, keylen, 1, fp);

and then use the for loop in your second example to write the data. I can't see anything wrong with that one, your problem might simply have been the binary vs text mode.

Edit: Try using hexdump -C file.txt to view your file instead of cat.


Here

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

data[d] is 0 (as it is binary data), so it will leave the loop "too" soon.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜