PowerShell RSA解密

时间:2018-01-21 20:24:40

标签: powershell encryption rsa

有没有办法在PowerShell中解密RSA,并将私钥作为字符串参数传递给程序,而不是使用存储在计算机上的证书?欢迎任何帮助,我的代码如下。

Function Decrypt-Asymmetric
{
[CmdletBinding()]
[OutputType([System.String])]
param(
    [Parameter(Position=0, Mandatory=$true)][ValidateNotNullOrEmpty()][System.String]
    $EncryptedBase64String,
    [Parameter(Position=1, Mandatory=$true)][ValidateNotNullOrEmpty()][System.String]
    $Cert
)

if($Cert) {
    $EncryptedByteArray = [Convert]::FromBase64String($EncryptedBase64String)
    $ClearText = [System.Text.Encoding]::UTF8.GetString($Cert.PrivateKey.Decrypt($EncryptedByteArray,$true))
}
Else {Write-Error "Certificate with thumbprint: $CertThumbprint not found!"}

Return $ClearText
}

$dec = Decrypt-Asymmetric "OZqKrQy56eclr/9pJRnguFzwKM3B8CqM+mxCqn5oGiTfh8/kp7r7Q+d+pxOBKmerkoscIsPHWdqGtds3CrmLog==" "PrivateKey as String"
$dec

1 个答案:

答案 0 :(得分:0)

我过去使用过SushiHangover的RSACrypto模块来解密电子邮件。您还可以尝试弄清楚他做了什么并在您的代码中实现它。

https://github.com/sushihangover/SushiHangover-PowerShell/tree/master/modules/SushiHangover-RSACrypto

Import-Module SushiHangover-RSACrypto
$PemFile = $HOME + '\.ssh\SSH2RSA2048_private_test_key.pem'
$cypherBase64 = "OZqKrQy56eclr/9pJRnguFzwKM3B8CqM+mxCqn5oGiTfh8/kp7r7Q+d+pxOBKmerkoscIsPHWdqGtds3CrmLog=="
$decBytes = Get-RSAClearBytes $cypherBase64 $false
$decText = Get-RSAClearText $cypherBase64 $false

干杯 格伦