help with mcrypt_encrypt mcrypt_decrypt

hello,
I have the following function that can not be changed.
I am having problems writing the decrypt function so i get "a=1&b=2&c=3" returned from decrypt

    $myclass = new MyClass();

    $tempToken = "05710678";
    $settingsCipher = $myclass ->encrypt("a=1&b=2&c=3", $tempToken);
    echo (" =[".$settingsCipher."]\n");
    $result = $proPay->decrypt($settingsCipher,$tempToken);
    echo ("$result=[".$result."]\n");


function encrypt($text, $tempToken ){
    $encodedTempToken = utf8_encode($tempToken);
    $md5Hash = md5($encodedTempToken, TRUE);
    $encodedNameValueString = utf8_encode($text);

    $blocksize = mcrypt_get_block_size('rijndael_128', 'cbc');
    $pad = $blocksize - (strlen($encodedNameValueString) % $blocksize);
    $encodedNameValueString = $encodedNameValueString . str_repeat(chr($pad), $pad);

    $aes128 = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $md5Hash,
                                            $encodedNameValueString, MCRYPT_MODE_CBC, $md5Hash);

    return base64_encode($aes128);
}



this is what i am using now that does not work. i think i am close


function decrypt($text, $tempToken ){
    $encodedTempToken = utf8_encode($tempToken);
    $md5Hash = md5($encodedTempToken, TRUE);
   
    $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $md5Hash, $text,
                                     MCRYPT_MODE_ECB, $md5Hash);
    $blocksize = mcrypt_get_block_size('rijndael_128', 'cbc');
    $pad = $blocksize - (strlen($str) % $blocksize);
    $str = $str . str_repeat(chr($pad), $pad);
    return $str;
}


pariesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
For starters, you're using a different mode (MCRYPT_MODE_ECB) than the encryption (MCRYPT_MODE_CBC).  http://us3.php.net/manual/en/mcrypt.constants.php

http://us3.php.net/manual/en/function.mcrypt-decrypt.php

On this page http://us3.php.net/manual/en/function.mcrypt-encrypt.php are some more complete examples.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pariesAuthor Commented:
as mentioned in the description i can not modify the encrypt
0
Dave BaldwinFixer of ProblemsCommented:
Then make your 'decrypt' match it.
0
pariesAuthor Commented:
yeah sorry ,
I read it again.
Still does not work.
0
Dave BaldwinFixer of ProblemsCommented:
Save this as 'PHPmcrypt.php' and try it.  I borrowed from the PHP.net page and commented out the things I replaced.  The second set of functions (encrypt2 and decrypt2) are from http://us3.php.net/manual/en/function.mcrypt-encrypt.php.  I didn't use the class you set up because I didn't see any use for it in this code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Mcrypt</title>
</head>
<body>
<h1>PHP Mcrypt</h1>
<?php
    /* $myclass = new MyClass();

    $tempToken = "05710678";
    $settingsCipher = $myclass ->encrypt("a=1&b=2&c=3", $tempToken);
    echo (" =[".$settingsCipher."]\n");
    $result = $proPay->decrypt($settingsCipher,$tempToken);
    echo ("$result=[".$result."]\n"); */

    $tempToken = "05710678";
		echo "Temp Token: ".$tempToken."<br><br>";
    $settingsCipher = encrypt("a=1&b=2&c=3", $tempToken);
    echo ("Encrypt = [".$settingsCipher."]\n<br>");
    $result = decrypt($settingsCipher,$tempToken);
    echo ("Decrypt = [".$result."]\n<br><br>");

    $settingsCipher = encrypt2("a=1&b=2&c=3", $tempToken);
    echo ("Encrypt2 = [".$settingsCipher."]\n<br>");
    $result = decrypt2($settingsCipher,$tempToken);
    echo ("Decrypt2 = [".$result."]\n<br><br>");


function encrypt($text, $tempToken ){
    $encodedTempToken = utf8_encode($tempToken);
    $md5Hash = md5($encodedTempToken, TRUE);
    $encodedNameValueString = utf8_encode($text);

    $blocksize = mcrypt_get_block_size('rijndael_128', 'cbc');
    $pad = $blocksize - (strlen($encodedNameValueString) % $blocksize);
    $encodedNameValueString = $encodedNameValueString . str_repeat(chr($pad), $pad);

    $aes128 = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $md5Hash, $encodedNameValueString, MCRYPT_MODE_CBC, $md5Hash);

     return base64_encode($aes128);
    // return $aes128;
}

//this is what i am using now that does not work. i think i am close

function decrypt($text, $tempToken ){
    $encodedTempToken = utf8_encode($tempToken);
    $md5Hash = md5($encodedTempToken, TRUE);
    $text2 = base64_decode($text);
    $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $md5Hash, $text2, MCRYPT_MODE_CBC, $md5Hash);
    //$blocksize = mcrypt_get_block_size('rijndael_128', 'cbc');
    //$pad = $blocksize - (strlen($str) % $blocksize);
    //$str = $str . str_repeat(chr($pad), $pad);
    //return $str;
    $block = mcrypt_get_block_size('rijndael_128', 'cbc');
    $pad = ord($str[($len = strlen($str)) - 1]);
    return substr($str, 0, strlen($str) - $pad);
		
}

function encrypt2($str, $key)
{
    $block = mcrypt_get_block_size('des', 'ecb');
    $pad = $block - (strlen($str) % $block);
    $str .= str_repeat(chr($pad), $pad);

    return mcrypt_encrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);
}

function decrypt2($str, $key)
{  
    $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB);

    $block = mcrypt_get_block_size('des', 'ecb');
    $pad = ord($str[($len = strlen($str)) - 1]);
    return substr($str, 0, strlen($str) - $pad);
}

 ?>

</body>
</html>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.