5,696,038 members and growing! (11,437 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Bitmaps     Intermediate License: The Code Project Open License (CPOL)

A very simple solution for partial bitmap encryption

By Jim Xochellis

A simple and fast image encryption technique, which facilitates the secure use of external image files in common applications.
VC7.1, C++Windows, Win2K, WinXP, MFC, VS.NET2003, Visual Studio, Dev

Posted: 15 Mar 2006
Updated: 15 Mar 2006
Views: 27,474
Bookmarked: 25 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 5.27 Rating: 4.60 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
5 votes, 35.7%
4
9 votes, 64.3%
5

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 | 
                   LR_CREATEDIBSECTION | LR_DEFAULTSIZE);

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.

Acknowledgments

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.

History

  • 16 March 2006
    • Initial release.

References

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

License

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

About the Author

Jim Xochellis


I live in Greece with my wife and our two daughters. I am a professional software developer since 1999, working mostly with C/C++. My expertise are: C/C++, STL, cross-platform programming (Mac+Win) and software optimization. I am familiar with the MFC, wxWidgets and Cplat GUI frameworks and the Python, Java, Pascal, Fortran, Prolog and Rexx programming languages.
Occupation: Software Developer
Location: Greece Greece

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
Generalhelpmemberwhy you want me sign up21:32 8 May '07  
GeneralRe: helpmemberJim Xochellis5:19 9 May '07  
GeneralHey~memberwe3Guy0:26 6 Jul '06  
GeneralI need your help...memberwe3Guy21:29 13 Jun '06  
GeneralRe: I need your help...memberJim Xochellis19:22 14 Jun '06  
GeneralRe: I need your help...memberwe3Guy23:01 14 Jun '06  
GeneralRe: I need your help...memberJim Xochellis23:53 15 Jun '06  
GeneralRe: I need your help... [modified]memberwe3Guy16:31 18 Jun '06  
GeneralRe: I need your help...memberJim Xochellis4:38 19 Jun '06  
GeneralRe: I need your help...memberwe3Guy16:01 19 Jun '06  
GeneralRe: I need your help...memberJim Xochellis6:01 10 Jul '06  
GeneralGreat job!memberkellyonlyone16:26 15 Mar '06  
GeneralcoolmemberVarindir Rajesh Mahdihar11:10 15 Mar '06  
GeneralRe: coolmemberJim Xochellis11:22 15 Mar '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Mar 2006
Editor: Smitha Vijayan
Copyright 2006 by Jim Xochellis
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project