![]() |
Loop Training: Password Finder
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 |
Loop Training: Password Finder
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 |
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 |
Loop Training: Password Finder
Roy, Good code. I like it. I had not though to use the len and mi functions. As I read your code, you take the first letter of th PASSWORD and compare it against a the letters "a" through "z" until match is found. Then, the macro grabs the second letter of th password...etc. I like it. However, I was assuming that the password was NOT known. I had to hav a password for my macro to find, so I chose eureka. I was trying t emulate the non-unique situation where we have no idea what th password is, so we want the macro to brute-force its way to th solution...but as you note, my macro takes forever. I let it try t find "eureka" and I had to make it stop after 4 hours of trying... There has got to be a more effecient way of checking all possibl solutions.. -- jasonsweene ----------------------------------------------------------------------- jasonsweeney's Profile: http://www.excelforum.com/member.php...nfo&userid=522 View this thread: http://www.excelforum.com/showthread.php?threadid=39361 |
Loop Training: Password Finder
A few suggestions based only on theory:
1) Declare the variables a to g as Long. As I'm told, the long variable type is native to Excel while integers, even though they are a simpler data type, are converted to longs anyways. 2) Streamline the contents inside the loop as much as possible. The code inside the loop must be evaluated for each iteration which is many thousands or even millions of times. In particular, don't do anything unnecessary to the worksheet such as change a cell's value from within the loop. Note that, in your code, all of the following must be evaluated (and, in the case of A2's value executed) with each iteration: Range("A2").Value = Crack If Range("A2").Value = Range("A1").Value Then With Range("a6") .Value = "Stop: " & Now() End With Exit Sub 3) I replaced all of the above with the following. I suspect that comparing one variable (memory location) to another directly rather than comparing cell values is more efficient: If p = pwd Then GoTo EndMsg 4) I didn't see the point of the Do...Loop so I removed it. Same for first If...Then construct: "If Range("A2").Value < Range("A1").Value Then" All of the above said, if you do the math, it will probably take days (!!!) to break 7 character passwords if you don't actually know the number of characters - i.e. you have to test all possibilities from 1 to 7 characters. And this assumes that only lower case alphabetic characters are used. Include numbers and upper case and it will take till eternity. And then there's punctuation. Regards, Greg Sub passwordsearch() Dim a As Long, b As Long, c As Long Dim d As Long, e As Long, f As Long, g As Long Dim pwd As String, p As String pwd = Range("A1").Value Range("a5").Value = "Start: " & Now() Application.ScreenUpdating = False 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 p = Chr(a) & Chr(b) & Chr(c) & Chr(d) & Chr(e) & Chr(f) & Chr(g) If p = pwd Then GoTo EndMsg Next: Next: Next: Next: Next: Next: Next EndMsg: Range("a6").Value = "Stop: " & Now() Application.ScreenUpdating = True End Sub "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 |
Loop Training: Password Finder
Greg, Thanks for the suggestions. That sped up the calculations significantly. It takes about 24 minutes to test all possible lower-case 7-digit passwords beggining with the letter "a". That would translate into approximately 10.5 hours for a search of all possible 7-digit lower-case passwords, with a single search pattern. better...but still slow. Then again, I am asking the computer to try just a tad over 8 Billion possible passwords... Thanks again... -- jasonsweeney ------------------------------------------------------------------------ jasonsweeney's Profile: http://www.excelforum.com/member.php...fo&userid=5222 View this thread: http://www.excelforum.com/showthread...hreadid=393619 |
All times are GMT +1. The time now is 04:24 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com