# How are non-text files encrypted?

I thought encryption algorithms (like AES) are used to encrypt texts. So, how are files or folders (containing pictures, videos, etc.) encrypted? What is it that encryption algorithms exactly encrypt?

• I could find some useful information here: crypto.stackexchange.com/questions/2423/… – hamedb71 Nov 12 '14 at 13:28
• Modern crypto algorithms all operate on bits. Even PBKDF2 - which is used to derive keys from passwords - operates on bits instead of text. In modern crypto, the terms plaintext and ciphertext are rather confusing as neither one of them are actually text, you actually need character-encoding such as UTF-8 to convert from text to binary and encoding schemes such as base 64 to convert binary to text. So in that sense encrypting a file is actually easier as no encoding needs to be applied. – Maarten Bodewes Nov 12 '14 at 14:29
• @owlstead. I steel don't know how to access to a file bits? – hamedb71 Nov 12 '14 at 14:42
• You have to open it in binary mode. How to do this depends on your language/runtime environment. Most runtimes should provide tutorials for this; opening files is rather common :) Note that in practice crypto is petformed on sets of 8 bits called octets or bytes. – Maarten Bodewes Nov 12 '14 at 14:45
• Can use Hex Editor for this purpose? – hamedb71 Nov 12 '14 at 14:52

Encryption algorithms encrypt bits. All digital media (text, pictures, videos, etc) is stored as bits at the lowest level, therefore, they can all be encrypted.

• So how can i access to those bits? – hamedb71 Nov 12 '14 at 13:37
• In any programming language, you can read them in to an array. – mikeazo Nov 12 '14 at 14:01
• Yes, reading them in an array is one option. For larger files binary streams and memory mapping may be more efficient though. – Maarten Bodewes Nov 12 '14 at 14:31

All files (text, video, whatever) consist of bits. The general procedure with symmetric encryption algorithms (such as AES) is they generate a stream of pseudo random bits, which is derived from a key or password, and then they mangle (typically XOR) the original data with the random data. Thus resulting in new data that appears to be totally random.

Without the key or password, there is no way to predict or generate the peuso random data, so it's also impossible to distinguish the original bits from the encrypted result.

Decrypting works the same way: when you enter the valid key, the same pseudo random mangle data is generated, so it can undo the mangling (e.g. XOR again) and restore the original data.

It makes no difference whatsoever whether you encrypt text, or pictures, or video, or data that might even be already encrypted otherwise. Data is just data: a fancy collection of ones and zeroes.

• The key stream is "only" generated for stream ciphers and block ciphers used in a mode of operation (e.g. CTR) that produces a key stream. The still very common CBC mode of encryption encrypts by splitting the plaintext/ciphertext in blocks and does not use XOR for the last step. Otherwise the answer seems well explained to me. – Maarten Bodewes Nov 12 '14 at 14:34

Others have already given the answer, so the following just uses slightly different words that might be helpful.

Files on a computer consists of bytes. Bytes are made up of bits. A bit is a 0 or a 1. A byte consists of 8 bits. That is, a byte is a string of 8 1's and 0's (a binary string). Most encryption algorithms works of the bits. So if you have a file, then the algorithm treats the file as a binary string.

If you want to find the string of bits or bytes making up a file you can use a Hex editor. If you are on Windows I can recommend XVI32. You simply open the file with the program and see the bytes. Now since you are using a Hex editor, the bytes will be written as two hexadecimal digits. For example, the byte E2 corresponds to the dcimal value 226. As a binary string this is 11100010. There are a bunch of applications online that will give you this conversion and there are many good explanations of how this works.

Now AES is a 128 bit block cipher. That means it encrypts the file is 128 bis blocks. 128 bits is 16 bytes (128/8 = 16). So you take chucks of 16 bytes and encrypts them. There is a lot more to how exactly this is done. There are, for example, different modes of operation.