Windows Forms Application To Generate A Unique Random Password

This blog demonstrates how to create an application to generate random number encrypted passwords. We will create one Windows Form application in C# which generates a unique encrypted password.

So, let's open Visual Studio and follow these steps.

Step 1

Open Visual Studio.

Step 2

Go to File >> New >> Project. 

Windows.Forms

Step 3

Now, choose Visual C# and select Windows. Then, select Windows Forms Application. Now, you can give your application name and click on OK button. 

Windows.Forms

Step 4

Design the form like this.

Windows.Forms

Step 5

Create one static method with the name "Shuffle" in code-behind. 

This Shuffle method returns a string parameter with the position changed for all the characters.

  1. static string Shuffle(string input) {  
  2.     var q = from c in input.ToCharArray()  
  3.     orderby Guid.NewGuid()  
  4.     select c;  
  5.     string s = string.Empty;  
  6.     foreach(var r in q)  
  7.     s += r;  
  8.     return s;  
  9. }  

Step 6

Now, write the following code in ValueChanged event of Tickbar control.

  1. private void trckbar_length_ValueChanged(object sender, EventArgs e) {  
  2.     passLength = trckbar_length.Value + 1; //trackbar value starts from 0, so I add +1 to make it understandable;  
  3.     lbl_passlength.Text = passLength.ToString();  
  4. }  

Step 7

After this, write the following code in click event of the "Generate password" button.

  1. private void btn_generatepass_Click(object sender, EventArgs e)  
  2. {  
  3.     txb_password.Text = "";  
  4.     string text = "aAbBcCdDeEfFgGhHiIjJhHkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ01234567890123456789,;:!*$@-_=,;:!*$@-_=";  
  5.     text = Shuffle(text); //shuffle the above symbols using shuffle() method.  
  6.     text = text.Remove(passLength); //cut the string size according to the chosen trackbar value.  
  7.     txb_password.Text = text;  
  8. }  

Now, let's write full C# code, so you can understand it very well.

C# Code

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. namespace iPass {  
  11.     public partial class MainForm: Form {  
  12.         int passLength = 0;  
  13.         public MainForm() {  
  14.             InitializeComponent();  
  15.         }  
  16.         //Shuffle method that returns a string parameter with all the characters' position changed.  
  17.         static string Shuffle(string input) {  
  18.             var q = from c in input.ToCharArray()  
  19.             orderby Guid.NewGuid()  
  20.             select c;  
  21.             string s = string.Empty;  
  22.             foreach(var r in q)  
  23.             s += r;  
  24.             return s;  
  25.         }  
  26.         private void btn_generatepass_Click(object sender, EventArgs e) {  
  27.             txb_password.Text = "";  
  28.             string text = "aAbBcCdDeEfFgGhHiIjJhHkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ01234567890123456789,;:!*$@-_=,;:!*$@-_=";  
  29.             text = Shuffle(text); //shuffle the above symbols using shuffle() method.  
  30.             text = text.Remove(passLength); //cut the string size according to the chosen trackbar value.  
  31.             txb_password.Text = text;  
  32.         }  
  33.         private void trckbar_length_ValueChanged(object sender, EventArgs e) {  
  34.             passLength = trckbar_length.Value + 1; //trackbar value starts from 0, so I add +1 to make it understandable;  
  35.             lbl_passlength.Text = passLength.ToString();  
  36.         }  
  37.     }  
  38. }  

Step 8

Run this project.

Windows.Forms

Summary

This Password Generator will generate a unique random password using characters, letters, numbers, and special characters so it can't be decrypted easily.

Ebook Download
View all
Learn
View all
Codingvila is an educational website, developed to help tech specialists/beginners.