Visual Basic Password Generator

I was commissioned to create a generator of random visual data generators. I came up with below that works, but its a little rude with its password criteria. I would like for each password that it generated to have at least 1 number in it, in upper and lower case. However, as I have encoded this, it will generate a random combination, which often leads to the fact that one of the criteria will be missed.

I had the game myself, and I had three lines: one with capital letters, one with lower case and the third with numbers. Once it has one of them, it will generate the rest of the password using my code. It does not seem very clean and I am having problems with this.

Can someone help point me in the right direction or help with the code below. Passwords must be between 6 and 20 characters long.

Thank you in advance

Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click TextBox2.Text = GenerateCode() End Sub Private Function GenerateCode() As Object Dim intRnd As Object Dim intStep As Object Dim strName As Object Dim intNameLength As Object Dim intLength As Object Dim strInputString As Object strInputString = "1234567890ABCDEFGHIJKLMEOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz" intLength = Len(strInputString) Randomize() strName = "" 'Check for valid numeric entry If Integer.TryParse(TextBox1.Text, intNameLength) And intNameLength >= 6 And intNameLength <= 20 Then For intStep = 1 To intNameLength intRnd = Int((intLength * Rnd()) + 1) strName = strName & Mid(strInputString, intRnd, 1) Next GenerateCode = strName Else TextBox1.Text =("Please enter a valid password length") End If End Function Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click My.Computer.Clipboard.SetText(TextBox2.Text) End Sub Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter TextBox1.Text = "" End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub End Class 

=========== UPDATE ===========

That's right, I changed my code a bit, so now I have three lines with different characters, 1 for the vertices, one for the slides and one for the numbers.

This allows me to have checkbox functionality in the form, which is a plus. I played with the code and now it generates passwords depending on what I choose with the help of checkboxes, which, as I say, are awesome, but I can not always guarantee that if I choose, number, upper and lower, I I’ll get a password containing all three, and sometimes the password will contain only numbers, even if all three fields are checked.

Guessing this is due to the fact that I just ask him to generate a random password using the characters that I give him, and there is no confirmation that he used all three parameters.

Any help on this would be awesome. I try and I do not just send messages and hope that someone will do the work for me. If someone can point me in the right direction, it would be great.

Here is my new code.

 Public Function GenerateCode() Dim intRnd As Integer Dim intStep As Integer = Nothing Dim strname As String Dim intlength As Integer Dim strinputstring As String = "" Dim Numbers As String = "12345678901234567890" Dim Lower As String = "abcdefghijklmnopqrstuvwxyzyz" Dim Upper As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZYZ" Dim intnamelength As Integer = 1 If CheckBox1.Checked Then strinputstring &= Lower If CheckBox2.Checked Then strinputstring &= Numbers If CheckBox3.Checked Then strinputstring &= Upper intlength = Len(strinputstring) Integer.TryParse(NumericUpDown1.Text, intnamelength) Randomize() strname = "" For inStep = 1 To intnamelength intRnd = Int(Rnd() * intlength) + 1 strname = strname & Mid(strinputstring, intRnd, 1) Next Return strname End Function 
+5
source share
3 answers

It is independent of user interface controls and does not use the deprecated pre-NET VB features. Instead, the variables are passed to the generator, so it is independent of the user interface. It also does not use As Object because strings are strings.

 <Flags> Private Enum PasswordParts Upper = 1 Lower = 2 Numerals = 4 All = 7 End Enum Private Shared RNG As New Random() Private Shared Numerals As String = "0123456789" Private Shared Upper As String = "ABCDEFGHIJKLMNPQRSTUVWXYZYZ" Private Shared Lower As String = "abcdefghijkmnopqrstuvwxyzyz" Private Function GeneratePassword(length As Int32, Optional pwparts As PasswordParts = PasswordParts.All) As String Dim PWCharPool As String = "" Dim PW As New List(Of String)() ' check the requested length If length < 6 Then length = 6 If length > 20 Then length = 20 ' build the pool and add the first of required characters ' for assure minimum conformance If pwparts.HasFlag(PasswordParts.Lower) Then PW.Add(Lower(RNG.Next(0, Lower.Length))) PWCharPool &= Lower End If If pwparts.HasFlag(PasswordParts.Upper) Then PW.Add(Upper(RNG.Next(0, Upper.Length))) PWCharPool &= Upper End If If pwparts.HasFlag(PasswordParts.Numerals) Then PW.Add(Numerals(RNG.Next(0, Numerals.Length))) PWCharPool &= Numerals End If ' pick the rest of the elements For n As Int32 = PW.Count To length - 1 PW.Add(PWCharPool(RNG.Next(0, PWCharPool.Length))) Next ' shuffle the result so that the ' first 1-3 chars are not predictable Shuffle(PW, RNG) ' create a string from the result Return String.Join("", PW) End Function 
  • Note that lowercase L or uppercase O not. Users will confuse them with 0 and 1 , so either delete them, or 0 and 1 (or both)
  • If the requirements for at least 1 upper, lower, and numerical values ​​are complex, then there is no need for If statements (rather than blocks): always select one from each list before adding them to the pool. You also won't need Enum or param. I left them based on the code in the question
    • Please note that you must add code for processing when they do not check the boxes before calling this method.
  • A StringBuilder can be used instead of List(of String) , but the strings are so short that it really does not save time until you run it in a loop many times and even then the time saved is insignificant.

Testing:

 Dim enumValues = [Enum].GetValues(GetType(PasswordParts)).Cast(Of Int32)() For n As Int32 = 1 To 1000 Dim style = CType(enumValues(RNG.Next(0, 4)), PasswordParts) Dim pwLen = RNG.Next(6, 21) Dim PW = GeneratePassword(pwLen, style) ' get riled up if the length does not match Debug.Assert(PW.Length = pwLen) Console.WriteLine("style: {0} pw: '{1}' (len={2})", style, PW, PW.Length) Next 

Output Example:

style: Upper pw: 'QFHGPLIEEYPRP' (len = 13)
style: All pw: 'Z9Y3CoW' (len = 7)
style: Lower pw: 'tyghanjzudhhorfmvjr' (len = 19)
style: All pw: 'XyY3q10N6S' (len = 10)
style: Upper pw: 'IOZGTTQTPCYLKGEFRZ' (len = 18)
style: All pw: '7t5CNMUM0GdWb' (len = 13)
style: Upper pw: 'YIFXHRKEICOHXEUX' (len = 16)

Then a shuffle helper (which is somewhat optional):

 ' Standared FY shuffle for List(Of T) Public Sub Shuffle(Of T)(items As List(Of T), rnd As Random) Dim temp As T Dim j As Int32 For i As Int32 = items.Count - 1 To 0 Step -1 ' Pick an item for position i. j = rnd.Next(i + 1) ' Swap them. temp = items(i) items(i) = items(j) items(j) = temp Next i End Sub 

The big problem is that no one remembers these passwords, so they are tied to a monitor or the bottom of the keyboard. The best way is to let the user select PW and just make sure that it matches any rules by checking the string they select for length and content. This will allow my~MuffyDoodle123 or some that the user can remember. Maybe make them change it periodically and maybe add TrashCan to store the old PW by the user so that they cannot reuse the same one for 6 or 9 months.

The way to automatically create catchy PW uses words. Create a list of several thousand adjectives and adverbs and another list of several thousand nouns (easier than it sounds - there are online generators), you can create alliterative combinations plus a special character:

Rancid3rain
Withered $ Whisper
Creamy / Cotton
Enduring / Empire
Maximum7Mist
Oily / Glory
Vaporized_Vision
Toxic! Tears
Angry! Abacus

The "hard" part is to use a centralized list and discard words as they are used so that there are no duplicate words in the generated passwords. Only when one of the lines becomes low does one start.

+14
source

Try:

Here is the basic idea. If you need uppercase and lowercase letters, add lowercase letters to the string s.

 Sub Main() Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" Dim r As New Random Dim sb As New StringBuilder For i As Integer = 1 To 8 Dim idx As Integer = r.Next(0, 35) sb.Append(s.Substring(idx, 1)) Next Console.WriteLine(sb.ToString()) Console.ReadKey() End Sub 

And that should replace all your old code

+1
source

Below is my shot at this. This works well for me, so I believe it will be for you. This ensures that your password has a combination of 4 categories, namely:

  1. Uppercase
  2. lower case
  3. numerical
  4. Special symbols

The minimum password length is 6 characters.

The first section selects a character from each of the 4 categories. Then the loop is executed for the remaining required number of characters. Since the first 4 will be in the order of the listed categories, after generating the password, a shuffle code is added that performs random shuffling, thereby improving the randomness of the password.

 Public Function GeneratePassword(Optional ByVal Len As Integer = 8) If Len < 6 Then MsgBox("Minimum password length is 6 characters", MsgBoxStyle.OkOnly, "Minimum Length Reset") Len = 6 End If Dim pass As String = String.Empty Dim nums As String() = "2 3 4 5 6 7 8 9".Split(" ") 'Omit 1 & 0 Dim lettU As String() = "ABCDEFGHJKLMNPQRSTUVW XYZ".Split(" ") 'Omit i,I,o & O Dim lettL As String() = "ABCDEFGHJKMNPQRSTUVWX YZ".ToLower.Split(" ") 'Omit i,I,l, L,o & O Dim chars As String() = "(-) @ # $ % * {-} [-] - _ ^ < > + = ~ /\".Split(" ") 'omit ? / \ ( ) ' " . , ; : & Dim passRan() As Array = {nums, lettU, lettL, chars} Dim min As Integer = 0 Dim max As Integer = passRan.Length 'this will include the length Dim rnd As Integer = 0 Dim sb As New List(Of String) For l As Integer = 0 To Len - passRan.Length - 1 'select the set to pick from ensuring you have a character from each set If l = 0 Then For p As Integer = 0 To passRan.Length - 1 'pick a random position in the selected set max = passRan(p).Length rnd = GetRandom(min, max) sb.Add(passRan(p)(rnd)) Next End If 'select the set to pick from by random max = passRan.Length rnd = GetRandom(min, max) For p As Integer = 0 To passRan.Length - 1 'pick a random position in the selected set If p = rnd Then max = passRan(p).Length rnd = GetRandom(min, max) sb.Add(passRan(p)(rnd)) Exit For End If Next Next 'shuffle the result Dim R As New List(Of String) R = sb.ToList For Int As Integer = 0 To Len - 1 Dim curr As Integer = GetRandom(min, R.Count) pass &= R(curr) R.RemoveAt(curr) Next Return pass End Function Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer Static Generator As System.Random = New System.Random() Return Generator.Next(Min, Max) End Function 

NB. To increase the number of special characters available and their complexity, I used (-), [-] and {-} because they are difficult to read on their own, but they can also be complex if you combine them without a space between them. I also usually I prefer to omit OoiILL01 from my passwords to avoid confusion.

0
source

Source: https://habr.com/ru/post/1262269/


All Articles