2012-12-03 4 views
-2

AES 암호화 방법을 사용하여 powershell 스크립트에서 바이트 배열 ([byte []])을 암호화해야합니다. 이 바이트 배열이 아니라 문자열 내 영어에 대한AES. powershell의 바이트 배열 암호화

Ssory를 인코딩 할 수 있도록 http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Script-410ef9df

도움말 변경 기능에서 의견

[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) 
    } 
} 

읽기 코드 : 나는 문자열을 인코딩 기능을 찾을 수 있어요. 안녕하세요, 러시아에서 :)

+0

"코드 표시"질문은 대개이 사이트에서 빨리 닫힙니다. 자신을 시험해 볼 때 구체적인 질문을하십시오. –

답변

0

나는 그것을 할 필요가 있습니다. 새 코드 :

[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()