Score:2

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

in flag

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)?

Paul Uszak avatar
cn flag
You mean to only encrypt the raster, so that the entire file still remains a valid .BMP?
akez avatar
in flag
@Paul Uszak, yes exactly.
Score:1
in flag

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
head -c 54 tux.bmp > tux_head.part
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"
cp tux_head.part encrypted_tux.bmp
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

enter image description here

The output image

enter image description here

Paul Uszak avatar
cn flag
Needs `in` to be `-in`.
Paul Uszak avatar
cn flag
And there's an oddness. Have you tried this? Mine appears with a green tinge. Is that a 'feature' of the BMP format?
kelalaka avatar
in flag
@PaulUszak I've corrected, tested, and added the result into the answer, Could you check with your file?
Paul Uszak avatar
cn flag
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.
kelalaka avatar
in flag
Interesting. I've used a viewer, now tested with GIMP, too. It is working.
Paul Uszak avatar
cn flag
https://pasteboard.co/K6biu3Y.bmp done as `openssl enc -aes-128-ctr -e -in pic_data.part -out aes-ctr-enc-data.part -K "aaaaaa" -iv "bbbbbb"`
kelalaka avatar
in flag
Did you combine the header part and the encrypted raster?
Paul Uszak avatar
cn flag
Yes. Anyway, viewers wouldn't display a raw data raster without header information. You'd have to import raw, set dimensions and assign a format and palette. It's something I've done by GIMPing. I'm special that way... The chat bot is shouting at me.
akez avatar
in flag
@Paul Uszak Chacha20 is using 128-bit constant, a 256-bit key, a 32-bit counter, and a 96-bit nonce. Which means, it will increase the file size of the file, but that's fine with me. The problem is is it still possible to display the ciphertext as a raster bitmap?
kelalaka avatar
in flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.