Created
January 10, 2026 13:31
-
-
Save VictorNS69/ba00e3dcbecd84129ace58fb320b2f6c to your computer and use it in GitHub Desktop.
PowerShell script that decrypts passwords stored in the Windows Registry that have been encrypted using DPAPI (Data Protection API)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| param( | |
| [Parameter(Mandatory=$true)] | |
| [string]$RegistryPath, | |
| [Parameter(Mandatory=$true)] | |
| [string]$ValueName, | |
| [Parameter(Mandatory=$true)] | |
| [ValidateSet("LocalMachine", "CurrentUser")] | |
| [string]$Scope | |
| ) | |
| Add-Type -AssemblyName System.Security | |
| # Convert scope parameter to enum | |
| $protectionScope = [System.Security.Cryptography.DataProtectionScope]::$Scope | |
| try { | |
| # Read the encrypted blob from the registry | |
| $encryptedString = (reg query $RegistryPath /v $ValueName | findstr 'REG_SZ') | |
| if (-not $encryptedString) { | |
| Write-Host "it wasn't possible to decrypt" -ForegroundColor Red | |
| exit 1 | |
| } | |
| $encryptedString = $encryptedString.Trim() -replace '^.*REG_SZ\s+', '' | |
| $encryptedBytes = [Convert]::FromBase64String($encryptedString) | |
| $decryptedBytes = [System.Security.Cryptography.ProtectedData]::Unprotect($encryptedBytes, $null, $protectionScope) | |
| $plaintextPassword = [System.Text.Encoding]::UTF8.GetString($decryptedBytes) | |
| # Check if decrypted password is empty or null | |
| if ([string]::IsNullOrEmpty($plaintextPassword)) { | |
| Write-Host "it wasn't possible to decrypt" -ForegroundColor Red | |
| exit 1 | |
| } | |
| $plaintextPassword | |
| } | |
| catch { | |
| Write-Host "it wasn't possible to decrypt" -ForegroundColor Red | |
| exit 1 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Note
First verify the registry values:
reg query 'HKEY_LOCAL_MACHINE\SOFTWARE\'reg query 'HKEY_LOCAL_MACHINE\SOFTWARE\InterestingApp'