使用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);