qshinoの日記

Powershell関係と徒然なこと

des暗号 by powershell

des暗号化 by powershell

Powershell のSecureStringはそのマシンのそのユーザーでしか復号できないので可搬性がない。

そこで、可搬性のある暗号化

# 変数定義

$inPath = "F:\Desktop\myTest.txt"
$encPath = "F:\Desktop\Enc.txt"
$decPath = "F:\Desktop\Dec.txt"

# 24bytes key
$desKey = [byte[]](
1,1,1,1,1,1,1,1,
1,1,1,2,1,1,1,1,
1,1,1,1,1,1,1,1)
# 8bytes 
$desIV = [byte[]](0,0,0,0,0,0,0,0)
$src = [System.IO.File]::ReadAllBytes($inPath)

#暗号化

$desProvider = 
  New-Object System.Security.Cryptography.TripleDESCryptoServiceProvider
$encryptor = $desProvider.CreateEncryptor($desKey,$desIV)
$ms = New-Object System.IO.MemoryStream
$cs = New-Object System.Security.Cryptography.CryptoStream(
  $ms, $encryptor, [System.Security.Cryptography.CryptoStreamMode]::Write)
$cs.Write($src,0,$src.Length)
$cs.Close()
[System.IO.File]::WriteAllBytes($encPath,$ms.ToArray())

#複合化

$src = [System.IO.File]::ReadAllBytes($encPath)
$decryptor = $desProvider.CreateDecryptor($desKey,$desIV)
$ms = New-Object System.IO.MemoryStream
$cs = New-Object System.Security.Cryptography.CryptoStream(
  $ms, $decryptor, [System.Security.Cryptography.CryptoStreamMode]::Write)
$cs.Write($src,0,$src.Length)
$cs.close()
[System.IO.File]::WriteAllBytes($decPath,$ms.ToArray())

cシャープ

using System;
using System.Security.Cryptography;
using System.Text;

class EncryptorExample
{
     private static string quote =
         "Things may come to those who wait, but only the " +
         "things left by those who hustle. -- Abraham Lincoln";

     public static void Main()
     {
         AesCryptoServiceProvider aesCSP = new AesCryptoServiceProvider();

         aesCSP.GenerateKey();
         aesCSP.GenerateIV();
         byte[] encQuote = EncryptString(aesCSP, quote);

         Console.WriteLine("Encrypted Quote:\n");
         Console.WriteLine(Convert.ToBase64String(encQuote));

         Console.WriteLine("\nDecrypted Quote:\n");
         Console.WriteLine(DecryptBytes(aesCSP, encQuote));
     }

     public static byte[] EncryptString(SymmetricAlgorithm symAlg, string inString)
     {
         byte[] inBlock = UnicodeEncoding.Unicode.GetBytes(inString);
         ICryptoTransform xfrm = symAlg.CreateEncryptor();
         byte[] outBlock = xfrm.TransformFinalBlock(inBlock, 0, inBlock.Length);

         return outBlock;
     }

     public static string DecryptBytes(SymmetricAlgorithm symAlg, byte[] inBytes)
     {
         ICryptoTransform xfrm = symAlg.CreateDecryptor();
         byte[] outBlock = xfrm.TransformFinalBlock(inBytes, 0, inBytes.Length);

         return UnicodeEncoding.Unicode.GetString(outBlock);
     }
}

aes 暗号化 by ps

##################################################
# AES 暗号化
##################################################
function AESEncrypto($Key, $PlainString){
    $KeySize = 256
    $BlockSize = 128
    $Mode = "CBC"
    $Padding = "PKCS7"

    if( $Key.Length * 8 -ne $KeySize ){
        echo "Key size error"
        return $null
    }

    # 平文をバイト配列にする
    $ByteString = [System.Text.Encoding]::UTF8.GetBytes($PlainString)

    # 鍵をバイト配列にする
    $ByteKey = [System.Text.Encoding]::UTF8.GetBytes($Key)

    # アセンブリロード
    Add-Type -AssemblyName System.Security

    # AES オブジェクトの生成
    $AES = New-Object System.Security.Cryptography.AesCryptoServiceProvider

    # 各値セット
    $AES.KeySize = $KeySize
    $AES.BlockSize = $BlockSize
    $AES.Mode = $Mode
    $AES.Padding = $Padding

    # IV 生成
    $AES.GenerateIV()

    # 生成した IV
    $IV = $AES.IV

    # 鍵セット
    $AES.Key = $ByteKey

    # 暗号化オブジェクト生成
    $Encryptor = $AES.CreateEncryptor()

    # 暗号化
    $EncryptoByte = $Encryptor.TransformFinalBlock($ByteString, 0, $ByteString.Length)

    # IV と暗号化した文字列を結合
    $DataByte = $IV + $EncryptoByte

    # 暗号化した文字列
    $EncryptoString = [System.Convert]::ToBase64String($DataByte)

    # オブジェクト削除
    $Encryptor.Dispose()
    $AES.Dispose()

    return $EncryptoString
}

aes復号

##################################################
# AES 復号化
##################################################
function AESDecrypto($Key, $EncryptoString){
    $KeySize = 256
    $BlockSize = 128
    $IVSize = $BlockSize / 8
    $Mode = "CBC"
    $Padding = "PKCS7"

    if( $Key.Length * 8 -ne $KeySize ){
        echo "Key size error"
        return $null
    }

    # 暗号文をバイト配列にする
    $ByteString = [System.Convert]::FromBase64String($EncryptoString)

    # 鍵をバイト配列にする
    $ByteKey = [System.Text.Encoding]::UTF8.GetBytes($Key)

    # IV を取り出す
    $IV = @()
    for( $i = 0; $i -lt $IVSize; $i++){
        $IV += $ByteString[$i]
    }

    # アセンブリロード
    Add-Type -AssemblyName System.Security

    # オブジェクトの生成
    $AES = New-Object System.Security.Cryptography.AesCryptoServiceProvider

    # 各値セット
    $AES.KeySize = $KeySize
    $AES.BlockSize = $BlockSize
    $AES.Mode = $Mode
    $AES.Padding = $Padding

    # IV セット
    $AES.IV = $IV

    # 鍵セット
    $AES.Key = $ByteKey

    # 復号化オブジェクト生成
    $Decryptor = $AES.CreateDecryptor()

    # 復号化
    $DecryptoByte = $Decryptor.TransformFinalBlock($ByteString, $IVSize, $ByteString.Length - $IVSize)

    # 平文にする
    $PlainString = [System.Text.Encoding]::UTF8.GetString($DecryptoByte)

    # オブジェクト削除
    $Decryptor.Dispose()
    $AES.Dispose()

    return $PlainString
}

参考

http://mtgpowershell.blogspot.jp/2012/11/blog-post_25.html?m=1