challenge (and help): reverse this

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adamk
Forum Newbie
Posts: 4
Joined: Tue Sep 30, 2003 10:59 pm
Location: Granbury, TX
Contact:

challenge (and help): reverse this

Post by adamk »

Code: Select all

function do_encrypt($text, $key, $confusion = 3)
{
	global $encrypted;

	for($x=0; $x<=strlen($text)-1; $x++)
	&#123;
		for($y=0; $y<=strlen($key)-1; $y++)
			for($z=0; $z<=$confusion; $z++)
				$encrypted .= substr(base64_encode($text), $x, $z) . substr(base64_encode($key), $y, $z);
	&#125;

	return $encrypted;
&#125;
That code needs to be reversed to give a decrypted version of whatever text is passed to it.

$text is the unencrypted text, $key is the public key used in the encryption/decryptyion process, and $confusion is basically the amount of confusion to add to the generated encrypted text. If you could help me reverse this, then I would be very greatful. If you need more explanation of the confusion variable, then just pm me or contact me on some instant messenger.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

probably impossible because not all of the subject is enciphered (you're losing data). This is not entirely true; if you adjust $confusion according to the length of the plaintext it might work but then imho $confusion must be part of the key (which might be wrong, I'm definitely not an expert on ciphers).

Let me illustrate this

Code: Select all

<?php
function do_encrypt($text, $key, $confusion = 3)
{
   global $encrypted;

   for($x=0; $x<=strlen($text)-1; $x++)
   {
      for($y=0; $y<=strlen($key)-1; $y++)
         for($z=0; $z<=$confusion; $z++)
            $encrypted .= substr(base64_encode($text), $x, $z) . substr(base64_encode($key), $y, $z);
   }

   return $encrypted;
}
$encrypted = '';
$text = 'abc000123';
$key = 'a';
echo do_encrypt($text, $key);
echo '<br />', base64_encode($text);
?>
the output is
YYYWYQYWJYQ=WYWJYQWJjYQ=JYJjYQJjMYQ=jYjMYQjMDYQ=MYMDYQMDAYQ=DYDAYQDAwYQ=AYAwYQAwMYQ=wYwMYQwMTYQ=MYMTYQMTIYQ=
YWJjMDAwMTIz
since no transformation on the base64-string takes place there must be a z in the cipher's output but isn't, this information is lost.
adamk
Forum Newbie
Posts: 4
Joined: Tue Sep 30, 2003 10:59 pm
Location: Granbury, TX
Contact:

Post by adamk »

thanks :). i noticed that something wasn't happening with the output, but i wasn't 100% sure of what it was! thanks again!
Post Reply