php - mcrypt_crypt and decrypt does result does not match

601

I've got these 2 methods for encryption and decryption but the input value does not match the output value of the decrypt function, can you please help me find the problem?

private function encryptMd($md)
{
    $md = serialize($md);
    //encryption should happen here
    $md = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->secret, $md, MCRYPT_MODE_ECB);
    $md = gzcompress($md);
    $md = base64_encode($md);
    return $md;
}

private function decryptMd($md)
{
    $md = base64_decode($md);
    $md = gzuncompress($md);
    // decryption happen here
    $md = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->secret, $md, MCRYPT_MODE_ECB));
    $md = unserialize($md);
    return $md;
}

here are the results:

the first string: orderid=1111264&cardnumber=4012001037141112&cardname=Test&cardtype=VISA&amount=100&expdate=0214

the encrypted string: eJwBgAB//4LFthFrKDuzX7v+90h2j24xrEnKHHxefzvh5si5yKxEJxtOAo8XG923Ako0eYP7JVH03yN9LPdb/V8JghJVi8tPvloqlEOFwDHUbwBoFGHBNvTRq0vgMRN/Wlpfk8e+UYQWV4TRYYNWNWxVLiwMFWWOk6bsESDgRBkMDQcp//OOFkg5cg==

the recieved string: eJwBgAB//4LFthFrKDuzX7v+90h2j24xrEnKHHxefzvh5si5yKxEJxtOAo8XG923Ako0eYP7JVH03yN9LPdb/V8JghJVi8tPvloqlEOFwDHUbwBoFGHBNvTRq0vgMRN/Wlpfk8e+UYQWV4TRYYNWNWxVLiwMFWWOk6bsESDgRBkMDQcp//OOFkg5cg==

the decrypted result: FALSE

391

Answer

Solution:

the problem was with $this->secret which was empty when decrypting. thanks anyway.

People are also looking for solutions to the problem: php - Including a OneToMany object in a DoctrineExtension Tree entity

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.