• We just launched and are currently in beta. Join us as we build and grow the community.

How to hava a Complex Password in C#

ODGB

Ad Reach Optimizer
Divine
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
147
Likes
115
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Today in C#, we will create a program that can determine if the inputted password is complex or not. Strong passwords meet a number of requirements for complexity - including length and character categories - that make passwords more difficult for attackers to determine. Establishing strong password policies for your organization can help prevent attackers from impersonating users and can thereby help prevent the loss, exposure, or corruption of sensitive information.

Now, let's start this tutorial!

1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application.

2. Next, add only one Button named Button1 and labeled it as "Submit" for determining the complexity of the password. Add one textbox named TextBox1 for inputting your desired password. Add two labels named Label1 labeled it as "Password is Complex:" and Label2 which is an empty string that has a boolean value for displaying the complexity. You must design your interface like this:

passworddesign_0.png


3. Import System.text.RegularExpressions.
  1. using

    System.Text.RegularExpressions

    ;

We imported this namespace because it enables us to determine whether a particular string conforms to a regular expression pattern.

4. Create a function named ValidatePassword. Put this code in your code module:

  1. public

    bool

    ValidatePassword(

    string

    pwd, int

    minLength =

    8

    , int

    numUpper =

    2

    , int

    numLower =

    2

    , int

    numNumbers =

    2

    , int

    numSpecial =

    2

    )
  2. {

  3. // Replace [A-Z] with \p{Lu}, to allow for Unicode uppercase letters.
  4. System.Text

    .

    RegularExpressions

    .

    Regex

    upper =

    new

    System.Text

    .

    RegularExpressions

    .

    Regex

    (

    "[A-Z]"

    )

    ;
  5. System.Text

    .

    RegularExpressions

    .

    Regex

    lower =

    new

    System.Text

    .

    RegularExpressions

    .

    Regex

    (

    "[a-z]"

    )

    ;
  6. System.Text

    .

    RegularExpressions

    .

    Regex

    number =

    new

    System.Text

    .

    RegularExpressions

    .

    Regex

    (

    "[0-9]"

    )

    ;
  7. // Special is "none of the above".
  8. System.Text

    .

    RegularExpressions

    .

    Regex

    special =

    new

    System.Text

    .

    RegularExpressions

    .

    Regex

    (

    "[^a-zA-Z0-9]"

    )

    ;

  9. // Check the length.
  10. if

    (

    pwd.

    Length

    <

    minLength)
  11. {
  12. return

    false

    ;
  13. }
  14. // Check for minimum number of occurrences.
  15. if

    (

    upper.

    Matches

    (

    pwd)

    .

    Count

    (

    )

    <

    numUpper)
  16. {
  17. return

    false

    ;
  18. }
  19. if

    (

    lower.

    Matches

    (

    pwd)

    .

    Count

    (

    )

    <

    numLower)
  20. {
  21. return

    false

    ;
  22. }
  23. if

    (

    number.

    Matches

    (

    pwd)

    .

    Count

    (

    )

    <

    numNumbers)
  24. {
  25. return

    false

    ;
  26. }
  27. if

    (

    special.

    Matches

    (

    pwd)

    .

    Count

    (

    )

    <

    numSpecial)
  28. {
  29. return

    false

    ;
  30. }

  31. // Passed all checks.
  32. return

    true

    ;
  33. }

We have created ValidatePassword Function that has the following parameters of a password as string, minimum length of the password is 8, Capital Letter and Lower Letter must have a minimum of two letters, two digits of numbers, and must have a minimum of 2 special characters to make it complex. Then we have initialized variable upper to have capitalized letter, variable lower to have non-capitalized letter, variable number to have numerical inputs, and we used RegEx because it will hold the regular expression of not having a number or letter but to have a special character which it holds by variable special. Then it checks the length, if it will not have a minimum of 8 characters then it will return False as well as the other parameters. If it meets all the requirements, then it will display True.

5. Then, put this code to button1_click. This will display the output in boolean in Label2.

  1. private

    void

    Button1_Click(

    object

    sender, EventArgs e)
  2. {
  3. Label2.

    Text

    =

    ValidatePassword(

    TextBox1.

    Text

    )

    ;
  4. }

Output:


Not a complex password

passwordoutput1_0.png


A complex password

passwordoutput2_0_0.png


For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

 

442,401

317,942

317,951

Top