Cryptage Décryptage avec PowershellJ'essaie de chiffrer une liste de JSL valides à l'aide de runprogram/powershell.Il est important qu'il ne s'agisse que d'un seul appel à powershell en raison des performances.Je pensais que cela fonctionnait, mais j'ai réalisé que les guillemets simples ou doubles me gênaient lorsqu'il s'agissait de listes imbriquées.
De plus, je ne peux PAS écrire dans un fichier et faire en sorte que Powershell le lise.fonctionne très bien, pas autorisé à le faire cependant.Je pense que je suis juste cassé sur l'évasion de JSL et C et 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);
Ingénierie de la qualité et des procédésRe : Cryptage Décryptage avec PowershellJe pensais plus à ce qui est ci-dessous, à moins que vous n'ayez besoin de conserver la structure de la liste externe dans powershell?Je ne pense pas que vous ayez même besoin du tableau powershell à moins que la fonction de chiffrement que vous utilisez ne l'exige.
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")
);Re : Cryptage Décryptage avec PowershellJ'ai essayé de le faire avec [quote], mais je n'ai pas pu le faire fonctionner à cause de la façon dont char (liste contenant des chaînes) fonctionne.Je ne peux pas obtenir de powershell pour me rendre les guillemets, il supprime simplement les guillemets, donc quand je le réinsère (s'il y a aussi des guillemets à l'intérieur), il bousille l'analyse.Je pourrais peut-être créer une fonction récursive avec profondeur afin qu'elle entre et transforme toutes les choses en ce dont j'ai besoin, puis n'enveloppe que le niveau supérieur.Si cela a du sens.
Names default to here(1);
ll = {"Fak'e\!"", {Empty(), "Str'\!"ing"}};
char(ll); // "{\!"Fak'e\!\!\!"\!", {Empty(), \!"Str'\!\!\!"ing\!"}}"
Re : Cryptage Décryptage avec PowershellPourriez-vous remplacer les guillemets dans JSL par quelque chose qui n'inclut pas de guillemet, puis les remplacer dans Powershell en utilisant replace?
Re : Cryptage Décryptage avec PowershellJe pense que l'essentiel est d'aller vers/depuis un tableau powershell.
Peut-être que cela rend les choses plus faciles à comprendre
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);