<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Encrypting Decrypting with Powershell in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439646#M68806</link>
    <description>&lt;P&gt;Could you substitute the quotes in JSL with something that doesn't include a quote and then substitute them back within powershell using replace?&lt;/P&gt;</description>
    <pubDate>Wed, 24 Nov 2021 18:26:42 GMT</pubDate>
    <dc:creator>ih</dc:creator>
    <dc:date>2021-11-24T18:26:42Z</dc:date>
    <item>
      <title>Encrypting Decrypting with Powershell</title>
      <link>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439389#M68785</link>
      <description>&lt;P&gt;I'm trying to encrypt a list of valid JSL using runprogram/powershell.&amp;nbsp; It's important that this is only a single call to powershell because of performance.&amp;nbsp; I thought I had it working but realized that single or double quotes messes me up when they're nested lists.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I can NOT write to a file and have powershell read that.&amp;nbsp; works great, not allowed to do it though.&amp;nbsp; I think I'm just broken on the escaping of JSL and C and Powershell.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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&amp;lt;=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&amp;lt;=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&amp;lt;=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);
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jun 2023 18:06:01 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439389#M68785</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2023-06-09T18:06:01Z</dc:date>
    </item>
    <item>
      <title>Re: Encrypting Decrypting with Powershell</title>
      <link>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439475#M68786</link>
      <description>&lt;P&gt;I think the crux is with going to/from a powershell array.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Maybe this makes things easier to understand&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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&amp;lt;=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 &amp;amp; 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&amp;lt;=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);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Nov 2021 16:50:39 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439475#M68786</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2021-11-23T16:50:39Z</dc:date>
    </item>
    <item>
      <title>Re: Encrypting Decrypting with Powershell</title>
      <link>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439646#M68806</link>
      <description>&lt;P&gt;Could you substitute the quotes in JSL with something that doesn't include a quote and then substitute them back within powershell using replace?&lt;/P&gt;</description>
      <pubDate>Wed, 24 Nov 2021 18:26:42 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439646#M68806</guid>
      <dc:creator>ih</dc:creator>
      <dc:date>2021-11-24T18:26:42Z</dc:date>
    </item>
    <item>
      <title>Re: Encrypting Decrypting with Powershell</title>
      <link>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439666#M68808</link>
      <description>&lt;P&gt;I tried to do that with [quote], but I couldn't get it to work because of the the way char(list containing strings) works.&amp;nbsp; I can't get powershell to give me quotes back, it just strips the quotes, so when I pull it back in (if it has quotes inside as well) then it screws up the parse.&amp;nbsp; I could maybe make a recursive function with depth so that it goes in and turns all things into what I need then only wrap the top level.&amp;nbsp; If that makes any sense.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names default to here(1);
ll = {"Fak'e\!"", {Empty(), "Str'\!"ing"}};
char(ll); // "{\!"Fak'e\!\!\!"\!", {Empty(), \!"Str'\!\!\!"ing\!"}}"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Nov 2021 19:33:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439666#M68808</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2021-11-24T19:33:51Z</dc:date>
    </item>
    <item>
      <title>Re: Encrypting Decrypting with Powershell</title>
      <link>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439686#M68810</link>
      <description>&lt;P&gt;I was thinking more like what is below, unless you need to keep the outer list structure in powershell?&amp;nbsp; I don't think you even need the powershell array unless the encryption function you're using requires it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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"}} ) ); 
	) &amp;gt; 0,
	write("\!N\!NFailed"), 
	write("\!N\!NSuccess")
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Nov 2021 21:02:32 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Encrypting-Decrypting-with-Powershell/m-p/439686#M68810</guid>
      <dc:creator>ih</dc:creator>
      <dc:date>2021-11-29T21:02:32Z</dc:date>
    </item>
  </channel>
</rss>

