By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Feb 5 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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?
Feb 5 '06 #2

P: n/a
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.
Feb 5 '06 #3

P: n/a

"sweety" <ur********@gmail.com> 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.

Feb 5 '06 #4

P: n/a
In article <ds*********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com>,
Malcolm <re*******@btinternet.com> 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...

Feb 5 '06 #5

P: n/a
On 4 Feb 2006 23:12:09 -0800, in comp.lang.c , "sweety"
<ur********@gmail.com> 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 =----
Feb 5 '06 #6

P: n/a
"Malcolm" <re*******@btinternet.com> writes:
"sweety" <ur********@gmail.com> 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) ks***@mib.org <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.
Feb 6 '06 #7

P: n/a
"Keith Thompson" <ks***@mib.org> 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.

Feb 6 '06 #8

P: n/a
"Malcolm" <re*******@btinternet.com> writes:
"Keith Thompson" <ks***@mib.org> 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) ks***@mib.org <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.
Feb 6 '06 #9

P: n/a
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)
Feb 7 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.