【问题标题】:powershell asymmetric encrypt/decrypt functionspowershell 非对称加密/解密函数
【发布时间】:2013-06-04 08:55:13
【问题描述】:

我找不到好的 powershell 函数来利用非对称加密,所以我创建了以下内容。由于我是加密新手,因此希望获得任何关于改进的反馈。需要注意的是,这些功能是非常基本的。没有错误检查,几乎不需要解密后的写入主机。只是想在添加受保护的内存等之前建立核心功能。

这已在两个系统上成功测试:Win8 w/Powershell v3 和 Win2008R2 w/Powershell v2。

Function Encrypt-Asymmetric([string]$Encrypt,[string]$CertPath,[string]$XmlExportPath)
{
    # Encrypts a string with a public key
    $pubcer = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($CertPath)
    $byteval = [System.Text.Encoding]::UTF8.GetBytes($Encrypt)
    $pubcer.PublicKey.Key.Encrypt($byteval,$true) | Export-Clixml -Path $XmlExportPath    
}

Function Decrypt-Asymmetric([string]$XmlPath,[string]$CertThumbprint)
{
    # Decrypts cipher text using the private key
    # Assumes the certificate is in the LocalMachine store
    $store = new-object System.Security.Cryptography.X509Certificates.X509Store([System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine)
    $store.open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
    $cer = $store.Certificates | %{if($_.thumbprint -eq $CertThumbprint){$_}}
    $ciphertext = Import-Clixml -Path $XmlPath
    $decryptedBytes = $cer.PrivateKey.Decrypt($ciphertext,$true)
    $ClearText = [System.Text.Encoding]::UTF8.GetString($decryptedBytes)
    Write-Host $ClearText
}

【问题讨论】:

  • 您有什么具体问题吗?仅仅征求一般性反馈并不适合这个网站。
  • @zdan。因为我是加密新手,所以我担心我是否正确使用了加密功能。将以上几行放在一起时,我遇到了这篇文章http://stackoverflow.com/questions/7539984/asymetric-cryptography-example-in-c-sharp/7540173#7540173,这启发了我采取类似的方法。另外对比上一个链接中的加密函数,我觉得即使测试成功了,也有一些遗漏。
  • 你从哪里得到你的钥匙?您是通过 powershell 自己制作的吗?
  • 在这种情况下,本地证书存储。公钥已导出,因此 Encrypt-Asymmetric 可以在其他位置使用。

标签: windows powershell certificate encryption-asymmetric


【解决方案1】:

我知道这是旧的。我采用了您在这里的起点并添加了一些项目。我尝试在适当的地方清理并使用变量名,这可能有助于让阅读本文的人更容易理解。

加密:

Function Encrypt-Asymmetric {
[CmdletBinding()]
[OutputType([System.String])]
param(
    [Parameter(Position=0, Mandatory=$true)][ValidateNotNullOrEmpty()][System.String]
    $ClearText,
    [Parameter(Position=1, Mandatory=$true)][ValidateNotNullOrEmpty()][ValidateScript({Test-Path $_ -PathType Leaf})][System.String]
    $PublicCertFilePath
)
# Encrypts a string with a public key
$PublicCert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($PublicCertFilePath)
$ByteArray = [System.Text.Encoding]::UTF8.GetBytes($ClearText)
$EncryptedByteArray = $PublicCert.PublicKey.Key.Encrypt($ByteArray,$true)
$EncryptedBase64String = [Convert]::ToBase64String($EncryptedByteArray)

Return $EncryptedBase64String 
}

解密:

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]
    $CertThumbprint
)
# Decrypts text using the private key
# Assumes the certificate is in the LocalMachine\My (Personal) Store
$Cert = Get-ChildItem cert:\LocalMachine\My | where { $_.Thumbprint -eq $CertThumbprint }
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
}

http://grokgarble.com/blog/?p=228

【讨论】:

    猜你喜欢
    • 2017-02-22
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    相关资源
    最近更新 更多