title: How to read pkcs#7 file properly
body: <p>I have a file with detached pkcs#7 signature of size 267 bytes. I create it after executing </p>
  
  <pre><code>CryptSignMessage(
      &amp;SigParams,
      TRUE,
      1,
      MessageArray,
      MessageSizeArray,
      pbSignedMessageBlob,
      &amp;cbSignedMessageBlob)
  </code></pre>
  
  <p>function and then I write content of pbSignedMessageBlob:</p>
  
  <pre><code>pbSignedMessageBlob[cbSignedMessageBlob] = '\0';
   FILE *file;
   file = fopen("c:\\path\\sign_pkcs7.pk7", "wb");
   fwrite(pbSignedMessageBlob, sizeof(BYTE), cbSignedMessageBlob, file);
   fclose(file);
  </code></pre>
  
  <p>Then I try to verify this signature and read this file:</p>
  
  <pre><code>BYTE *pkcsContent;
  FILE *file3;
  file3 = fopen("c:\\users\\timur\\desktop\\sign_pkcs7.pk7", "rb");
  pkcsContent = MyReadFile(file3, cbArray);
  BYTE* MyReadFile(FILE *f, DWORD &amp;len)
  {
      size_t result;
      BYTE *buffer;
      if (f == NULL) 
      {
          fputs ("File error", stderr); 
          exit (1);
      }
  
  fseek (f, 0, SEEK_END);
  len = ftell (f);
  rewind (f);
  
  //buffer = (BYTE*) malloc (sizeof(char)*lSize);
  buffer = new BYTE[len + 1];
  if (buffer == NULL) 
  {
      fputs ("Memory error", stderr); 
      exit (2);
  }
  
  result = fread (buffer, 1, len, f);
  if (result != len) 
  {
      fputs ("Reading error",stderr); 
      exit (3);
  }
  buffer[len] = '\0';
  
  fclose (f);
  
      return buffer;
  }
  </code></pre>
  
  <p>But the file content I get into pkcsContent is only 37 bytes length.
  I also tried to manage files using std::fstream, but result is the same. Maybe the way I'm writing file to disk is wrong?</p>