how to encrypt a C data and write a bin file and read a bin at run time and decrypt C data

Discussion in 'C Programming' started by sweety, Feb 5, 2006.

  1. sweety

    sweety Guest

    Dear All,

    How to encrypt a C data file and make binary file and then have to read
    a bin file at run time and decrypt the file and have to read the data.

    Any help to achive this pls. Would be great if any sample source code
    provided.

    Thanks,
    Sweety
     
    sweety, Feb 5, 2006
    #1
    1. Advertising

  2. Re: how to encrypt a C data and write a bin file and read a bin atrun time and decrypt C data

    sweety wrote:
    > Dear All,
    >
    > How to encrypt a C data file and make binary file and then have to read
    > a bin file at run time and decrypt the file and have to read the data.
    >
    > Any help to achive this pls. Would be great if any sample source code
    > provided.
    >
    > Thanks,
    > Sweety
    >


    I don't really know what you mean. Could you give me an example, or tell
    me about the overall goal of acutally doing such a thing?
     
    Patrick M. Rutkowski, Feb 5, 2006
    #2
    1. Advertising

  3. sweety

    Michael Mair Guest

    Re: how to encrypt a C data and write a bin file and read a bin atrun time and decrypt C data

    sweety wrote:
    > Dear All,
    >
    > How to encrypt a C data file and make binary file and then have to read
    > a bin file at run time and decrypt the file and have to read the data.
    >
    > Any help to achive this pls. Would be great if any sample source code
    > provided.


    fopen() the file, in text or binary mode, as appropriate.
    If your encryption algorithm depends on context w.r.t. the
    input, read in the whole file (use either fread() or getc()
    and malloc()/realloc() for the buffer).
    Encrypt.
    fopen() the output file in "b" binary mode. Write the output
    to it. fclose() input and output file.

    Essentially the same applies to the decryption part.

    If you provide us with your best shot at it (minus the
    encryption or decryption part), we may help you further
    with the standard C part of it.
    If you want some sort of dummy encryption/decryption for the
    code you post, use a simple one, e.g. Caesar cipher.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Feb 5, 2006
    #3
  4. sweety

    Malcolm Guest

    "sweety" <> wrote
    > How to encrypt a C data file and make binary file and then have to read
    > a bin file at run time and decrypt the file and have to read the data.
    >
    > Any help to achive this pls. Would be great if any sample source code
    > provided.
    >


    If you use the XOR operation, you can apply the operation once to flip
    arbitrary bits, and twice to retrieve the original information.

    So a simple scheme would XOR everything with a constant value, say 0x55.

    This is too easy to break - simply by counting the commonest character you
    can get the spaces, then the 'e's, then the 't's, assuming you are crypting
    English text.

    So an improvement is to have a pseudo-random number generator. You seed it
    with your secret seed, and then XOR with the numbers that come out. This is
    much harder to break.

    To do a really professional job, you need to implement a completely
    differenent schme, like RSA, which is built on the fact that it is
    impossible to factorise large numbers in reasonable time, if the factors are
    also large, but it is very easy to create such numbers by multiplying
    together two large primes.
     
    Malcolm, Feb 5, 2006
    #4
  5. In article <ds5s2p$cp$-infra.bt.com>,
    Malcolm <> wrote:
    ....
    >To do a really professional job, you need to implement a completely
    >differenent schme, like RSA, which is built on the fact that it is
    >impossible to factorise large numbers in reasonable time, if the factors are
    >also large, but it is very easy to create such numbers by multiplying
    >together two large primes.


    I see you've been talking to Bill Gates...
     
    Kenny McCormack, Feb 5, 2006
    #5
  6. On 4 Feb 2006 23:12:09 -0800, in comp.lang.c , "sweety"
    <> wrote:

    >Dear All,
    >
    >How to encrypt a C data file and make binary file and then have to read
    >a bin file at run time and decrypt the file and have to read the data.


    This is an algorithms or cryptology question. Why not ask in more
    appropriate groups?
    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Feb 5, 2006
    #6
  7. Re: how to encrypt a C data and write a bin file and read a bin atrun time and decrypt C data

    "Malcolm" <> writes:
    > "sweety" <> wrote
    >> How to encrypt a C data file and make binary file and then have to read
    >> a bin file at run time and decrypt the file and have to read the data.
    >>
    >> Any help to achive this pls. Would be great if any sample source code
    >> provided.
    >>

    >
    > If you use the XOR operation, you can apply the operation once to flip
    > arbitrary bits, and twice to retrieve the original information.
    >
    > So a simple scheme would XOR everything with a constant value, say 0x55.
    >
    > This is too easy to break - simply by counting the commonest character you
    > can get the spaces, then the 'e's, then the 't's, assuming you are crypting
    > English text.
    >
    > So an improvement is to have a pseudo-random number generator. You seed it
    > with your secret seed, and then XOR with the numbers that come out. This is
    > much harder to break.
    >
    > To do a really professional job, you need to implement a completely
    > differenent schme, like RSA, which is built on the fact that it is
    > impossible to factorise large numbers in reasonable time, if the factors are
    > also large, but it is very easy to create such numbers by multiplying
    > together two large primes.


    There are plenty of commercial and freeware encryption programs out
    there. If you try to implement your own, it will almost certainly be
    breakable *unless* you know a lot more about encryption than, for
    example, I do. For example, depending on the rand() function provided
    with your C implementation is unlikely to give you good encryption.

    sci.crypt is a good place to ask for more information.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 6, 2006
    #7
  8. sweety

    Malcolm Guest

    "Keith Thompson" <> wrote
    > There are plenty of commercial and freeware encryption programs out
    > there. If you try to implement your own, it will almost certainly be
    > breakable *unless* you know a lot more about encryption than, for
    > example, I do. For example, depending on the rand() function provided
    > with your C implementation is unlikely to give you good encryption.
    >

    Let's say we are terrorists planning an attack on America.

    Now we implement a simple scheme to encrypt our emails.
    If the CIA, FBI, or whatever know who we are, of course they will shell out
    twenty dollars an hour for a programmer to break our rand().
    However what if there are hundreds of thousands of moon-worshipers in the
    world, all of whom hate the USA? They can't afford twenty dollars a shot for
    every message.

    Probably they just run intercepted emails through standard decrypts, and
    pick out key words and phrases, like "moon is auspicious for an attack" .

    So we have a substantial degree of safety.
     
    Malcolm, Feb 6, 2006
    #8
  9. OT: Encryption (was Re: how to encrypt a C data and write a binfile and read a bin at run time and decrypt C data)

    "Malcolm" <> writes:
    > "Keith Thompson" <> wrote
    >> There are plenty of commercial and freeware encryption programs out
    >> there. If you try to implement your own, it will almost certainly be
    >> breakable *unless* you know a lot more about encryption than, for
    >> example, I do. For example, depending on the rand() function provided
    >> with your C implementation is unlikely to give you good encryption.
    >>

    > Let's say we are terrorists planning an attack on America.
    >
    > Now we implement a simple scheme to encrypt our emails.
    > If the CIA, FBI, or whatever know who we are, of course they will shell out
    > twenty dollars an hour for a programmer to break our rand().
    > However what if there are hundreds of thousands of moon-worshipers in the
    > world, all of whom hate the USA? They can't afford twenty dollars a shot for
    > every message.
    >
    > Probably they just run intercepted emails through standard decrypts, and
    > pick out key words and phrases, like "moon is auspicious for an attack" .
    >
    > So we have a substantial degree of safety.


    You're assuming it costs $20 per message. Given a weak encryption
    method, it's more likely to cost, say, $20 to break the encryption,
    then a fraction of a cent (if that much) to decrypt each message.

    If you care *at all* about security, there are plenty of freely
    available encryption programs that are almost certainly many orders of
    magnitude more difficult to crack than anything I, for one, could come
    up with on my own.

    Why would anyone settle for a "substantial degree of safety" when a
    much higher degree of safety is free?

    If you just want to play around, of course, there's nothing wrong with
    trying to write your own encryption code -- just don't depend on it
    unless you happen to be an expert.

    And this is entirely off-topic for comp.lang.c. You'll find the
    experts on this topic (definitely not including me) in sci.crypt.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 6, 2006
    #9
  10. sweety said:

    > Dear All,
    >
    > How to encrypt a C data file


    Putting the word "C" into an article doesn't make it topical in comp.lang.c!

    > and make binary file and then have to read
    > a bin file at run time and decrypt the file and have to read the data.


    The answer to this question depends on your threat model, and on the balance
    you are prepared to strike between performance, security, and key
    distribution, and on your skill level with C, and so on and so forth.

    If you want something that'll stop a major government from reading your
    data, you'd be well-advised to use a well-analysed algorithm such as
    Rijndael (AES) or TwoFish.

    If you just want something that'll stop your kid sister from reading your
    data, a simple Feistel network in CBC mode with a primitive round function
    based on a 64-bit key is probably sufficient.

    Caveat: if your kid sister works for a major government in a cryptology
    capacity, go for the first option, not the second.

    I recommend "Applied Cryptography", 2nd edition, by Bruce Schneier, or
    "Handbook of Applied Cryptography" (legally available online for free, from
    <http://www.cacr.math.uwaterloo.ca/hac/> for some useful insights into
    designing cryptographical algorithms - but please remember that no
    algorithm you can design yourself is going to give a clueful user the same
    "feel safe" factor as a properly-analysed algorithm designed by experts.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Feb 7, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    512
    Jonathan Allen
    Sep 25, 2004
  2. sweety
    Replies:
    1
    Views:
    861
    osmium
    Feb 9, 2006
  3. Replies:
    0
    Views:
    989
  4. anne001
    Replies:
    1
    Views:
    586
  5. Replies:
    1
    Views:
    460
    Daniel Martin
    Jun 16, 2007
Loading...

Share This Page