Click here to Skip to main content
13,258,055 members (50,872 online)
Click here to Skip to main content
Add your own
alternative version

Stats

70.6K views
6.5K downloads
41 bookmarked
Posted 23 Oct 2007

Visual Cryptography Generator

, 23 Oct 2007
Rate this:
Please Sign up or sign in to vote.
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

Screenshot - codeproject0.png

Image 2

Screenshot - codeproject1.png

After Combining 2 Images

Screenshot - codeproject.png

Introduction

I should say "Thank you" to tectures, I write this article because of his idea.

Before writing this article, I've written an article about alpha in PNG to make a magic card for friends and tectures asked me what's the different between my program and Visual cryptography. Actually, in that article I didn't think about encrypting anything inside the image, it just did a simple track and made your friends happy.

And I found that no one had written this before, so I've made a simple program to represent this concept.

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

Two constant variables that need to be defined are as follows:

private Size IMAGE_SIZE = new Size(437, 106); //Image size
private const int GENERATE_IMAGE_COUNT = 2; //How many images you would like to generate

The following function generates the shared image:

private Bitmap[] GenerateImage(string inputText)
{
    Bitmap finalImage = new Bitmap(IMAGE_SIZE.Width, IMAGE_SIZE.Height);
    Bitmap tempImage = new Bitmap(IMAGE_SIZE.Width / 2, IMAGE_SIZE.Height);
    Bitmap[] image = new Bitmap[GENERATE_IMAGE_COUNT];
    
    Random rand = new Random();
    SolidBrush brush = new SolidBrush(Color.Black);
    Point mid = new Point(IMAGE_SIZE.Width / 2, IMAGE_SIZE.Height / 2);
    Graphics g = Graphics.FromImage(finalImage);
    Graphics gtemp = Graphics.FromImage(tempImage);

    //set text format
    StringFormat sf = new StringFormat();
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Center;
    Font font = new Font("Times New Roman", 48);

    Color fontColor;
    g.DrawString(inputText, font, brush, mid, sf);
    gtemp.DrawImage(finalImage, 0, 0, tempImage.Width, tempImage.Height);
    for (int i = 0; i < image.Length; i++)
    {
        image[i] = new Bitmap(IMAGE_SIZE.Width, IMAGE_SIZE.Height);
    }
    
    int index = -1;
    int width = tempImage.Width;
    int height = tempImage.Height;
    for (int x = 0; x < width; x += 1)
    {
        for (int y = 0; y < height; y += 1)
        {
            fontColor = tempImage.GetPixel(x, y);
            index = rand.Next(image.Length);

            //Check if the color empty
            if (fontColor.Name == Color.Empty.Name)
            {
                for (int i = 0; i < image.Length; i++)
                {
                    if (index == 0)
                    {
                        image[i].SetPixel(x * 2, y, Color.Black);
                        image[i].SetPixel(x * 2 + 1, y, Color.Empty);
                    }
                    else
                    {
                        image[i].SetPixel(x * 2, y, Color.Empty);
                        image[i].SetPixel(x * 2 + 1, y, Color.Black);
                    }
                }
            }
            else
            {
                for (int i = 0; i < image.Length; i++)
                {
                    if ((index + i) % image.Length == 0)
                    {
                        image[i].SetPixel(x * 2, y, Color.Black);
                        image[i].SetPixel(x * 2 + 1, y, Color.Empty);
                    }
                    else
                    {
                        image[i].SetPixel(x * 2, y, Color.Empty);
                        image[i].SetPixel(x * 2 + 1, y, Color.Black);
                    }
                }
            }
        }
    }

    //Clean Up
    brush.Dispose();
    tempImage.Dispose();
    finalImage.Dispose();

    return image;
}

Points of Interest

They demonstrated a visual secret sharing scheme, where an image was broken up into n shares so that only someone with all n shares could decrypt the image, while any n-1 shares revealed no information about the original image. Each share was printed on a separate transparency, and decryption was performed by overlaying the shares. When all n shares were overlayed, the original image would appear.

History

  • 23rd October, 2007: Initial post

License

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

Share

About the Author

Jacky Yiu
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

You may also be interested in...

Comments and Discussions

 
QuestionHow to implement it for web forms application? Pin
Member 1275739624-Sep-16 19:37
memberMember 1275739624-Sep-16 19:37 
AnswerRe: How to implement it for web forms application? Pin
Jacky Yiu12-Oct-16 22:26
memberJacky Yiu12-Oct-16 22:26 
GeneralRe: How to implement it for web forms application? Pin
Member 1275739610-Dec-16 20:13
memberMember 1275739610-Dec-16 20:13 
GeneralRe: How to implement it for web forms application? Pin
Jacky Yiu10-Apr-17 23:04
memberJacky Yiu10-Apr-17 23:04 
Generalgreetings Pin
Lithin Nasani17-Mar-15 20:50
memberLithin Nasani17-Mar-15 20:50 
QuestionHow to Decryt the Encrypted image Pin
Nandhakumar Jothi3-Dec-14 4:17
memberNandhakumar Jothi3-Dec-14 4:17 
AnswerRe: How to Decryt the Encrypted image Pin
Jacky Yiu23-Apr-15 19:01
memberJacky Yiu23-Apr-15 19:01 
QuestionHow to decrypt Generating two images Pin
Member 111860691-Dec-14 0:41
memberMember 111860691-Dec-14 0:41 
AnswerRe: How to decrypt Generating two images Pin
Jacky Yiu23-Apr-15 19:33
memberJacky Yiu23-Apr-15 19:33 
QuestionHow to download Third Image Pin
Member 1118606929-Nov-14 1:30
memberMember 1118606929-Nov-14 1:30 
QuestionHow to get Original image? Pin
SOHAM_GANDHI3-Sep-14 21:40
memberSOHAM_GANDHI3-Sep-14 21:40 
AnswerRe: How to get Original image? Pin
Jacky Yiu4-Nov-14 23:58
memberJacky Yiu4-Nov-14 23:58 
GeneralRe: How to get Original image? Pin
SOHAM_GANDHI5-Nov-14 0:06
memberSOHAM_GANDHI5-Nov-14 0:06 
QuestionVery Good Article Madam but As am New to C#.Net I am Not Able to Understand it in detailed...so please can you provide any explanation to the code.. Pin
santhosh281-Aug-14 1:31
membersanthosh281-Aug-14 1:31 
QuestionVisual cryptography Pin
Lawrence3628-Dec-13 7:08
memberLawrence3628-Dec-13 7:08 
AnswerRe: Visual cryptography Pin
Jacky Yiu5-Mar-14 22:33
memberJacky Yiu5-Mar-14 22:33 
Questionplease help me Pin
Member 1000475323-Oct-13 16:28
memberMember 1000475323-Oct-13 16:28 
QuestionColor Cryptography Ideas... Pin
manuthebos11-Apr-13 21:27
membermanuthebos11-Apr-13 21:27 
Generalrequest Pin
Akbar basha km18-Jan-13 7:04
memberAkbar basha km18-Jan-13 7:04 
GeneralRe: request Pin
Member 1000475323-Oct-13 16:29
memberMember 1000475323-Oct-13 16:29 
GeneralMy vote of 5 Pin
Vinamaruthi20-Dec-11 1:19
memberVinamaruthi20-Dec-11 1:19 
GeneralI like this. Somehow it got less votes. But it is a good one. My 5 Pin
AlbinAbel16-Mar-11 20:59
memberAlbinAbel16-Mar-11 20:59 
GeneralGreat Idea Pin
Bigdeak10-Jun-10 4:01
memberBigdeak10-Jun-10 4:01 
GeneralGreat Minds think alike... Pin
Keith Vinson29-Oct-07 13:09
memberKeith Vinson29-Oct-07 13:09 
GeneralRe: Great Minds think alike... Pin
Jacky Yiu1-Nov-07 18:00
memberJacky Yiu1-Nov-07 18:00 

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 | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 23 Oct 2007
Article Copyright 2007 by Jacky Yiu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid