読者です 読者をやめる 読者になる 読者になる

qshinoの日記

Powershell関係と徒然なこと

des暗号化その2 by powershell

powershell

DES暗号化その2 by powershell

使い方から

$in="password"
$key="keyword"
$des=get-des($in,$key)
$undes=get-undes($des,$key)
$in
$des
$undes

暗号

function get-des($in, $key) {
    $des = New-Object System.Security.Cryptography.DESCryptoServiceProvider
    $des.Padding = [System.Security.Cryptography.PaddingMode]::Zeros

    $des.Key = [System.Text.Encoding]::ASCII.GetBytes($key)
    $des.IV  = [Byte[]](0,0,0,0,0,0,0,0)

    $ib = [System.Text.Encoding]::ASCII.GetBytes($in)
    $params = ($ib, 0, $ib.Length)

    $encrypter = $des.CreateEncryptor()
    $eb = [System.Security.Cryptography.ICryptoTransform].GetMethod("TransformFinalBlock").Invoke($encrypter , $params)
   $encrypter.Dispose()
   $eb
}

復号

function get-undes($in, $key) {
    $des = New-Object System.Security.Cryptography.DESCryptoServiceProvider
    $des.Padding = [System.Security.Cryptography.PaddingMode]::Zeros

    $des.Key = [System.Text.Encoding]::ASCII.GetBytes($key)
    $des.IV  = [Byte[]](0,0,0,0,0,0,0,0)
    $params = ($in, 0, $in.Length)

    $decrypter = $des.CreateDecryptor()
    $db = [System.Security.Cryptography.ICryptoTransform].GetMethod("TransformFinalBlock").Invoke($decrypter , $params)
    $decrypter.Dispose()

    $ds = [System.Text.Encoding]::ASCII.GetString($db)
    $ds
}

参考

http://ooltcloud.azurewebsites.net/201512/article_30162837.html