LoginSignup
0
0

More than 3 years have passed since last update.

ファイル暗号化コマンドとしてのStreamRelay.NET.exe

Last updated at Posted at 2017-06-08

暗号化コマンドとしてのStreamRelay.NET.exe

圧縮と同じように、暗号化処理もSystem.Security.Cryptography.CryptoStreamクラスになっているので、StreamRelay.NET.exeがあれば、ファイルの暗号化もできるという事

暗号化コマンドは、入力ファイル→(圧縮された)出力ファイルなので、
「local」の入力ソース(実体)にファイル、「remote」の出力ソース(実体)に圧縮ファイルとすればいい。
そして、暗号化などのデータ加工処理は、netstreamスキーム・プロキシのオプションとして実装している。
よって、暗号化時は、出力側のストリームに圧縮指定をすればいい。

つまり、

  • アルゴリズム : AES
  • モード : CBC
  • パディング : PKCS7
  • 鍵 : abcdefg
  • 鍵の攪拌 : SHA512 (実際の鍵は「abcdefgh」のSHA512の先頭128bit)
  • 暗号クラスの初期値 : 0x0000000000000000

StreamRelay.NET.exe -LocalPort 0 -LocalInputFile a.txt -RemotePort 0 -RemoteOutputFile a.txt.aes -RemoteProxy netstream:///?EncryptCipherAlgorithm=AES/CBC/PKCS7^&EncryptCipherPassword=abcdefg^&EncryptCipherPasswordHashAlgorithm=SHA512^&EncryptCipherIVHex=0000000000000000

とすると、上記の条件で a.txt→(暗号化)→a.txt.aes となる

IV(初期値)の指定が面倒の場合は指定しなくてもよい。
大抵は、0x00の配列が指定されたと同じになる。

なので、

StreamRelay.NET.exe -LocalPort 0 -LocalInputFile a.txt -RemotePort 0 -RemoteOutputFile a.txt.aes -RemoteProxy netstream:///?EncryptCipherAlgorithm=AES/CBC/PKCS7^&EncryptCipherPassword=abcdefg^&EncryptCipherPasswordHashAlgorithm=SHA512^&EncryptCipherIVHex=0000000000000000

StreamRelay.NET.exe -LocalPort 0 -LocalInputFile a.txt -RemotePort 0 -RemoteOutputFile a.txt.aes -RemoteProxy netstream:///?EncryptCipherAlgorithm=AES/CBC/PKCS7^&EncryptCipherPassword=abcdefg^&EncryptCipherPasswordHashAlgorithm=SHA512
は一緒

復号コマンドの代替

復号コマンドは、(暗号化された)入力ファイル→(復号された)出力ファイルなので、「local」の入力ソース(実体)に暗号化ファイル、「remote」の出力ソース(実体)に結果ファイルとすればいい。
そして、復号などのデータ加工処理は、netstreamスキーム・プロキシのオプションとして実装している。
よって、復号時は、入力側のストリームに復号指定をすればいい。

上記の暗号化されたファイルの復号は

StreamRelay.NET.exe -LocalPort 0 -LocalInputFile a.txt.aes -RemotePort 0 -RemoteOutputFile a.txt.out -LocalProxy netstream:///?DecryptCipherAlgorithm=AES/CBC/PKCS7^&DecryptCipherPassword=abcdefg^&DecryptCipherPasswordHashAlgorithm=SHA512^&DecryptCipherIV=0000000000000000

初期値は0x00の配列なので、無指定でもよい
つまり、

StreamRelay.NET.exe -LocalPort 0 -LocalInputFile a.txt.aes -RemotePort 0 -RemoteOutputFile a.txt.out -LocalProxy netstream:///?DecryptCipherAlgorithm=AES/CBC/PKCS7^&DecryptCipherPassword=abcdefg^&DecryptCipherPasswordHashAlgorithm=SHA512
でも一緒

暗号アルゴリズムの一覧

StreamRelay.NET.exe -ListCipher

実装しているライブラリごとに対応している「アルゴリズム名」「モード」「パディング法」が異なる

とりあえず、.NET Framework 標準は、StreamRelay.NET.exe に直接実装している

Plugin.CSharpCode.SharpZipLib.dllでは、zipパスワードを実装している

Plugin.BouncyCastle.Crypto.dllではBouncyCastleを呼び出している

鍵を攪拌するハッシュ関数の一覧

StreamRelay.NET.exe -ListHash
と、ハッシュ関数と、パスワード攪拌専用の
StreamRelay.NET.exe -ListPasswordStirring
で攪拌できるでしょう。

PBKDF1 で鍵を攪拌する

.NET Frameworkは標準のSystem.Security.Cryptography.PasswordDeriveBytesクラスで実装している

こんな感じ

  • アルゴリズム : AES
  • モード : お任せ(既定)
  • パディング : お任せ(既定)
  • 鍵 : abcdefgh
  • 暗号クラスの初期値 : お任せ(0x00の配列)
  • 鍵の攪拌 : PBKDF1
    • Salt : 0x0001020304050607
    • 暗号アルゴリズム : DES
    • ハッシュアルゴリズム : MD5
    • 初期値 : 0x00000000

暗号化(a.txt→a.txt.aes)
StreamRelay.NET.exe -LocalPort 0 -RemotePort 0 -LocalInputFile a.txt -RemoteOutputFile a.txt.aes -RemoteProxy netstream:///?EncryptCipherAlgorithm=AES^&EncryptCipherPassword=abcdefgh^&EncryptCipherPasswordHashAlgorithm=PBKDF1/0001020304050607/DES/MD5/00000000
復号(a.txt.aes→a.txt.aes.out)
StreamRelay.NET.exe -LocalPort 0 -RemotePort 0 -LocalInputFile a.txt.aes -RemoteOutputFile a.txt.aes.out -LocalProxy netstream:///?DecryptCipherAlgorithm=AES^&DecryptCipherPassword=abcdefgh^&DecryptCipherPasswordHashAlgorithm=PBKDF1/0001020304050607/DES/MD5/00000000

PBKDF2 で鍵を攪拌する

.NET Frameworkは標準のSystem.Security.Cryptography.Rfc2898DeriveBytesクラスで実装している

こんな感じ

  • アルゴリズム : AES
  • モード : お任せ(既定)
  • パディング : お任せ(既定)
  • 鍵 : abcdefgh
  • 暗号クラスの初期値 : お任せ(0x00の配列)
  • 鍵の攪拌 : PBKDF2
    • Salt : 0x0001020304050607
    • 回数 : 1000

暗号化(a.txt→a.txt.aes)
StreamRelay.NET.exe -localport 0 -remoteport 0 -localinputfile a.txt -remoteoutputfile a.txt.aes -remoteproxy netstream:///?EncryptCipherAlgorithm=AES^&EncryptCipherPassword=abcdefgh^&EncryptCipherPasswordHashAlgorithm=PBKDF2/0001020304050607/1000
復号(a.txt.aes→a.txt.aes.out)
StreamRelay.NET.exe -localport 0 -remoteport 0 -localinputfile a.txt.aes -remoteoutputfile a.txt.aes.out -localproxy netstream:///?DecryptCipherAlgorithm=AES^&DecryptCipherPassword=abcdefgh^&DecryptCipherPasswordHashAlgorithm=PBKDF2/0001020304050607/1000


アルゴリズムごとの制限

暗号化アルゴリズムごとの鍵長さやモード、IV などの制限はこちら


暗号通信

zebedee のような vpn の代替としての StreamRelay.NET.exe


StreamRelay.NET.exe で入力ダイアログで暗号鍵を与える

StreamRelay.NET.exe で入力ダイアログで暗号鍵を与える


ver3.5.0.0 以降

同梱の BouncyCastle/C# のバージョンが 1.8.5 に変更したことに伴い、いくつかバージョンアップした。

  • BouncyCastle/C# に合わせて、使用可能なアルゴリズム/モードの増加
  • 未実装だったモード(GMACなど)の実装
  • 未実装だった AEADMode 特有のオプションが指定可能にした など

目次へ戻る

目次というか最初の一歩

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0