VBA 破解两个字符密码的代码

VBA code to crack a two character password

尝试破解两个字符的密码

Sub BruteForce()

        Dim pw As String
        Dim i As Integer

        Do
            ThisWorkbook.Sheets("Login").Cells(4, 7).ClearContents
            For i = 1 To 2
                If i Mod 2 = 0 Then
                    pw = pw & Int((9 - 0 + 1) * Rnd + 0)
                Else
                    pw = pw & Chr(Int((90 - 65 + 1) * Rnd + 65))
                End If
            Next i
            ThisWorkbook.Sheets("Login").Cells(4, 7).Value = pw
            If ThisWorkbook.Sheets("Login").Cells(5, 7).Value <> "NO" Then
                Exit Do
            End If
        Loop

    End Sub

代码进入无限循环,我不确定如何修复

Sub BruteForce()

Dim pw As String
Dim i As Integer

Do
    ThisWorkbook.Sheets("Login").Cells(4, 7).ClearContents
    pw = ""
    For i = 1 To 2
        If i Mod 2 = 0 Then
            pw = pw & Int((9 - 0 + 1) * Rnd + 0)
        Else
            pw = pw & Chr(Int((90 - 65 + 1) * Rnd + 65))
        End If
    Next i
    ThisWorkbook.Sheets("Login").Cells(4, 7).Value = pw
    If ThisWorkbook.Sheets("Login").Cells(5, 7).Value <> "NO" Then
        Exit Do
    End If
Loop

结束子

已解决

使用嵌套循环更简单:

Sub BruteForce()

    Dim i As long, n As Long

    With ThisWorkbook.Sheets("Login")
        For i = 0 To 9
        For n = 1 to 26
            .Cells(4, 7).Value = Chr(64+n) & i
            DoEvents   
            If .Cells(5, 7).Value <> "NO" Then Exit Sub
        Next n
        Next i
    End With

End Sub