-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsDPAPI_protect_unprotect.ps1
46 lines (44 loc) · 1.52 KB
/
psDPAPI_protect_unprotect.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
38
39
40
41
42
43
44
45
46
#requires -version 5.1
# https://github.com/vcudachi/DPAPI_example
# Should work in Powershell 7
Function Protect-psDPAPI {
[CmdletBinding()]
[OutputType([String])]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[String]$string
)
process {
Return ($string | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString)
}
}
Function Unprotect-psDPAPI {
[CmdletBinding()]
[OutputType([String])]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[String]$protectedstring
)
begin {
Function Convert-HexToByte {
[CmdletBinding()]
[OutputType([byte[]])]
param(
[Parameter(Mandatory = $true)] [String]$Value
)
$bytes = [byte[]]::new($Value.Length / 2)
For ($i = 0; $i -lt $Value.Length; $i += 2) {
$bytes[$i / 2] = [Convert]::ToByte($Value.Substring($i, 2), 16)
}
Return , $bytes
}
}
process {
$protectedstring_ba = Convert-HexToByte -Value $protectedstring
$string_ba = [System.Security.Cryptography.ProtectedData]::Unprotect($protectedstring_ba, $null, [System.Security.Cryptography.DataProtectionScope]::CurrentUser)
$string = [System.Text.Encoding]::Unicode.GetString($string_ba)
Return $string
}
}