Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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 8-May-11 3:07am
Comments
Pete O'Hanlon at 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
good
Please Sign up or sign in to vote.

Solution 1

Encrypt it: System.Security.Cryptography Namespace[^]
 
Sorry if it seems obvious, but...it is!
  Permalink  
Comments
SAKryukov at 8-May-11 11:25am
   
Yes, that's all, my 5.
--SA
Rate this: bad
good
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;
            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.
  Permalink  
Comments
mishkaPishka at 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 at 9-May-11 7:21am
   
hi my5 gud
Tarun.K.S at 9-May-11 7:23am
   
Good answer. 5+
mishkaPishka at 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 at 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
good
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.
  Permalink  
v2
Comments
mishkaPishka at 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
good
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();
  Permalink  
Comments
deep subba at 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
0 Sergey Alexandrovich Kryukov 269
1 OriginalGriff 240
2 CPallini 195
3 Maciej Los 177
4 BillWoodruff 173
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 5 Apr 2013
Copyright © CodeProject, 1999-2014
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