Click here to Skip to main content
15,889,462 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Visual Image Cryptography Generator

Rate me:
Please Sign up or sign in to vote.
4.69/5 (7 votes)
6 Mar 2014CPOL1 min read 46.1K   4.9K   37   7
Visual cryptography is a cryptographic technique which allows visual information (pictures, text, etc.) to be encrypted in such a way that the decryption can be performed by the human visual system, without the aid of computers.

Image 1

Image 2

After combining two images

Introduction

Merry Christmas! Before writing this article, I wrote an article about Visual cryptography. But it's in text base, that means when you enter text and it will generate a visual encrypted images for you.

Recently, I received an email for if it can do in image base, select image and return encrypted images. And answer is of cause.

Background

Visual cryptography was introduced by Naor and Shamir at EUROCRYPT '94. They asked the following intriguing question: is it possible to devise a secret sharing scheme in which an image can be reconstructed "visually" by superimposing two shares? Each share would consist of a transparency, made up of black and white pixels. (Note that it would be more accurate to say "transparent" rather than "white".) Examination of one share should reveal no information about the image.

For more details, please read this.

Using the Code

The main difference between Image and Text is that you can control the text background in Black and White but Image contains Color, so first what we need is to convert the color into Black and White:

C#
private Bitmap ConvertToBackAndWhite(Bitmap source)
{
    int sourceWidth = source.Width;
    int sourceHeight = source.Height;
 
    Bitmap result = new Bitmap(sourceWidth, sourceHeight);
    double mid = 255d * (1d / 2d);
 
    for (int x = 0; x < sourceWidth; x++)
    {
        for (int y = 0; y < sourceHeight; y++)
        {
            Color c = source.GetPixel(x, y);
            c = (Average(c.R, c.G, c.B) > mid) ? Color.Empty : Color.Black;
            result.SetPixel(x, y, c);
        }
    }
 
    return result;
} 

or you can change the color into gray scale

C#
public Bitmap ConvertToGrayscale(Bitmap source)
{
    int sourceWidth = source.Width;
    int sourceHeight = source.Height;
    Bitmap result = new Bitmap(sourceWidth, sourceHeight);
    for (int y = 0; y < sourceHeight; y++)
    {
        for (int x = 0; x < sourceWidth; x++)
        {
            Color c = source.GetPixel(x, y);
            int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
            result.SetPixel(x, y, Color.FromArgb(luma, luma, luma));
        }
    }
    return result; 
} 

and use my halftone class to convert it into black and white, read more

C#
List<Color> palette = new List<Color>(); //Color Palette
palette.Add(Color.FromArgb(0, 0, 0));
palette.Add(Color.FromArgb(255, 255, 255));
using (Bitmap gSource = ConvertToGrayscale(source))
{
    using (Bitmap bwSource = FloydSteinbergDither.Process(gSource, palette.ToArray()))
    {
        m_EncryptedImages = GenerateImage(bwSource);
    }
}

After converting the image to black and white, the other is the same as text encryption. And I'm not describing the details here.

Enjoy the article and Christmas Holidays.

Cheers!

History

  • 26th December, 2011: Initial post
  • 6th March 2014: Update to grayscale and halftone support

License

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


Written By
Architect MouxIdea Limited
Hong Kong Hong Kong
1981 Born in Hong Kong
1996 Become Badminton Trainer
1997 Hong Kong's Return to China
1998 The Year After Hong Kong's Return to China
1999 The Year Before Millennium
2000 First touch of programming - ASP(guestbook)
2001 Outstanding Student Award - Computing Department
2002 Xcellence Developer Awards - Best Graphical Focused Application(Game) Award
2003 Microsoft MVP - .NET
2004 Be lost in Technology
2005 Microsoft MVP - C#
2006 Microsoft MVP - C#
2007 Getting Marry - Cheers~
2008 Microsoft MVP - C#
2009 Microsoft MVP - C#
2010 Microsoft MVP - C#
2011 Start my software hut

http://www.csharpfans.com


http://www.bugyiu.com


http://www.mouxidea.com

Comments and Discussions

 
QuestionHow to save a 3 rd generated image Pin
Ganga Lagshetti8-Mar-19 20:01
Ganga Lagshetti8-Mar-19 20:01 
QuestionVisual Cryptography Pin
Member 1187135416-Feb-19 5:19
Member 1187135416-Feb-19 5:19 
QuestionCode description needed Pin
Member 1307693622-Mar-17 17:13
Member 1307693622-Mar-17 17:13 
AnswerRe: Code description needed Pin
Jacky Yiu10-Apr-17 21:10
Jacky Yiu10-Apr-17 21:10 
QuestionPlease help Pin
koznov12314-Jan-12 19:22
koznov12314-Jan-12 19:22 
AnswerRe: Please help Pin
Member 1000475323-Oct-13 15:23
Member 1000475323-Oct-13 15:23 
GeneralNice to read the article Pin
sudhansu_k12326-Dec-11 22:40
sudhansu_k12326-Dec-11 22:40 

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.