Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Created August 17, 2023 12:38
Show Gist options
  • Save mavaddat/ef4b054dd09ec876d78762b7684e1eea to your computer and use it in GitHub Desktop.
Save mavaddat/ef4b054dd09ec876d78762b7684e1eea to your computer and use it in GitHub Desktop.
decrypt oracle sql developer password using PowerShell
function Get-DecryptedPassword {
[CmdletBinding()]
param (
[Parameter(ValueFromPipelineByPropertyName)]
[securestring]
$EncryptedPassword = (Get-Content -Path 'C:\Users\B0649033\AppData\Roaming\SQL Developer\system*\o.jdeveloper.db.connection\connections.json' | ConvertFrom-Json | ForEach-Object { $_.connections.info } | Out-ConsoleGridView -Title "Choose connection" -OutputMode Single | ConvertTo-SecureString -AsPlainText -Force ),
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$DbSystemId = (([xml]$(Get-Content -Path "$env:APPDATA\SQL Developer\system*\o.sqldeveloper\product-preferences.xml")) | ForEach-Object { $_.preferences.value } | Where-Object -FilterScript { $_.n -eq 'db.system.id' } | Select-Object -ExpandProperty v -Unique -First 1)
)
[byte[]]$PasswordBytes = [System.Convert]::FromBase64String(($EncryptedPassword | ConvertFrom-SecureString -AsPlainText))
[byte[]]$Salt = [System.Convert]::FromHexString('051399429372e8ad') # Magic salt
$Key = $DbSystemId + $Salt
try {
$MessageDigestFive = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
foreach ($null in 0..42) { $Key = $MessageDigestFive.ComputeHash($Key) } # 43 rounds of hashing
}
finally {
$MessageDigestFive.Dispose()
}
$SecretKey = $Key[0..7] # First 8 bytes
$InitializationVector = $Key[8..($Key.Length - 1)] # Remaining bytes
try {
[System.Security.Cryptography.DESCryptoServiceProvider]$provider = New-Object -TypeName System.Security.Cryptography.DESCryptoServiceProvider
try {
[System.Security.Cryptography.ICryptoTransform]$transform = $provider.CreateDecryptor($SecretKey, $InitializationVector)
try {
[System.IO.MemoryStream]$memoryStream = New-Object -TypeName System.IO.MemoryStream -ArgumentList @($PasswordBytes, $true)
try {
[System.Security.Cryptography.CryptoStream]$cryptoStream = New-Object -TypeName System.Security.Cryptography.CryptoStream($memoryStream, $transform, [System.Security.Cryptography.CryptoStreamMode]::Write)
$cryptoStream.Write($PasswordBytes, 0, $PasswordBytes.Length)
try {
[System.IO.StreamReader]$streamReader = New-Object -TypeName System.IO.StreamReader($cryptoStream)
$streamReader.ReadToEnd()
}
finally {
$streamReader.Dispose()
}
}
finally {
$cryptoStream.Dispose()
}
}
finally {
$memoryStream.Dispose()
}
}
finally {
$transform.Dispose()
}
}
finally {
$provider.Dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment