2013-10-30 44 views

回答

6
function DecodePassword([string] $encodedPassword) 
{ 
    $encodedMessage = [Convert]::FromBase64String($encodedPassword); 
    $cms = New-Object System.Security.Cryptography.Pkcs.EnvelopedCms; 
    $cms.Decode($encodedMessage); 

    $store = $null; 
    try 
    { 
     $store = New-Object System.Security.Cryptography.X509Certificates.X509Store('My', 'CurrentUser'); 
     $cms.Decrypt($store.Certificates); 
    } 
    finally 
    { 
     $store.Close(); 
    } 

    return [Text.Encoding]::UTF8.GetString($cms.ContentInfo.Content); 
} 
+1

在PowerShell中使用它。還要更改第4行將「系統」添加到命名空間。 $ cms = New-Object System.Security.Cryptography.Pkcs.EnvelopedCms; – MichaelLake

0

謝謝!這對我來說是一種嚴肅的救命!這裏有一個版本,我調整了方便的命令行調用(並添加System.Security加載):

$error.clear() 
function DecodePassword([string] $encodedPassword) 
{ 
    [System.Reflection.Assembly]::LoadWithPartialName("System.Security") | out-null; 
    $encodedMessage = [Convert]::FromBase64String($encodedPassword); 
    $cms = New-Object System.Security.Cryptography.Pkcs.EnvelopedCms; 
    $cms.Decode($encodedMessage); 

    $store = $null; 
    try 
    { 
     $store = New-Object System.Security.Cryptography.X509Certificates.X509Store('My', 'CurrentUser'); 
     $cms.Decrypt($store.Certificates); 
    } 

    finally 
    { 
     $store.Close(); 
    } 

    return [Text.Encoding]::UTF8.GetString($cms.ContentInfo.Content); 
} 

$password = DecodePassword($args[0]); 
Write-Host Decoded password: """$password"""; 

這可以簡單地援引爲:

>powershell -f DecodeRemotePassword.ps1 "PASTE_ENCODED_PASSWORD_HERE" 

(請不要贊成票因爲這只是阿列克謝優秀答案的一個調整)