Skip to content

Instantly share code, notes, and snippets.

@mpomery
Created August 15, 2018 08:47
Show Gist options
  • Save mpomery/6872c959147e3e4e86f6bdefb0c85fba to your computer and use it in GitHub Desktop.
Save mpomery/6872c959147e3e4e86f6bdefb0c85fba to your computer and use it in GitHub Desktop.
DPAPI Encryption/Decryption
function Get-DpapiEncryptedValue([string] $valueToEncrypt) {
Add-Type -AssemblyName System.Security
$clearBytes = [System.Text.Encoding]::UTF8.GetBytes($valueToEncrypt)
$encryptedBytes = [System.Security.Cryptography.ProtectedData]::Protect($clearBytes, $null, [System.Security.Cryptography.DataProtectionScope]::LocalMachine)
return [System.Convert]::ToBase64String($encryptedBytes)
}
function Get-DpapiDecryptedValue([string] $valueToDecrypt) {
Add-Type -AssemblyName System.Security
$clearBytes = [System.Convert]::FromBase64String($valueToDecrypt)
$decryptedBytes = [System.Security.Cryptography.ProtectedData]::Unprotect($clearBytes, $null, [System.Security.Cryptography.DataProtectionScope]::LocalMachine)
return [System.Text.Encoding]::UTF8.GetString($decryptedBytes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment