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

S

sweety

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
 
P

Patrick M. Rutkowski

sweety said:
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?
 
M

Michael Mair

sweety said:
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
 
M

Malcolm

sweety said:
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.
 
K

Kenny McCormack

Malcolm said:
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...
 
M

Mark McIntyre

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
 
K

Keith Thompson

Malcolm said:
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.
 
M

Malcolm

Keith Thompson said:
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.
 
K

Keith Thompson

Malcolm said:
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.
 
R

Richard Heathfield

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top