4
' modUnlockRoutines ' ' Module provides Excel workbook and sheet unlock routines. The algorithm ' relies on a backdoor password that can be 1 to 9 characters long where each ' character is either an "A" or "B" except the last which can be any character ' from ASCII code 32 to 255. ' ' Implemented as a regular module for use with any Excel VBA project. ' ' Dependencies: ' ' None ' ' © 2007 Kevin M. Jones Option Explicit Private Sub DisplayStatus( _ ByVal PasswordsTried As Long _ ) ' Display the status in the Excel status bar. ' ' Syntax ' ' DisplayStatus(PasswordsTried) ' ' PasswordsTried - The number of passwords tried thus far. Static LastStatus As String LastStatus = Format(PasswordsTried / 57120, "0%") & " of possible passwords tried." If Application.StatusBar <> LastStatus Then Application.StatusBar = LastStatus DoEvents End If End Sub Private Function TrySheetPasswordSize( _ ByVal Size As Long, _ ByRef PasswordsTried As Long, _ ByRef Password As String, _ Optional ByVal Base As String _ ) As Boolean ' Try unlocking the sheet with all passwords of the specified size. ' ' TrySheetPasswordSize(Size, PasswordsTried, Password, [Base]) ' ' Size - The size of the password to try. ' ' PasswordsTried - The cummulative number of passwords tried thus far. ' ' Password - The current password. ' ' Base - The base password from the calling routine. Dim Index As Long

Excel Desbloquear

Embed Size (px)

DESCRIPTION

Crear macro con esta información para desbloquear hojas y libros de Excel.

Citation preview

' modUnlockRoutines'' Module provides Excel workbook and sheet unlock routines. The algorithm' relies on a backdoor password that can be 1 to 9 characters long where each' character is either an "A" or "B" except the last which can be any character' from ASCII code 32 to 255.'' Implemented as a regular module for use with any Excel VBA project.'' Dependencies:''None'' 2007 Kevin M. JonesOption ExplicitPrivate Sub DisplayStatus( _ByVal PasswordsTried As Long _)' Display the status in the Excel status bar.'' Syntax'' DisplayStatus(PasswordsTried)'' PasswordsTried - The number of passwords tried thus far.Static LastStatus As StringLastStatus = Format(PasswordsTried / 57120, "0%") & " of possible passwords tried."If Application.StatusBar LastStatus ThenApplication.StatusBar = LastStatusDoEventsEnd IfEnd SubPrivate Function TrySheetPasswordSize( _ByVal Size As Long, _ByRef PasswordsTried As Long, _ByRef Password As String, _Optional ByVal Base As String _) As Boolean' Try unlocking the sheet with all passwords of the specified size.'' TrySheetPasswordSize(Size, PasswordsTried, Password, [Base])'' Size - The size of the password to try.'' PasswordsTried - The cummulative number of passwords tried thus far.'' Password - The current password.'' Base - The base password from the calling routine.Dim Index As LongOn Error Resume NextIf IsMissing(Base) Then Base = vbNullStringIf Len(Base) < Size - 1 ThenFor Index = 65 To 66If TrySheetPasswordSize(Size, PasswordsTried, Password, Base & Chr(Index)) ThenTrySheetPasswordSize = TrueExit FunctionEnd IfNext IndexElseIf Len(Base) < Size ThenFor Index = 32 To 255ActiveSheet.Unprotect Base & Chr(Index)If Not ActiveSheet.ProtectContents ThenTrySheetPasswordSize = TruePassword = Base & Chr(Index)Exit FunctionEnd IfPasswordsTried = PasswordsTried + 1Next IndexEnd IfOn Error GoTo 0DisplayStatus PasswordsTriedEnd FunctionPrivate Function TryWorkbookPasswordSize( _ByVal Size As Long, _ByRef PasswordsTried As Long, _ByRef Password As String, _Optional ByVal Base As String _) As Boolean' Try unlocking the workbook with all passwords of the specified size.'' TryWorkbookPasswordSize(Size, PasswordsTried, Password, [Base])'' Size - The size of the password to try.'' PasswordsTried - The cummulative number of passwords tried thus far.'' Password - The current password.'' Base - The base password from the calling routine.Dim Index As LongOn Error Resume NextIf IsMissing(Base) Then Base = vbNullStringIf Len(Base) < Size - 1 ThenFor Index = 65 To 66If TryWorkbookPasswordSize(Size, PasswordsTried, Password, Base & Chr(Index)) ThenTryWorkbookPasswordSize = TrueExit FunctionEnd IfNext IndexElseIf Len(Base) < Size ThenFor Index = 32 To 255ActiveWorkbook.Unprotect Base & Chr(Index)If Not ActiveWorkbook.ProtectStructure And Not ActiveWorkbook.ProtectWindows ThenTryWorkbookPasswordSize = TruePassword = Base & Chr(Index)Exit FunctionEnd IfPasswordsTried = PasswordsTried + 1Next IndexEnd IfOn Error GoTo 0DisplayStatus PasswordsTriedEnd FunctionPublic Sub UnlockSheet()' Unlock the active sheet using a backdoor Excel provides where an alternate' password is created that is more limited.Dim PasswordSize As VariantDim PasswordsTried As LongDim Password As StringPasswordsTried = 0If Not ActiveSheet.ProtectContents ThenMsgBox "The sheet is already unprotected."Exit SubEnd IfOn Error Resume NextActiveSheet.Protect ""ActiveSheet.Unprotect ""On Error GoTo 0If ActiveSheet.ProtectContents ThenFor Each PasswordSize In Array(5, 4, 6, 7, 8, 3, 2, 1)If TrySheetPasswordSize(PasswordSize, PasswordsTried, Password) Then Exit ForNext PasswordSizeEnd IfIf Not ActiveSheet.ProtectContents ThenMsgBox "The sheet " & ActiveSheet.Name & " has been unprotected with password '" & Password & "'."End IfApplication.StatusBar = FalseEnd SubPublic Sub UnlockWorkbook()' Unlock the active workbook using a backdoor Excel provides where an alternate' password is created that is more limited.Dim PasswordSize As VariantDim PasswordsTried As LongDim Password As StringPasswordsTried = 0If Not ActiveWorkbook.ProtectStructure And Not ActiveWorkbook.ProtectWindows ThenMsgBox "The workbook is already unprotected."Exit SubEnd IfOn Error Resume NextActiveWorkbook.Unprotect vbNullStringOn Error GoTo 0If ActiveWorkbook.ProtectStructure Or ActiveWorkbook.ProtectWindows ThenFor Each PasswordSize In Array(5, 4, 6, 7, 8, 3, 2, 1)If TryWorkbookPasswordSize(PasswordSize, PasswordsTried, Password) Then Exit ForNext PasswordSizeEnd IfIf Not ActiveWorkbook.ProtectStructure And Not ActiveWorkbook.ProtectWindows ThenMsgBox "The workbook " & ActiveWorkbook.Name & " has been unprotected with password '" & Password & "'."End IfApplication.StatusBar = FalseEnd Sub