Private/ConvertFrom-LPHexString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<#
.Synopsis
   Returns the plaintext for a hex string from the LastPass vault
.DESCRIPTION
   Loops through the hex characters in a string and returns a decoded string.
.EXAMPLE
   ConvertFrom-LPHexString -String $String
#>

function ConvertFrom-LPHexString
{
    [CmdletBinding()]
    Param(
        # The encrypted string to decrypt
        [Parameter(Mandatory=$true, 
                   ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $String
    )

    Process
    {
        $CharArray = @()

        Write-Verbose "Converting from Hex string"
        Write-Debug "Hex string: $String"
        for ($i = 0; $i -lt $String.Length; $i = $i + 2)
        {
            if ($BasicEncoding.GetBytes($String.Substring($i,2)) -ne [byte]16)
            {
                $CharArray += [char][System.Convert]::ToInt16($String.Substring($i,2),16)
            }
        }

        -join $CharArray
    }
}