How to hava a Complex Password in C#

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: output 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.  
  4. // Replace [A-Z] with \p{Lu}, to allow for Unicode uppercase letters.
  5. System.Text.RegularExpressions.Regex upper = new System.Text.RegularExpressions.Regex("[A-Z]");
  6. System.Text.RegularExpressions.Regex lower = new System.Text.RegularExpressions.Regex("[a-z]");
  7. System.Text.RegularExpressions.Regex number = new System.Text.RegularExpressions.Regex("[0-9]");
  8. // Special is "none of the above".
  9. System.Text.RegularExpressions.Regex special = new System.Text.RegularExpressions.Regex("[^a-zA-Z0-9]");
  10.  
  11. // Check the length.
  12. if (pwd.Length < minLength)
  13. {
  14. return false;
  15. }
  16. // Check for minimum number of occurrences.
  17. if (upper.Matches(pwd).Count() < numUpper)
  18. {
  19. return false;
  20. }
  21. if (lower.Matches(pwd).Count() < numLower)
  22. {
  23. return false;
  24. }
  25. if (number.Matches(pwd).Count() < numNumbers)
  26. {
  27. return false;
  28. }
  29. if (special.Matches(pwd).Count() < numSpecial)
  30. {
  31. return false;
  32. }
  33.  
  34. // Passed all checks.
  35. return true;
  36. }
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 output A complex password output 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

Add new comment