【问题标题】:AES. Encrypt array of bytes in powershell [closed]AES。在powershell中加密字节数组[关闭]
【发布时间】:2012-12-03 03:19:27
【问题描述】:

我需要使用 AES 加密方法在 powershell 脚本中加密字节数组([byte[]])。我找到了编码字符串的函数:

[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使用 cmets 读取代码

帮助更改函数,使其编码字节数组而不是字符串

对不起,我的英语。来自俄罗斯的你好 :)

【问题讨论】:

  • 如果您想知道:“向我们展示代码”问题通常会在此站点上快速关闭。尝试自己,遇到困难时提出具体问题。

标签: algorithm powershell encryption aes encryption-symmetric


【解决方案1】:

我正在做我需要的事情。新代码:

[Reflection.Assembly]::LoadWithPartialName("System.Security")

 $String=$buff #ARRAY OF BYTES TO ENCODE
 $Passphrase="Pas"
  $salt="My Voice is my P455W0RD!"
  $init="Yet another key"

   $r = new-Object System.Security.Cryptography.AesManaged
   $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]
   $r.Padding="Zeros"

   $c = $r.CreateEncryptor()
   $ms = new-Object IO.MemoryStream
   $cs = new-Object Security.Cryptography.CryptoStream $ms,$c,"Write"
   $cs.Write($String, 0,$String.Length)
   $cs.Close()
   $ms.Close()
   $r.Clear()
   [byte[]]$Encrypted = $ms.ToArray()

【讨论】:

    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2010-10-22
    • 2012-08-22
    • 2020-01-31
    相关资源
    最近更新 更多