首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >powershell非对称加密/解密函数

powershell非对称加密/解密函数
EN

Stack Overflow用户
提问于 2013-06-07 23:41:09
回答 1查看 13.5K关注 0票数 3

我无法找到好的powershell函数来利用非对称加密,所以我创建了以下内容。因为我是个隐秘的菜鸟,所以我想得到任何改进方面的反馈。请注意,这些函数是非常基本的。不存在错误检查,解密后的写主机也几乎没有必要。只想在添加保护内存之类的东西之前建立核心功能。

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

代码语言:javascript
复制
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
}
EN

回答 1

Stack Overflow用户

发布于 2013-11-26 18:35:58

我知道这很古老。我从你在这里的起点开始,增加了一些项目。我试图在适当的地方进行清理,并使用变量名称,这可能有助于其他人更容易地理解这些内容。

加密:

代码语言:javascript
复制
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 
}

解密:

代码语言:javascript
复制
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

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16994452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档