Powershellでこれを行う必要があるとしましょう:
$SecurePass = Get-Content $CredPath | ConvertTo-SecureString -Key (1..16)
[String]$CleartextPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($CredPass));
$CredPath の内容は、ConvertFrom-SecureString -Key (1..16) の出力を含むファイルです。
ConvertTo-SecureString -key (1..16)
C#/.NET でこの部分を実現するにはどうすればよいですか?
の作成方法は知ってSecureString
いますが、暗号化をどのように処理すればよいかわかりません。
AES を使用して各文字を暗号化しますか、それとも文字列を復号化してから文字ごとに安全な文字列を作成しますか?
私は暗号化についてほとんど何も知りませんが、収集した情報から、C# を使用して Powershell コマンドを呼び出したいと思うかもしれません。
参考までに、AES 暗号化/復号化に関する同様の投稿を見つけました: C# での AES 暗号化の使用
アップデート
キースが投稿したリンクを確認しましたが、不明な点が他にもあります。DecryptStringFromBytes_Aes は 3 つの引数を取ります。
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
最初の引数は、暗号化されたテキストを表すバイト配列です。ここでの問題は、文字列をバイト配列でどのように表現する必要があるかということです。エンコーディングありまたはなしで表現する必要がありますか?
byte[] ciphertext = Encoding.ASCII.GetBytes(encrypted_text);
byte[] ciphertext = Encoding.UTF8.GetBytes(encrypted_text);
byte[] ciphertext = Encoding.Unicode.GetBytes(encrypted_text);
byte[] ciphertext = new byte[encrypted_password.Length * sizeof(char)];
System.Buffer.BlockCopy(encrypted_password.ToCharArray(), 0, text, 0, text.Length);
2 番目のバイト配列のキーは、単純に整数の配列である必要があります。
byte[] key = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
3 番目のバイト配列は「初期化ベクトル」です。Aes.Create() 呼び出しによって IV の byte[] がランダムに生成されるようです。読んでみると、同じ IV を使用する必要があるかもしれないことがわかりました。ConvertFrom-SecureString と ConvertTo-SecureString は単にキーを使用して暗号化/復号化できるため、IV[] がランダムである可能性がある、または静的な定義があるという仮定が残されています。
勝ち組はまだ見つかっていませんが、挑戦し続けます。