如何设置图像文件的密码C# [英] how to set a password to an image file C#

查看:64
本文介绍了如何设置图像文件的密码C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想构建一个获取图像文件并返回相同文件但使用密码锁定的应用程序。



有什么想法可以做到这么简单吗?



谢谢!

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!

推荐答案

加密: System.Security.Cryptography命名空间 [ ^ ]



很抱歉,如果它看起来很明显,但是......它是!
Encrypt it: System.Security.Cryptography Namespace[^]

Sorry if it seems obvious, but...it is!


尝试使用以下代码来输入密码和检查:

// 1)使用System.Security.Cryptography创建密码

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.


在我的代码中,第二个按钮事件,即btnOK_Click()是用于通过MD5Hash()方法保存实际密码来验证用户输入密码的事件。



这里我们不需要解码用户密码,而是我们必须再次解码编码用户输入密码(同时进行身份验证)并与之前创建的密码匹配(存储在系统,无论我们想要什么)每次尝试访问我们的源代码。



因此,First方法在将用户输入密码转换为MD5加密后保存用户输入密码。



第二种方法验证用户输入密码,并在MD5编码后将其与保存的密码进行比较。
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.


这篇关于如何设置图像文件的密码C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆