-2
我需要使用AES加密方法在PowerShell腳本中加密字節數組([byte []])。我發現,編碼字符串函數:AES。加密PowerShell中的字節數組
[Reflection.Assembly]::LoadWithPartialName("System.Security")
function Encrypt-String($String, $Passphrase, $salt="My Voice is my P455W0RD!", $init="Yet another key", [switch]$arrayOutput)
{
$r = new-Object System.Security.Cryptography.RijndaelManaged
$pass = [Text.Encoding]::UTF8.GetBytes($Passphrase)
$salt = [Text.Encoding]::UTF8.GetBytes($salt)
$r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $pass, $salt, "SHA1", 5).GetBytes(32) #256/8
$r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash([Text.Encoding]::UTF8.GetBytes($init))[0..15]
$c = $r.CreateEncryptor()
$ms = new-Object IO.MemoryStream
$cs = new-Object Security.Cryptography.CryptoStream $ms,$c,"Write"
$sw = new-Object IO.StreamWriter $cs
$sw.Write($String)
$sw.Close()
$cs.Close()
$ms.Close()
$r.Clear()
[byte[]]$result = $ms.ToArray()
if($arrayOutput) {
return $result
} else {
return [Convert]::ToBase64String($result)
}
}
閱讀代碼註釋在http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-410ef9df
有助於改變功能,使得它編碼的字節數組,而不是一個字符串
Ssory我的英語水平。你好,來自俄羅斯:)
如果您想知道:「向我們顯示代碼」,本網站上的問題通常會很快關閉。嘗試自己,在遇到困難時詢問具體問題。 –