Thread: Binary file handling

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    2

    Lightbulb Binary file handling

    Hello everyone and it's nice to be here!
    I have a rather complex task (for me) and I'm new to C. I worked with the language the past month but lets just say C is not trivial (for me). What I have to do is the following: four functions and they all deal with a byte file that contains several information. I started programming and I was able to achieve some parts but now I'm running into some problems and I need some help from more experienced programmer.

    The function takes a random file n and does more or less this:
    • Apply encoding on n and save n'
    • Create random numbers and a key and encrypt all of it and append it to n'
    • Create a mac of the current file and append it to the rest


    Right now I'm appending the bytes by just using fwrite. BUT how will I be able to retrieve the information again in a different function without knowing the size of the parts? Is there a convenient way to write a struct and parse it easily back?

    Further, I'm using libgcrypt (GnuPG) for generating values, hashing, and encryption. Besides the lack of real examples, I'm struggling with the debugging. I'm using notepad++ to view (or should I say explore) my created files. Is it possible to print char* in hex and view files in the same way? That would make it (hopefully) easier!

    Last but not least: Like I said I'm new to C; therefore, the concept of memory management is new to me. Most of the time I have to use char* to point to the file content, keys, hashs etc.. I always allocate memory and stuff, but reading about the different register sizes and different platforms I'm scared that my code will not work on another machine. Are there guidelines or things I have to consider to make my code portable?

    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    For an encrypted file, it seems unusual to put the key information at the end of the file instead of the beginning of the file. To get the size of a file, you can do "seek" (fseek) to end of file, then "tell" (ftell) to get the position of the end of the file, then a "seek" back to the beginning of the file to continue. Assuming the key information is fixed in size, then you could seek to the end of the file minus the size of the key information, then save that position as the logical end of file, and read the key information, then seek to the beginning of the file.

    If the file is not too big (< 512MB), you can probably read all of the file into memory and have enough memory for a second copy (if you can allocate 1GB of memory).

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    2
    Okay maybe that part is not clear: The program I write is a PoC of an algorithm and in the second step I encrypt one key and random numbers with another key, which I store locally.
    Write now i'm testing with smaller files but it should work with bigger files too. But I will get to that later!
    How about the hex explorer? Made anyone some experience with such a program?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-07-2013, 04:33 PM
  2. Replies: 6
    Last Post: 12-06-2013, 11:39 PM
  3. Binary File handling
    By codec_supreme in forum C Programming
    Replies: 10
    Last Post: 11-05-2009, 02:22 AM
  4. Binary data handling
    By maverickbu in forum C Programming
    Replies: 1
    Last Post: 06-26-2007, 01:14 PM
  5. File I/O both ASCII and binary handling in C++
    By random in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2005, 04:53 AM