string encryption

Hiemlick Hiemlicker via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 1 17:05:14 PDT 2016


On Friday, 1 July 2016 at 23:55:08 UTC, Adam D. Ruppe wrote:
> On Friday, 1 July 2016 at 23:23:19 UTC, Hiemlick Hiemlicker 
> wrote:
>> ok. For some reason I thought CTFE's applied to normal 
>> functions but I realize that doesn't make a lot of sense.
>
> It is applied to normal functions, just when they are used in 
> the right context.
>
> int a = factorial(3); // normal runtime
>
> static a = factorial(3); // CTFE
>
>
> Same function, but different contexts. In a static or enum 
> variable, it is CTFE'd. In a normal runtime variable, it is 
> runtime interpreted.
>
>> Yes, of course. Do D names change depending on -debug vs 
>> -release?
>
> No, he meant -g, not -debug. That puts the function names in 
> the executable, whereas they might not be there without it 
> (though they might be too... I don't think this makes much of a 
> difference)
>
> See my post here:
> http://stackoverflow.com/a/38149801/1457000
>
> tbh, I don't think encrypting strings is really worth it 
> either, they aren't hard to extract anyway.
>
>
>> I'm not too concerned about the attacker seeing them because 
>> they will have to watch every decryption to get the string(or 
>> possibly write a utility to automate the decryption).
>
> And that's not really hard.... where's the string going anyway? 
> You might want it in a separate file that you can optionally 
> encrypt or ask for it from the user.

Ok, Well, I'm curious. I've been string to write a string 
replacement that does the encryption and decryption. It could be 
used for other things like automatic language translation, etc..


Is there a way to write a wrapper around such that

mystring s = "a string that will be converted and not appear in 
binary";
writeln(s);

where s acts as a function that is called to transform the string?

There are two transformations that take place. One is at compile 
time on the string assignment. The other is at the "call site".

I can't seem to get anything to actually work correctly. writeln 
always ends up writing `mystring("whatever the first 
transformation is")`.



a test case


import std.stdio;




template enString(string s)
{
	string processor()
     {
		string q;
		foreach(c; s)
			q ~= c + 1;
		return q;
     }

     enum enString = processor();
}



template e(string s)
{
	de_enString e()
	{
		de_enString x;
		x.val = enString!(s);
		return x;
	}
}

struct de_enString
{
	string val;

	string decrypt()
	{
		string q;
		foreach(c; val)
			q ~= c - 1;
		return q;
	}

	alias this decrypt;
}

void main()
{
	auto s = e!("This is an encrypted string");
	writeln(s);
	getchar();
}

I've tried playing with opCall, opAssign, alias this, @property 
but writeln(s) never calls what I thought it should.

I thought s was short for s() if s was a property. having alias 
this decrypt and decrypt being a @property should allow this to 
work?







More information about the Digitalmars-d mailing list