Posted to microsoft.public.excel.programming
|
|
Loop Training: Password Finder
Dim LenPword As Integer, x As Integer
should be
Dim LenPword As Integer, x As Integer, y as Integer
"Roy Wagner" wrote:
Jason,
'In your unique example, you know where the password is.
'With that in mind, I would do a parsing loop. The example
'below determines the password by trial and error regardless
'of its length. There are only 3 changes on the sheet, so it
'isn't necessary to disable screen updating. My test took less
'than 1 second to find the answer.
'Nesting loops 7 deep is definitely going to slow things down.
'The cycles on your g loop would be phenominal in your case.
'Put an extra variable (make it a double) in the g loop to see
'what I mean. Do a control break (on your code) after a few minutes,
'hit debug and see what you gLoop counter says. Yikes!
Dim Pword As Range
Dim Solution As String
Dim LenPword As Integer, x As Integer
Range("a5").Value = "Start: " & Now()
Set Pword = Range("A1")
LenPword = Len(Pword)
For x = 1 To LenPword
For y = 97 To 122
If Mid(Pword, x, 1) = Chr(y) Then
Solution = Solution + Chr(y)
If x = LenPword Then
Range("A2").Value = Solution
Range("A6").Value = "Stop: " & Now()
Exit Sub
End If
End If
Next
Next
'Roy
--
(delete .nospam)
"jasonsweeney" wrote:
I am teaching myself VBA. To learn how to write lopps I have challenged
myself to do the following:
A macro that cracks a password. I have a macro below, but please let
me know if any of you can come up with a quicker way to search the
various strings (beyond the obvious of "Application.ScreenUpdating =
False").
So here is the challenge:
In the workbook:
Cell A1 contains the 7-digit password, say "eureka" (this is the
password I want the macro to find)
Cell A2 is blank (this is the cell I want the macro to test strings)
Note:
- This macro will look only for a 7-digit password, which password
shall be in lower-case letters only (the lowercase alphabet = Chr(97)
through Chr (122))
- This macro will also display the time the search begins and stops.
Here is my first try at a macro to hack the password:
_______________
Sub passwordsearch()
'
' 7 DIGIT SEARCH
'
Dim a As Integer, b As Integer, c As Integer
Dim d As Integer, e As Integer, f As Integer, g As Integer
Dim Crack As String
Range("a5").Value = "Start: " & Now()
On Error Resume Next
If Range("A2").Value < Range("A1").Value Then
Do
For a = 97 To 122: For b = 97 To 122: For c = 97 To 122
For d = 97 To 122: For e = 97 To 122: For f = 97 To 122: For g
= 97 To 122
Crack = Chr(a) & Chr(b) & Chr(c) & Chr(d) & Chr(e) & Chr(f) &
Chr(g)
Range("A2").Value = Crack
If Range("A2").Value = Range("A1").Value Then
With Range("a6")
.Value = "Stop: " & Now()
End With
Exit Sub
End If
Next: Next: Next: Next: Next: Next: Next
Loop
End If
End Sub
___________________
I launch this macro from a button on the worksheet. Any
reccomendations are appreciated!
-- Jason
--
jasonsweeney
------------------------------------------------------------------------
jasonsweeney's Profile: http://www.excelforum.com/member.php...fo&userid=5222
View this thread: http://www.excelforum.com/showthread...hreadid=393619
|