I am sorry that this is going to seem harsh, but what it
really looks like is that you have not a clue why the code
I gave you would work, and rather than ask or
running the code given you are inventing stupid
objections.
This seems a little silly to me. In your root node you ask
directly: Now how to detect if the code is encrypted the
first time? That is, you are trying to decide if the
code is encrypted so that you know whether or not to try
to encrypt it again. I gave an answer. I mentioned that
the answer works due to general mathematical principles,
and named the specific subject which you could learn more
about to find out why it works. I even, upon prompting,
gave you code that implements my answer so you can try it
out.
Now you know that I answer a lot of questions. My answers
tend to work, and I also seem to know about some fairly
esoteric topics. So even if you don't know how my test is
supposed to work, it would be reasonable to actually try
it before saying that it can't work!
Now let me make it even easier to try my code. I will
write the framework for how you might wrap the decision
process for it in your module:
use Carp;
use Compress::Zlib;
use constant SHORT_TEXT => 50;
# This takes text that might or might not be encrypted and
# returns it decrypted. Assumes that the text was produced
# by a human or get_encrypted_code, and that decrypt will
# reverse encrypt.
sub get_decrypted_code {
my ($text, @pass_through) = @_;
(length($text) < SHORT_TEXT or text_is_normal($text))
? $text
: decrypt($text, @pass_through);
}
# This takes text that might or might not be encrypted and
# returns it encrypted. Assumes that encrypt() does
# not compress text and the text was produced by
# get_encrypted_code if it is encrypted.
sub get_encrypted_code {
my ($text, @pass_through) = @_;
if (length($text) < SHORT_TEXT) {
# Need to encrypt equivalent but longer text to be
# sure our test works later.
return encrypt(("#\n" x SHORT_TEXT) . $text, @pass_through);
}
elsif (text_is_normal($text)) {
return encrypt($text, @pass_through);
}
else {
return $text;
}
}
# Here is the test function. It takes a string and returns
# whether or not it looks like it probably is
# human-generated text. It is a fatal error if the text
# is too short to tell.
sub text_is_normal {
my $text = shift;
length($text) < SHORT_TEXT
? confess("Too little text for a reliable test")
: (length(compress($text)) < 0.8*length($text));
}
OK, now it is up to you to write encrypt, decrypt, and
to call it appropriately. Plus to do the right things
with the text.
Please do not tell me it will not work without trying
it!
Now let me explain why this works, as best as I
can, in simple English. You want a test that is able to
detect the patterns of code that people write, and can tell
that apart from encrypted text. Well what compression
algorithms do is take text, look for redundant patterns,
then describe the text as efficiently as possible. With
the kinds of text that humans work with, compression
algorithms are essentially guaranteed to succeed.
What encryption algorithms do is take text, then transform
it so that there is no semblance of a pattern left from
which you could guess the original text. This leaves the
output without the kind of redundancies that compression
algorithms are searching for. Once encrypted, there is
no way to efficiently describe the result without first
decrypting it, and compression algorithms aren't magic,
they can't figure out how to decrypt it. Therefore you
are going to fail to significantly compress encrypted
text.
There are more mathematical ways of describing this, but
the result is that seeing if an attempted compression
manages to compress text is a pretty good test to see if
that text has been encrypted. And this is true with any
decent encryption algorithm (and many non-decent ones as
well).
And this ends the thread for me. You asked a question.
I have answered it. I have explained my answer at some
length. Repeatedly. If you are unwilling to try to
understand or test the answer given, then it is not worth
my time to repeat what I have already said several times
in several ways. |