Windows Forms: How to encrypt a password in C#

How to encrypt a password using MD5 encryption in C#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "EncryptPassword" and then click OK

encrypt password in c#Step 2: Design your form as below

encrypt password in c#

Step 3: Add code to handle your form as below

using System;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace EncryptPassword
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static string Encrypt(string value)
        {
            //Using MD5 to encrypt a string
            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
            {
                UTF8Encoding utf8 = new UTF8Encoding();
                //Hash data
                byte[] data = md5.ComputeHash(utf8.GetBytes(value));
                return Convert.ToBase64String(data);
            }
        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show("Please enter your password.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            txtResult.Text = Encrypt(txtPassword.Text);
        }
    }
}

VIDEO TUTORIALS