Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

I am confused, how can I encrypt a file using 128 Bit Advanced Encryption Standard?

Do I need only to encrypt the file name and it's content or is there something that I need to do to encrypt it? Is there something that I will need to extract to the file to encrypt it?

For example, I want to encrypt a ms word file or a pdf file.

It is easy to encrypt a file if the file only contains text, but what if there is a image in a file? How do I go about that?

share|improve this question
    
Isn't this more of a programming problem, than a crypto problem? Your question is also pretty vague. For example you didn't specify where the key comes from(A password? Randomly generated per file?, ...), if you need authentication, if you need random access(just reading, or read+write?)... And do you want transparent encryption, or just a program that takes a file, and spits out a new file that's encrypted? –  CodesInChaos Apr 22 '12 at 11:35
    
I don't have a programming problem, I have a problem on how can I encrypt it, what data should I get to the file to encrypt it... –  goldroger Apr 22 '12 at 11:37
    
I don't get how it is easier to encrypt a text file than any other file. A file is just a bunch of bytes. –  CodesInChaos Apr 22 '12 at 12:37
1  
That depends on your programming language/framework. Just use its (binary) file APIs. But you said this isn't a programming problem. –  CodesInChaos Apr 22 '12 at 12:42
1  
Encryption is not like a magic spice you sprinkle on a problem and it becomes secure. It's a low-level tool that gets combined with other tools and techniques to solve specific real-world problems. "I want to encrypt something" is not a specific real-world problem. Also, your question is backwards too, it's like "How can I build a house with a screwdriver?". You should figure out exactly what kind of house you want to build first. Then we can talk about whether a screwdriver is the right tool for the job. AES-128 is a low-level tool, not a high-level design. –  David Schwartz Apr 23 '12 at 3:17
show 3 more comments

2 Answers

up vote 7 down vote accepted

You are right to be confused, because you could just as well have asked "How can I encrypt a file using a CPU that supports xor, shifts and rotates?" The answer is that of course you can, but there is obviously a lot more to it, if you are going to do it right.

AES is just a standard block cipher primitive. The only thing this standard tells you, is how to transform a single 128 bit block using a key, into another 128 bit block. It doesn't tell you where the key is coming from, or how you are going to process any data larger than 128 bits. Those aspects have to be specified as well, and one example would be password based encryption as in PKCS#5 PBES2 with AES-CBC-128 combined with PBMAC1 with HMAC-SHA-256.

When reading the technical specifications of consumer software or business software, it is not that uncommon that the only thing specified is the block cipher primitive, such as "128 bit AES". In my experience this is often due to either of the following reasons:

  • The software developers don't have a clue about cryptography and are actually using AES-ECB-128 with a key that embedded as a constant in the software.
  • The software developers are using a proper file encryption scheme but specify only the block cipher primitive because that's the only buzz word that they thought was necessary for marketing reasons.
  • If the software is based on legacy software that used a scheme with e.g. 3DES, stating that they use "128 bit AES" might simply indicate that they use the same scheme as before, but have just substituted the block cipher primitive.

If you interested in how some existing software is encrypting files, just knowing that the software uses AES-128 is consequently not enough. If you are interested in which schemes and algorithms to look up if you were to write such software yourself, you have to be a lot more specific about your security requirements...

  • What's the relation between the person or entity encrypting the file and the person or entity decrypting the file?
  • Exactly what bad things do you hope to prevent by encrypting the file?
share|improve this answer
    
do you have any idea/suggestion how can I encrypt a file? –  goldroger Apr 22 '12 at 11:34
    
@JohnPaulParreño: I can't give any such suggestion without knowing your exact security requirements. Why do you want to encrypt it? Under exactly what circumstances should it be possible for someone to decrypt it, and under what circumstances should it be impossible? –  Henrick Hellström Apr 22 '12 at 11:39
    
hm, I want to encrypt a examination file In my school where I am studying as part of my thesis, I think it should help the school to protect their examination from leakage. –  goldroger Apr 22 '12 at 11:48
2  
Then why don't you just put the file in an encrypted TrueCrypt container? Or use some other existing crypto program that satisfies your requirements? –  CodesInChaos Apr 22 '12 at 11:56
3  
@JohnPaulParreño: The complete file from the outside is just a stream of bytes. Your encryption program doesn't (shouldn't) care if this originally was a word file with text and images, a plain text file, a video file or something else. You just encrypt the bytes as they come. –  Paŭlo Ebermann Apr 22 '12 at 12:27
show 9 more comments

I'm going to expand on Paŭlo's comment whilst I'm here:

The complete file from the outside is just a stream of bytes. Your encryption program doesn't (shouldn't) care if this originally was a word file with text and images, a plain text file, a video file or something else. You just encrypt the bytes as they come.

What you're not understanding, I think is the concept of a block, or a bit. In classical cryptography, it was common to use data types such as letters and numbers - this is what humans store and process, after all. Statements such as "take A, shift it three letters down the alphabet to D" are things humans can understand.

Computers can understand these too - but they also represent a lot of other types and formats of information - much more than humans. They do this in binary - strings of zeros and ones. Each digit is called a bit, 8 bits is called a byte.

So now we get onto the concept of a block. A block is just a collection of bits, or bytes. A 128-bit block, which AES uses, is 128 bits, or 16 bytes.

Now, just to confuse you, there are several types of AES encryption - 128-bit, 192-bit and 256-bit. These refer to the size of the key - not the input block. That block always, always stays at 128-bit, at least for AES (Rijndael, the algorithm on which it is based, supports more block sizes).

So, when Paŭlo says "take the bytes as they come" you literally read the file block by block - 16 bytes at a time. However, do please keep reading...

You can just encrypt these blocks byte at a time. AES can and will do that and the mode is called electronic code book (ECB). However, now I'm going to translate Hendrik's (very true, +1) statement:

AES is just a standard block cipher primitive. The only thing this standard tells you, is how to transform a single 128 bit block using a key, into another 128 bit block. It doesn't tell you where the key is coming from, or how you are going to process any data larger than 128 bits.

See here's the thing. In our ECB mode, we handle files larger than 128 bits by simply dividing the file up into 128-bit chunks. It turns out this can be quite an insecure way to work, as patterns might emerge in the ciphertext which relate to the file structure.

As such, there are different ways to use a block cipher called Modes of Operation. Hence why Hendrik referred to AES as a primitive - because it only forms part of a larger scheme. There are many modes of operation and not all of them will suit your use cases - but needless to say you probably want one that is not ECB.

These new modes of operation still take files in terms of bytes and bits - sometimes in 128-bit blocks, sometimes bit at a time, depending on the mode in question. The concept of using the computer's native formats (bits and bytes) rather than text, remains the same and thus any file type can be handled.

As Hendrik has also alluded to - the problem of generating a key is also an interesting case. I've seen software developers simply convert a string (in binary terms) straight into a key. This is a really poor way to go about generating a key, since the number of possible keys is much, much more limited than the AES keyspace. I won't say any more here as this is about files - but needless to say it is definitely something to look into.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.