Click here to Skip to main content
13,046,176 members (69,515 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I want to build an application that gets an image file and return the same file but locked with a password.

there are any idea how to do that simple as possible?

Posted 8-May-11 2:07am
Pete O'Hanlon 8-May-11 15:48pm
You have two possible questions here, could you clarify which one it is?
1) You want your application to control access to an image by requiring the user to enter a password to view it in the application.
2) You want to set it so that an image cannot be opened without a password.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Encrypt it: System.Security.Cryptography Namespace[^]

Sorry if it seems obvious, is!
SAKryukov 8-May-11 11:25am
Yes, that's all, my 5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Try following code to make password and checking:
// 1) Making Password
using System.Security.Cryptography;
static class CommonMethods
public static string MD5Hash(string str)
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.ASCII.GetBytes( str );
            data = md5.ComputeHash(data);
            string md5Hash = System.Text.Encoding.ASCII.GetString(data);
            return md5Hash;
        public static bool ConfirmPwd(string pwd)
            string storedPwd, encryptedPwd;
                StreamReader fsPwdFile = 
                            new StreamReader(
                                new FileStream(
                storedPwd = fsPwdFile.ReadToEnd();
            { return false; 
            encryptedPwd = MD5Hash(pwd);
            if (storedPwd != encryptedPwd)
                return false;
            return true;
// 2) Applying password on your image file 
       private void btnOK_Click(object sender, EventArgs e)
            if (CommonMethods.ConfirmPwd(tbPassword.Text))
                this.DialogResult = DialogResult.OK;
                   ("Wrong password, please re-enter", "Error",
                       MessageBoxButtons.OK, MessageBoxIcon.Error);

Hope this contains sufficient information, your require.
mishkaPishka 9-May-11 1:31am
First of all, THANKS!!!
I have two more question,
maybe i missed understood but, there is a place here that the user can enter his own password?
there is a way to decode password that was encoded with MD5?

Thanks alot!!
beginner in 9-May-11 7:21am
hi my5 gud
Tarun.K.S 9-May-11 7:23am
Good answer. 5+
mishkaPishka 11-May-11 8:24am
Hey sk saini,

one thing that is still unclear to me,
in my application i have a textBox for the user enter the password he wants.
i tried to run my application using the code that you gave but i get the error msg every time.
how can i use the password that entered by the user?

Hans Yuwono Salim 25-Jun-14 8:19am
First of all, this code seems will be working well.

but can you please specify what is needed in the interface?
I assume we will be needing two buttons and two text field?
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

In my code, 2nd button event i.e., btnOK_Click() is the event which is used to verify the user's input password with his actual password saved through MD5Hash() method.

Here we don't require to decode user password, rather we have to decode encode again user input password (while authentication) and match with his earlier created password (stored on system wherever we want) each time he try to access our source.

So, First method saves user input password after converting it to MD5 encryption.

And second method authenticates user input password with comparing it with saved password after its MD5 encoding too.
mishkaPishka 11-May-11 2:55am
What I meant is that in my application there is a textBox which the user enter the password he want to lock with the image.
i'm sorry but i cant find where in the MD5Hash() method i can do it.

again, thank a lot!!
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

StreamReader fsPwdFile = new StreamReader( new FileStream(Program.strPwdFilePath,FileMode.Open, FileAccess.Read));
storedPwd = fsPwdFile.ReadToEnd();
deep subba 17-Jun-13 8:44am
there is no method to set password to image. the given method is only for confirm passwords from a specified location. provide method to set password to image.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100