使用Powershell加密解密我正在嘗試使用 runprogram/powershell 加密有效 JSL 列表。重要的是,由於性能原因,這只是對 powershell 的一次調用。我以為我可以正常工作,但意識到單引號或雙引號在嵌套列表時會讓我感到困惑。
另外,我無法寫入文件並讓 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);
質量和工藝工程回复:使用 Powershell 加密解密我的想法更像是下面的內容,除非您需要在 powershell 中保留外部列表結構?我認為你甚至不需要 powershell 數組,除非你使用的加密函數需要它。
Names default to here(1);
to_pwsh_array = function({input, wrap_string = 1},
{DEFAULT LOCAL},
init_str = "@(\\!"" || substitute(char(input),"\!"","[quote]") || "\\!")";
);
from_pwsh_array = function({input}, {default local},
notrailingnewline = regex(rp,"(.*)\!N","\1");
replacedquotes = substitute(notrailingnewline, "[quote]", "\!"");
parsed = parse(replacedquotes);
parsed;
);
checkvalue = function( {in},
rp = Run Program(
Executable("powershell"),
Options(to_pwsh_array(in)),
ReadFunction("text")
);
out = from_pwsh_array(rp);
show(in, to_pwsh_array(in), rp, out, in == out);
in != out;
);
if(
sum(
checkvalue( Expr( {"Fake", {Empty(), "String"}} ) );
checkvalue( Expr( {"Fa'ke\!"", {Empty(), "Str'\!"ing"}} ) );
) > 0,
write("\!N\!NFailed"),
write("\!N\!NSuccess")
);回复:使用 Powershell 加密解密我嘗試使用 [quote] 來做到這一點,但由於 char(list contains strings) 的工作方式,我無法讓它工作。我無法讓 powershell 給我返回引號,它只是刪除引號,所以當我將其拉回(如果裡面也有引號)時,它就會搞砸解析。我也許可以製作一個具有深度的遞歸函數,以便它進入並將所有內容變成我需要的東西,然後只包裝頂層。如果那有意義的話。
Names default to here(1);
ll = {"Fak'e\!"", {Empty(), "Str'\!"ing"}};
char(ll); // "{\!"Fak'e\!\!\!"\!", {Empty(), \!"Str'\!\!\!"ing\!"}}"
回复:使用 Powershell 加密解密您能否用不包含引號的內容替換 JSL 中的引號,然後使用替換將它們替換回 powershell 中?
回复:使用 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);