Click here to Skip to main content
13,624,518 members
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 15 Mar 2006
Licenced CPOL

A very simple solution for partial bitmap encryption

, 15 Mar 2006
Rate this:
Please Sign up or sign in to vote.
A simple and fast image encryption technique, which facilitates the secure use of external image files in common applications.

Sample Image - ImgCrypt.jpg

The problem description

It can often be preferable for developers to store application images in separate image files, because it makes the construction and the maintenance of the application considerably easier. For example, using external images usually facilitates the cooperation between the programmer and the GUI designer, accommodates the localization and the use of themes, and also makes the updating procedure easier and faster.

On the other hand, having separate image files makes it easier for unauthorized persons, to change the appearance of the application, which is often unacceptable. Someone could add here that, in the case of the external image files, there is also a danger of stealing the GUI images. In my opinion, this is not an important issue (at least, not for still images), since these images are going to be displayed on the screen eventually, and screen captures are very easy to do. Hence, stealing the GUI images can almost never be avoided.

The proposed solution and the basic design decisions

The most obvious solution in order to use external images and also avoid the dangers described above, is to encrypt the image data. However, encryption will introduce two new problems to the image loading procedure. It will make this procedure much more complicated and slower. Since loading speed and simplicity are very important issues in my case, I have made the following design decisions:

  1. I have picked a relatively fast and simple symmetric encryption algorithm: The BlowFish [1,2] algorithm.
  2. I have decided to actually encrypt only a part of the image data. Namely, the first N consecutive pixel rows of every image will be encrypted, the next M pixel rows will not be encrypted, and then again the next N pixel rows will be encrypted etc. The actual values of N and M will be user defined.
  3. Because of the previous design decision (#2), this solution can only work easily with uncompressed images. Moreover, for the time being, I have only implemented it for DIB Sections, since I personally work with uncompressed BMPs most of the time.

As a result of the above design decisions, the encrypted image will be easily recognizable (only some pixel rows will be encrypted), but it would be difficult to reuse it in another application, and even more difficult to replace or modify it in an existing application.

The code implementation and usage

All my implementation code resides in the CImgCrypt class, which is derived from the CBlowFish class, introduced by George Anescu in another CodeProject article [1]. Compared to the CBlowFish class, CImgCrypt provides two additional public methods that can be used in order to encrypt and decrypt DIB Sections.

bool CImgCrypt::EncryptDIBSection(HBITMAP hbitmap, 
     unsigned encryptedHeight, unsigned unencryptedHeight)
bool CImgCrypt::DecryptDIBSection(HBITMAP hbitmap, 
     unsigned encryptedHeight, unsigned unencryptedHeight)

Specifically, the CImgCrypt::EncryptDIBSection and CImgCrypt::DecryptDIBSection methods encrypt or decrypt, respectively, the DIB Sections that correspond to their hbitmap parameter, processing encryptedHeight pixel rows, and skipping unencryptedHeight pixel rows by turns. Both these methods return true on success, and false otherwise. As a simple usage example, I demonstrate below, a piece of code that loads and decrypts a BMP file:

CImgCrypt imgCryptEngine("blowfish-key", 12);

HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, "image.bmp", 
                   IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | 

imgCryptEngine.DecryptDIBSection(hBitmap, 3, 6);

I hope that this work will be helpful for you. In the download section, you will also find a demo application that provides a more "live" demonstration of what I have described in this article. Thank you for reading this.


I have based this work on the BlowFish implementation of George Anescu [1], and I have also used the CDIBSectionLite class of Chris Maunder [3] in my demo application. Thanks a lot guys!

Future plans

  • Benchmark many Blowfish implementations, and then use the faster one.
  • Extend this work, in order to be applicable on other image formats as well, besides the uncompressed BMPs.


  • 16 March 2006
    • Initial release.


  1. A C++ Implementation of the BlowFish Encryption/Decryption method.
  2. The BlowFish Encryption Algorithm.
  3. A DIBSection wrapper for Win32 and WinCE.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jim Xochellis
Software Developer
Greece Greece
I live in Greece with my wife and our two daughters. I am a professional software developer since 1999, using mostly C/C++ in my work.

My main expertise are: C/C++, STL, software optimization, generic programming and debugging. I am also very experienced in client–server programming, communications, concurrent programming, software security and cryptography. Finally, in my early professional years, I have worked a lot on cross-platform programming (Mac+Win).

I am familiar with the MFC, wxWidgets and Cplat GUI frameworks and the Python, Java, Pascal, Fortran, Prolog and Rexx programming languages.

You may also be interested in...


Comments and Discussions

GeneralThanks Pin
o3yoon23-Apr-09 19:12
membero3yoon23-Apr-09 19:12 
GeneralRe: Thanks Pin
Jim Xochellis27-Apr-09 3:53
memberJim Xochellis27-Apr-09 3:53 
Generalhelp Pin
why you want me sign up8-May-07 20:32
memberwhy you want me sign up8-May-07 20:32 
GeneralRe: help Pin
Jim Xochellis9-May-07 4:19
memberJim Xochellis9-May-07 4:19 
GeneralHey~ Pin
we3Guy5-Jul-06 23:26
memberwe3Guy5-Jul-06 23:26 
GeneralI need your help... Pin
we3Guy13-Jun-06 20:29
memberwe3Guy13-Jun-06 20:29 
GeneralRe: I need your help... Pin
Jim Xochellis14-Jun-06 18:22
memberJim Xochellis14-Jun-06 18:22 
GeneralRe: I need your help... Pin
we3Guy14-Jun-06 22:01
memberwe3Guy14-Jun-06 22:01 
GeneralRe: I need your help... Pin
Jim Xochellis15-Jun-06 22:53
memberJim Xochellis15-Jun-06 22:53 
GeneralRe: I need your help... [modified] Pin
we3Guy18-Jun-06 15:31
memberwe3Guy18-Jun-06 15:31 
GeneralRe: I need your help... Pin
Jim Xochellis19-Jun-06 3:38
memberJim Xochellis19-Jun-06 3:38 
GeneralRe: I need your help... Pin
we3Guy19-Jun-06 15:01
memberwe3Guy19-Jun-06 15:01 
GeneralRe: I need your help... Pin
Jim Xochellis10-Jul-06 5:01
memberJim Xochellis10-Jul-06 5:01 
GeneralGreat job! Pin
kellyonlyone15-Mar-06 15:26
memberkellyonlyone15-Mar-06 15:26 
Generalcool Pin
Varindir Rajesh Mahdihar15-Mar-06 10:10
memberVarindir Rajesh Mahdihar15-Mar-06 10:10 
GeneralRe: cool Pin
Jim Xochellis15-Mar-06 10:22
memberJim Xochellis15-Mar-06 10:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180712.1 | Last Updated 15 Mar 2006
Article Copyright 2006 by Jim Xochellis
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid