# It possible to encrypt .bmp bitmap files using Stream Cipher Chacha20?

Maybe this sounds like a stupid question. I'm sorry for that.

I just wanted to know is it possible to encrypt a bitmap image file like *.BMP, and generate the ciphertext which is an obscure bitmap image where each pixel is random (The image maybe would looks like an analog TV without an antenna)?

• You mean to only encrypt the raster, so that the entire file still remains a valid .BMP? Jun 11 '21 at 21:04
• @Paul Uszak, yes exactly.
– akez
Jun 12 '21 at 0:50

Yes, it is possible to encrypt a BMP image with any stream cipher.

First of all, you need to cut the first 54-byte of the BMP image into another file. At this point, the data part begins. Then the rest can be encrypted with any stream cipher. After that prepend the header to the encrypted file.

Here the example with OpenSSL. I will use AES with CTR mode where CTR mode turns any block cipher into a stream cipher.

#/bin/bash
#original file is tux.bmp
tail -c +55 tux.bmp > tux_data.part
openssl enc -aes-128-ctr -e -in tux_data.part -out aes-ctr-enc-data.part -K "01234567890123450123456789012345" -iv "00000000000000010000000000000001"
cat aes-ctr-enc-data.part >> encrypted_tux.bmp


I've used AES-CTR since OpenSSL has ChaCha20-Poly1305 as a cipher suite that is an authenticated encryption. It will add tag, so it is not useful here.

The input image

The output image

• Needs in to be -in. Jun 11 '21 at 21:36
• And there's an oddness. Have you tried this? Mine appears with a green tinge. Is that a 'feature' of the BMP format? Jun 11 '21 at 21:39
• @PaulUszak I've corrected, tested, and added the result into the answer, Could you check with your file? Jun 11 '21 at 22:19
• Nope. Just did it on Tux and it comes out with the same green tinge, not the style you have :-( I suspect that it's compatibility options with GIMP as I used that to create my BMPs. Encryption works perfectly though :-) I'll leave it here. Good answer. Jun 11 '21 at 23:18
• It will increase the file size to only 96-bit, nothing more. My example already displays the raster as an image, did your check? One can easily use programming to achieve this. Jun 12 '21 at 7:48