Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hello,

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?

Thanks!
Posted
Comments
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.

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

Sorry if it seems obvious, but...it is!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-May-11 11:25am    
Yes, that's all, my 5.
--SA
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;
            try
            {
                StreamReader fsPwdFile = 
                            new StreamReader(
                                new FileStream(
                                    Program.strPwdFilePath, 
                                    FileMode.Open, 
                                    FileAccess.Read));
                storedPwd = fsPwdFile.ReadToEnd();
                fsPwdFile.Close();
            }
            catch
            { 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;
                this.Close();
            }
            else
            {
                MessageBox.Show
                   ("Wrong password, please re-enter", "Error",
                       MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbPassword.Focus(); 
                return;
            }
        }


Hope this contains sufficient information, your require.
 
Share this answer
 
Comments
mishkaPishka 9-May-11 1:31am    
Hey
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 C#.net 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?

Thanks!
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?
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.
 
Share this answer
 
v2
Comments
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!!
StreamReader fsPwdFile = new StreamReader( new FileStream(Program.strPwdFilePath,FileMode.Open, FileAccess.Read));
storedPwd = fsPwdFile.ReadToEnd();
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900