Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello Everybody,

The Original Question was already answered in the best possible way by @Lupp and @ajlittoz; All hope for the OQ at this moment is located somewhere in her own memory… You know you can do it!!

Building forth on the “Password Spreadsheet” provided in the answer from @Lupp, i would like to propose in this answer box a faster way to test the Passwords by which to open an encrypted LibreOffice document.

Instead of calling LoadComponentFromURL() for each Password to test, this proposal calls LoadComponentFromURL() only once for the encrypted document to be opened, and then uses a Password InteractionHandler to keep on re-entering new passwords for that Document, until the correct Password was entered, or until the “list of Passwords to try” has been depleted.

The “list of Passwords to try” is yielded by the function YieldNextPassword().

The intention is to make it into a “smart yielder” that generates the most promising passwords, based on a prefilled spreadheet containing any fragmentary bit of information about the real password from the user’s memory.

NB. i tested this method on my old laptop, and found that it is indeed faster ( about 30% faster ) than the method of calling LoadComponentFromURL() each time.

Code:

REM  *****  BASIC  *****
Option Explicit


Sub Main
REM Execute Main() to test the method TryPasswords() for the Document URL specified below:
REM Could take a long time to complete, depending on the value for g_MaximumPasswordTries specified below: 
REM To stop the macro prematurely, click on the red Stop button in the Macro Editor window.
REM See: https://ask.libreoffice.org/en/question/145716/i-forgot-my-password-to-a-very-important-document-please-please-help-ill-get-fired/

    Dim strURL As String   : strURL = "/home/strider/Desktop/testPW.odt"    REM Set here the encrypted Document to open.

    g_MaximumPasswordTries = 100        REM Set here the maximum number of tries after which the process will be aborted.

    Dim lStartTime As Long : lStartTime = Timer             REM Record Runtime Duration.
    Dim oDoc as Object     : oDoc = TryPasswords( strURL )  REM Runtime1 = Try Passwords with interaction handler.
'   Dim oDoc as Object     : oDoc = TryPasswords2( strURL ) REM Runtime2 = Try Passwords with repeated calls to LoadComponentFromURL()'
    Dim lEndTime As Long   : lEndTime = Timer
    Dim lTotalSecs As Long : lTotalSecs = lEndTime - lStartTime

    Dim strMsg As String   : strMsg = "Document : '" & strURL & "'." & chr(13)
    strMsg = strMsg & "Password Found : " & g_CurrentPasswordTry & chr(13)
    strMsg = strMsg & "Passwords Tried: " & g_CurrentPasswordCount & chr(13)
    strMsg = strMsg & "Total Seconds  : " & lTotalSecs & chr(13)
    If Not IsNull( oDoc ) Then msgbox( strMsg, 64, "TryPasswords - SUCCESS!" ) Else msgbox( strMsg, 16, "TryPasswords - FAILED!" )
End Sub


REM #################################
Global g_CurrentPasswordTry As String
Global g_CurrentPasswordCount As Long
Global g_MaximumPasswordTries As Long
Global g_AbortTryPasswords As Boolean
REM #################################


Function TryPasswords( sURL As String )
REM Tries to open an encrypted LibreOffice Document by entering passwords from a given set.
REM Faster Method : by interaction handler.
REM <sURL>  : The File Path or URL to the encrypted Document that we are trying to open.
    g_CurrentPasswordCount = 0
    g_AbortTryPasswords    = False  
    Dim aProps(0) As New com.sun.star.beans.PropertyValue
    aProps(0).Name = "InteractionHandler"
    aProps(0).Value = CreateUnoListener( "my_PasswordInteractionHandler_", "com.sun.star.task.XInteractionHandler" )
    TryPasswords = StarDesktop.loadComponentFromURL( ConvertToURL( sURL ), "_default", 0, aProps )
End Function

Function TryPasswords2( sURL As String )
REM Tries to open an encrypted LibreOffice Document by entering passwords from a given set.
REM Slower Method : by repeated calls to loadComponentFromURL().
REM <sURL>  : The File Path or URL to the encrypted Document that we are trying to open.
    g_CurrentPasswordCount = 0
    g_AbortTryPasswords    = False
    Dim aProps(0) As New com.sun.star.beans.PropertyValue
    aProps(0).Name = "Password"
    Do  
        aProps(0).Value = YieldNextPassword()
        TryPasswords2 = StarDesktop.loadComponentFromURL( ConvertToURL( sURL ), "_default", 0, aProps )
    Loop Until g_AbortTryPasswords
End Function


Function YieldNextPassword() As String
REM Currently yields only a test-set.
REM The Stop Condition must be set by setting the Global g_AbortTryPasswords to <True>.
REM 
    If g_CurrentPasswordCount = g_MaximumPasswordTries - 1 Then REM Determine here when the true Password will be found ( for testing ).
        YieldNextPassword = "testPW"                            REM Put here the true Password ( for testing purposes ).
        g_CurrentPasswordCount = g_CurrentPasswordCount + 1
    ElseIf g_CurrentPasswordCount < g_MaximumPasswordTries Then
        YieldNextPassword = "PW" & g_CurrentPasswordCount       REM generated Passwords, all invalid.
        g_CurrentPasswordCount = g_CurrentPasswordCount + 1
    Else
        g_AbortTryPasswords = True      REM Stop Condition.
    End If
End Function


Sub my_PasswordInteractionHandler_disposing()
End Sub
Sub my_PasswordInteractionHandler_handle( xInteractionRequest As com.sun.star.task.XInteractionRequest )
REM InteractionHandler Callback set by TryPasswords().  
    Dim varException : varException = xInteractionRequest.getRequest()
    Dim oCoreReflection As Object : oCoreReflection = CreateUnoService( "com.sun.star.reflection.CoreReflection" )
    Dim oType As Object : oType = oCoreReflection.getType( varException )

    If oType.getName() = "com.sun.star.task.DocumentPasswordRequest2" Then
        Dim pwMode : pwMode = com.sun.star.task.PasswordRequestMode
        If varException.Mode = pwMode.PASSWORD_ENTER Or varException.Mode = pwMode.PASSWORD_REENTER Then

            Dim aContinuations : aContinuations = xInteractionRequest.getContinuations()
            Dim oContinuation As Object
            Dim strPassword As String

            For Each oContinuation In aContinuations
                If g_AbortTryPasswords Then                     REM Yielder Aborted .. No succes.
                    If hasUNOinterfaces( oContinuation, "com.sun.star.task.XInteractionAbort" ) Then
                        oContinuation.select()
                        Stop
                    End If
                ElseIf hasUNOinterfaces( oContinuation, "com.sun.star.task.XInteractionPassword2" ) Then

                    g_CurrentPasswordTry = yieldNextPassword()                      REM Next Password Attempt.

                    If Not g_AbortTryPasswords Then
                        If varException.IsRequestPasswordToModify() Then            REM Password is for Editing the Document.
                            oContinuation.setPasswordToModify( g_CurrentPasswordTry )
                        Else
                            oContinuation.setPassword( g_CurrentPasswordTry )       REM Password is for Opening the Document.
                        End If
                        oContinuation.select()
                        Exit For
                    End If
                End If
            Next
        End If
    End If
End Sub

With Regards, lib

Hello Everybody,

The Original Question was already answered in the best possible way by @Lupp and @ajlittoz; All hope for the OQ at this moment is located somewhere in her own memory… You know you can do it!!

Building forth on the “Password Spreadsheet” provided in the answer from @Lupp, i would like to propose in this answer box a faster way to test the Passwords by which to open an encrypted LibreOffice document.

Instead of calling LoadComponentFromURL() for each Password to test, this proposal calls LoadComponentFromURL() only once for the encrypted document to be opened, and then uses a Password InteractionHandler to keep on re-entering new passwords for that Document, until the correct Password was entered, or until the “list of Passwords to try” has been depleted.

The “list of Passwords to try” is yielded by the function YieldNextPassword().

The intention is to make it into a “smart yielder” that generates the most promising passwords, based on a prefilled spreadheet containing any fragmentary bit of information about the real password from the user’s memory.

NB. i tested this method on my old laptop, and found that it is indeed faster ( about 30% faster ) than the method of calling LoadComponentFromURL() each time.

Code:

REM  *****  BASIC  *****
Option Explicit


Sub Main
REM Execute Main() to test the method TryPasswords() for the Document URL specified below:
REM Could take a long time to complete, depending on the value for g_MaximumPasswordTries specified below: 
REM To stop the macro prematurely, click on the red Stop button in the Macro Editor window.
REM See: https://ask.libreoffice.org/en/question/145716/i-forgot-my-password-to-a-very-important-document-please-please-help-ill-get-fired/

    Dim strURL As String   : strURL = "/home/strider/Desktop/testPW.odt"  "/home/user/Desktop/testPW.odt"   REM Set here the encrypted Document to open.

    g_MaximumPasswordTries = 100        REM Set here the maximum number of tries after which the process will be aborted.

    Dim lStartTime As Long : lStartTime = Timer             REM Record Runtime Duration.
    Dim oDoc as Object     : oDoc = TryPasswords( strURL )  REM Runtime1 = Try Passwords with interaction handler.
'   Dim oDoc as Object     : oDoc = TryPasswords2( strURL ) REM Runtime2 = Try Passwords with repeated calls to LoadComponentFromURL()'
    Dim lEndTime As Long   : lEndTime = Timer
    Dim lTotalSecs As Long : lTotalSecs = lEndTime - lStartTime

    Dim strMsg As String   : strMsg = "Document : '" & strURL & "'." & chr(13)
    strMsg = strMsg & "Password Found : " & g_CurrentPasswordTry & chr(13)
    strMsg = strMsg & "Passwords Tried: " & g_CurrentPasswordCount & chr(13)
    strMsg = strMsg & "Total Seconds  : " & lTotalSecs & chr(13)
    If Not IsNull( oDoc ) Then msgbox( strMsg, 64, "TryPasswords - SUCCESS!" ) Else msgbox( strMsg, 16, "TryPasswords - FAILED!" )
End Sub


REM #################################
Global g_CurrentPasswordTry As String
Global g_CurrentPasswordCount As Long
Global g_MaximumPasswordTries As Long
Global g_AbortTryPasswords As Boolean
REM #################################


Function TryPasswords( sURL As String )
REM Tries to open an encrypted LibreOffice Document by entering passwords from a given set.
REM Faster Method : by interaction handler.
REM <sURL>  : The File Path or URL to the encrypted Document that we are trying to open.
    g_CurrentPasswordCount = 0
    g_AbortTryPasswords    = False  
    Dim aProps(0) As New com.sun.star.beans.PropertyValue
    aProps(0).Name = "InteractionHandler"
    aProps(0).Value = CreateUnoListener( "my_PasswordInteractionHandler_", "com.sun.star.task.XInteractionHandler" )
    TryPasswords = StarDesktop.loadComponentFromURL( ConvertToURL( sURL ), "_default", 0, aProps )
End Function

Function TryPasswords2( sURL As String )
REM Tries to open an encrypted LibreOffice Document by entering passwords from a given set.
REM Slower Method : by repeated calls to loadComponentFromURL().
REM <sURL>  : The File Path or URL to the encrypted Document that we are trying to open.
    g_CurrentPasswordCount = 0
    g_AbortTryPasswords    = False
    Dim aProps(0) As New com.sun.star.beans.PropertyValue
    aProps(0).Name = "Password"
    Do  
        aProps(0).Value = YieldNextPassword()
        TryPasswords2 = StarDesktop.loadComponentFromURL( ConvertToURL( sURL ), "_default", 0, aProps )
    Loop Until g_AbortTryPasswords
End Function


Function YieldNextPassword() As String
REM Currently yields only a test-set.
REM The Stop Condition must be set by setting the Global g_AbortTryPasswords to <True>.
REM 
    If g_CurrentPasswordCount = g_MaximumPasswordTries - 1 Then REM Determine here when the true Password will be found ( for testing ).
        YieldNextPassword = "testPW"                            REM Put here the true Password ( for testing purposes ).
        g_CurrentPasswordCount = g_CurrentPasswordCount + 1
    ElseIf g_CurrentPasswordCount < g_MaximumPasswordTries Then
        YieldNextPassword = "PW" & g_CurrentPasswordCount       REM generated Passwords, all invalid.
        g_CurrentPasswordCount = g_CurrentPasswordCount + 1
    Else
        g_AbortTryPasswords = True      REM Stop Condition.
    End If
End Function


Sub my_PasswordInteractionHandler_disposing()
End Sub
Sub my_PasswordInteractionHandler_handle( xInteractionRequest As com.sun.star.task.XInteractionRequest )
REM InteractionHandler Callback set by TryPasswords().  
    Dim varException : varException = xInteractionRequest.getRequest()
    Dim oCoreReflection As Object : oCoreReflection = CreateUnoService( "com.sun.star.reflection.CoreReflection" )
    Dim oType As Object : oType = oCoreReflection.getType( varException )

    If oType.getName() = "com.sun.star.task.DocumentPasswordRequest2" Then
        Dim pwMode : pwMode = com.sun.star.task.PasswordRequestMode
        If varException.Mode = pwMode.PASSWORD_ENTER Or varException.Mode = pwMode.PASSWORD_REENTER Then

            Dim aContinuations : aContinuations = xInteractionRequest.getContinuations()
            Dim oContinuation As Object
            Dim strPassword As String

            For Each oContinuation In aContinuations
                If g_AbortTryPasswords Then                     REM Yielder Aborted .. No succes.
                    If hasUNOinterfaces( oContinuation, "com.sun.star.task.XInteractionAbort" ) Then
                        oContinuation.select()
                        Stop
                    End If
                ElseIf hasUNOinterfaces( oContinuation, "com.sun.star.task.XInteractionPassword2" ) Then

                    g_CurrentPasswordTry = yieldNextPassword()                      REM Next Password Attempt.

                    If Not g_AbortTryPasswords Then
                        If varException.IsRequestPasswordToModify() Then            REM Password is for Editing the Document.
                            oContinuation.setPasswordToModify( g_CurrentPasswordTry )
                        Else
                            oContinuation.setPassword( g_CurrentPasswordTry )       REM Password is for Opening the Document.
                        End If
                        oContinuation.select()
                        Exit For
                    End If
                End If
            Next
        End If
    End If
End Sub

With Regards, lib

Hello Everybody,

The Original Question was already answered in the best possible way by @Lupp and @ajlittoz; All hope for the OQ at this moment is located somewhere in her own memory… You know you can do it!!

Building forth on the “Password Spreadsheet” provided in the answer from @Lupp, i would like to propose in this answer box a faster way to test the Passwords by which to open an encrypted LibreOffice document.

Instead of calling LoadComponentFromURL() for each Password to test, this proposal calls LoadComponentFromURL() only once for the encrypted document to be opened, and then uses a Password InteractionHandler to keep on re-entering new passwords for that Document, until the correct Password was entered, or until the “list of Passwords to try” has been depleted.

The “list of Passwords to try” is yielded by the function YieldNextPassword().

The intention is to make it into a “smart yielder” that generates the most promising passwords, based on a prefilled spreadheet containing any fragmentary bit of information about the real password from the user’s memory.

NB. i tested this method on my old laptop, and found that it is indeed faster ( about 30% faster ) than the method of calling LoadComponentFromURL() each time.

Code:

REM  *****  BASIC  *****
Option Explicit


Sub Main
REM Execute Main() to test the method TryPasswords() for the Document URL specified below:
REM Could take a long time to complete, depending on the value for g_MaximumPasswordTries specified below: 
REM To stop the macro prematurely, click on the red Stop button in the Macro Editor window.
REM See: https://ask.libreoffice.org/en/question/145716/i-forgot-my-password-to-a-very-important-document-please-please-help-ill-get-fired/

    Dim strURL As String   : strURL = "/home/user/Desktop/testPW.odt"   REM Set here the encrypted Document to open.

    g_MaximumPasswordTries = 100        REM Set here the maximum number of tries after which the process will be aborted.

    Dim lStartTime As Long : lStartTime = Timer             REM Record Runtime Duration.
    Dim oDoc as Object     : oDoc = TryPasswords( strURL )  REM Runtime1 = Try Passwords with interaction handler.
'   Dim oDoc as Object     : oDoc = TryPasswords2( strURL ) REM Runtime2 = Try Passwords with repeated calls to LoadComponentFromURL()'
    Dim lEndTime As Long   : lEndTime = Timer
    Dim lTotalSecs As Long : lTotalSecs = lEndTime - lStartTime

    Dim strMsg As String   : strMsg = "Document : '" & strURL & "'." & chr(13)
    strMsg = strMsg & "Password Found : " & g_CurrentPasswordTry & chr(13)
    strMsg = strMsg & "Passwords Tried: " & g_CurrentPasswordCount & chr(13)
    strMsg = strMsg & "Total Seconds  : " & lTotalSecs & chr(13)
    If Not IsNull( oDoc ) Then msgbox( strMsg, 64, "TryPasswords - SUCCESS!" ) Else msgbox( strMsg, 16, "TryPasswords - FAILED!" )
End Sub


REM #################################
Global g_CurrentPasswordTry As String
Global g_CurrentPasswordCount As Long
Global g_MaximumPasswordTries As Long
Global g_AbortTryPasswords As Boolean
REM #################################


Function TryPasswords( sURL As String )
REM Tries to open an encrypted LibreOffice Document by entering passwords from a given set.
REM Faster Method : by interaction handler.
REM <sURL>  : The File Path or URL to the encrypted Document that we are trying to open.
    g_CurrentPasswordCount = 0
    g_AbortTryPasswords    = False  
    Dim aProps(0) As New com.sun.star.beans.PropertyValue
    aProps(0).Name = "InteractionHandler"
    aProps(0).Value = CreateUnoListener( "my_PasswordInteractionHandler_", "com.sun.star.task.XInteractionHandler" )
    TryPasswords = StarDesktop.loadComponentFromURL( ConvertToURL( sURL ), "_default", 0, aProps )
End Function

Function TryPasswords2( sURL As String )
REM Tries to open an encrypted LibreOffice Document by entering passwords from a given set.
REM Slower Method : by repeated calls to loadComponentFromURL().
REM <sURL>  : The File Path or URL to the encrypted Document that we are trying to open.
    g_CurrentPasswordCount = 0
    g_AbortTryPasswords    = False
    Dim aProps(0) As New com.sun.star.beans.PropertyValue
    aProps(0).Name = "Password"
    Do  
        aProps(0).Value = YieldNextPassword()
        TryPasswords2 = StarDesktop.loadComponentFromURL( ConvertToURL( sURL ), "_default", 0, aProps )
    Loop Until g_AbortTryPasswords
End Function


Function YieldNextPassword() As String
REM Currently yields only a test-set.
REM The Stop Condition must be set by setting the Global g_AbortTryPasswords to <True>.
REM 
    If g_CurrentPasswordCount = g_MaximumPasswordTries - 1 Then REM Determine here when the true Password will be found ( for testing ).
        YieldNextPassword = "testPW"                            REM Put here the true Password ( for testing purposes ).
        g_CurrentPasswordCount = g_CurrentPasswordCount + 1
    ElseIf g_CurrentPasswordCount < g_MaximumPasswordTries Then
        YieldNextPassword = "PW" & g_CurrentPasswordCount       REM generated Passwords, all invalid.
        g_CurrentPasswordCount = g_CurrentPasswordCount + 1
    Else
        g_AbortTryPasswords = True      REM Stop Condition.
    End If
End Function


Sub my_PasswordInteractionHandler_disposing()
End Sub
Sub my_PasswordInteractionHandler_handle( xInteractionRequest As com.sun.star.task.XInteractionRequest )
REM InteractionHandler Callback set by TryPasswords().  
    Dim varException : varException = xInteractionRequest.getRequest()
    Dim oCoreReflection As Object : oCoreReflection = CreateUnoService( "com.sun.star.reflection.CoreReflection" )
    Dim oType As Object : oType = oCoreReflection.getType( varException )

    If oType.getName() = "com.sun.star.task.DocumentPasswordRequest2" Then
        Dim pwMode : pwMode = com.sun.star.task.PasswordRequestMode
        If varException.Mode = pwMode.PASSWORD_ENTER Or varException.Mode = pwMode.PASSWORD_REENTER Then

            Dim aContinuations : aContinuations = xInteractionRequest.getContinuations()
            Dim oContinuation As Object
            Dim strPassword As String

            For Each oContinuation In aContinuations
                If g_AbortTryPasswords Then                     REM Yielder Aborted .. No succes.
                    If hasUNOinterfaces( oContinuation, "com.sun.star.task.XInteractionAbort" ) Then
                        oContinuation.select()
                        Stop
                    End If
                ElseIf hasUNOinterfaces( oContinuation, "com.sun.star.task.XInteractionPassword2" ) Then

                    g_CurrentPasswordTry = yieldNextPassword()                      REM Next Password Attempt.

                    If Not g_AbortTryPasswords Then
                        If varException.IsRequestPasswordToModify() Then            REM Password is for Editing the Document.
                            oContinuation.setPasswordToModify( g_CurrentPasswordTry )
                        Else
                            oContinuation.setPassword( g_CurrentPasswordTry )       REM Password is for Opening the Document.
                        End If
                        oContinuation.select()
                        Exit For
                    End If
                End If
            Next
        End If
    End If
End Sub

With Regards, lib