将数据写入文件但文件仍包​​含 NULL

发布于 2024-12-07 13:39:32 字数 3103 浏览 0 评论 0原文

与我之前的帖子相关,但它不是重复的。现在我已经尝试了一些东西,

在这里我向您询问代码中的逻辑错误。

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

输出:

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 $

密钥被打印到文件,但不打印数据。

现在,当我稍微修改代码时:

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);

}

现在密钥和数据都被打印...虽然数据不完全正确(但可以写入文件)

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 $

对 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;
  }

以下数据包描述了我实际需要写入文件的数据...

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

请帮助我...我在上一篇文章中还没有收到任何令人满意的实现,所以仍在尝试自己动手。真的需要它..

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 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;
  }

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 ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玉环 2024-12-14 13:39:32

您正在使用字符串语义来处理二进制数据。那是行不通的。如果仔细观察,您会发现 file.txt 示例输出中的第一个字符是 k,这也是键的第一个字符。这意味着您的数据以 NUL 字节开头,并且 while 循环将立即退出。

首先,您需要以二进制模式打开文件:

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

要写入密钥

fwrite(key, keylen, 1, fp);

,然后使用第二个示例中的 for 循环写入数据。我看不出有什么问题,你的问题可能只是二进制与文本模式。

编辑:尝试使用 hexdump -C file.txt 来查看您的文件而不是 cat。

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.

挥剑断情 2024-12-14 13:39:32

这里

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

data[d] 是 0(因为它是二进制数据),所以它会“太”快地离开循环。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文