Powershellを使用した暗号化と復号化runprogram / powershellを使用して有効なJSLのリストを暗号化しようとしています。パフォーマンスのため、これがPowerShellへの1回の呼び出しであることが重要です。私はそれが機能していると思っていましたが、一重引用符または二重引用符がネストされたリストである場合、私を混乱させることに気づきました。
また、ファイルに書き込んでPowerShellに読み取らせることもできません。うまく機能しますが、それを行うことは許可されていません。私はJSLとCとPowershellの脱出にちょうど壊れていると思います。
Names default to here(1);
encryption_key = J(16, 1, randominteger(0, 255));
unencrypted_input = {"Fa'ke\!"", {Empty(), "Str'\!"ing"}}; // this one breaks me
unencrypted_input = {"Fake", {Empty(), "String"}}; // this one is fine
print("encryptng");
init_str = "@("; // creating a powershell array
for(i=1, i<=nitems(unencrypted_input), i++,
if(i!=1, init_str ||= ", ");
item = unencrypted_input[i];
// for encrypt we want to put it in quotes so we can pull it out later so we can parse;
if( isstring(item), item = "\!""||char(item)||"\!"");
// because of the way runprogram works we have to C escape the quotes as well
init_str ||= "\\!""||substitute(char(item), "\!"", "'")||"\\!"";
);
init_str ||= ")";
exec_str = init_str || " | ConvertTo-SecureString -AsPlainText -Force"
|| " | ConvertFrom-SecureString -Key " || Substitute(char(encryption_key), "[", "(", "]", ")");
write("\!n"||exec_str);
rp = Run Program(
Executable("powershell"),
Options(exec_str),
ReadFunction("text")
);
print("");
show(rp);
encrypted_list = words(rp, "\!n\!r");
print("decrypting");
// now to decrypt
init_str = "@("; // creating a powershell array
for(i=1, i<=nitems(encrypted_list), i++,
if(i!=1, init_str ||= ", ");
item = encrypted_list[i];
// because of the way runprogram works we have to C escape the quotes as well
init_str ||= "\\!""||substitute(char(item), "\!"", "'")||"\\!"";
);
init_str ||= ")";
exec_str = init_str || " | ConvertTo-SecureString -Key " || Substitute(char(encryption_key), "[", "(", "]", ")")
|| " | ForEach-Object {"// doing this in a loop in case of multiple lines
|| "$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($_);"
|| "[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)}";
write("\!n"||exec_str);
rp = Run Program(
Executable("powershell"),
Options(exec_str),
ReadFunction("text")
);
print("");
show(rp);
dec_list = Words(rp, "\!n\!r");
for(i=1, i<=nitems(dec_list), i++,
r = substitute(dec_list[i], "'", "\!"");
r = parse(r); // parse it because we wrapped everything
dec_list[i] = r;
);
show(dec_list, unencrypted_input);
show(dec_list == unencrypted_input);
スクリプティングRe:Powershellを使用した暗号化の復号化重要なのは、PowerShellアレイに出入りすることだと思います。
多分これは物事を理解しやすくします
Names default to here(1);
//unencrypted_input = {"Fa'ke\!"", {Empty(), "Str'\!"ing"}}; // this one breaks me
unencrypted_input = {"Fake", {Empty(), "String"}}; // this one is fine
to_pwsh_array = function({input, wrap_string = 1},
// wrap string because the inputs for decrypt aren't valid JSL so we don't want to wrap those
{DEFAULT LOCAL},
init_str = "@("; // creating a powershell array
for(i=1, i<=nitems(input), i++,
if(i!=1, init_str ||= ", ");
item = input[i];
// for encrypt we want to put it in quotes so we can pull it out later so we can parse;
if( wrap_string & isstring(item), item = "\!""||char(item)||"\!"");
// because of the way runprogram works we have to C escape the quotes as well
init_str ||= "\\!""||substitute(char(item), "\!"", "'")||"\\!"";
);
init_str ||= ")";
);
from_pwsh_array = function({input},
final_list = Words(input, "\!n\!r");
for(i=1, i<=nitems(final_list), i++,
r = substitute(final_list[i], "'", "\!"");
r = parse(r); // parse it because we wrapped everything
final_list[i] = r;
);
return(final_list);
);
rp = Run Program(
Executable("powershell"),
Options(to_pwsh_array(unencrypted_input)),
ReadFunction("text")
);
out = from_pwsh_array(rp);
show(out == unencrypted_input);Re:Powershellを使用した暗号化の復号化JSLの引用符を引用符を含まないものに置き換えてから、replaceを使用してPowerShell内でそれらを元に戻すことはできますか?
Re:Powershellを使用した暗号化の復号化[quote]でそれをやろうとしましたが、char(文字列を含むリスト)の動作方法が原因で動作させることができませんでした。PowerShellを使用して引用符を返すことはできません。引用符を削除するだけなので、(引用符も含まれている場合は)引き戻すと、解析が失敗します。深みのある再帰関数を作成して、すべてのものを必要なものに変換し、トップレベルのみをラップすることができます。それが理にかなっているなら。
Names default to here(1);
ll = {"Fak'e\!"", {Empty(), "Str'\!"ing"}};
char(ll); // "{\!"Fak'e\!\!\!"\!", {Empty(), \!"Str'\!\!\!"ing\!"}}"