Thread: Encrypting/Decrypting file holding binary data

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    13

    Encrypting/Decrypting file holding binary data

    How to encrypt/decrypt a file that has binary data in it..

    its a sort of database file. a number of records of fixed length stored in it. i've used fwrite to write each record (structure).

    i tried reading byte by byte and adding a key value to the byte but obviously it doesnt work.

    I know the the file is not human readable anyway but still..i want to put it as an "extra security feature"

    Any other way?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Read this:
    SourceForge.net: Serialization - cpwiki

    It's about exactly what you are doing (serializing data). You probably don't need to change the data at all (post the struct definition here to make sure).

    I wrote that article, so you can ask questions about it. The "tofile" and "fromfile" functions work pretty well as is.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's trivial to get to the parts of the file that is readable ie plain text, either by hexdump or by reading in the file and printing only after a check like this:

    Code:
    if( isprint(line[i]) ){
            //print here
    }

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    13
    my file is not readable as such anyway..

    i am writing simple structures having 3 float values to the file.

    and i can print the values back also

    what i want is that if i enter the WRONG KEY, then it should display the contents, however, garbage contents..

    hope u get what i mean..

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by wayne08 View Post
    my file is not readable as such anyway..
    Yes. I understand EXACTLY what you are talking about and that wiki article is about EXACTLY that. Your file IS readable that way (but not the way Subsonics is on about).

    i tried reading byte by byte and adding a key value to the byte but obviously it doesnt work.
    You have not really explained yourself here, if this is the real issue, why don't you post the code you used so someone can understand what you are referring to? Again, I suspect you do not need to do this at all. READ THE ARTICLE. It will take 5 minutes.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wayne08
    what i want is that if i enter the WRONG KEY, then it should display the contents, however, garbage contents..
    A simple way is to use the help provided by the wiki article that MK27 linked to, except that after you are done, you encrypt the file contents. For the encryption itself, a library like mcrypt could come in handy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Ahhh...that's what "key" is about, sorry . I would just use one for the entire file, as laserlight implies. Post your code wayne.
    Last edited by MK27; 04-20-2010 at 11:23 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by wayne08 View Post
    I know the the file is not human readable anyway but still..i want to put it as an "extra security feature"
    Understand, that I am only responding to this line. I don't know what's in your file, since you failed to mention it. It's should be obvious, but simply writing the file in binary form, isn't giving you any kind of security from prying eyes, that's all.

    Quote Originally Posted by MK27 View Post
    (but not the way Subsonics is on about).
    Yes it is.
    Last edited by Subsonics; 04-20-2010 at 11:42 AM.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    13
    @MK27 - yes i read the article.. that is what i have done. thanks. however, it doesnt say on encrypting the file.

    @laserlight - i read about mcrypt but i'm not sure what i am to do to use it. Is it a library file already present in turbo c or what? i'll look into it anyway..

    @subsonics - i guess i didnt put it that well . sorry for that. nevermind.


    thanks for response guys. i managed to work some other, rather trivial, ways out.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wayne08
    i read about mcrypt but i'm not sure what i am to do to use it. Is it a library file already present in turbo c or what?
    You probably need to download libmcrypt from one of the links listed, learn its API, and then use use it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. reading binary file with different data types
    By larne in forum C Programming
    Replies: 8
    Last Post: 07-29-2008, 10:12 AM
  4. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM