Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8271575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:53:06+00:00 2026-06-08T06:53:06+00:00

I came across a strange problem with encryption/decryption in PHP. Look at the code

  • 0

I came across a strange problem with encryption/decryption in PHP. Look at the code below:

<?php

function encrypt($encrypt, $mc_key, $iv) 
{
    $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv));
    $encode = base64_encode($passcrypt);
    return $encode;
}

function decrypt($decrypt, $mc_key, $iv)
{
    $decoded = base64_decode($decrypt);
    $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv));
    return $decrypted;
}

$key = 'SAtFIAI2g_r=supL43QkO#dTQYjS0JCt';
$iv = '#CvmQT1TlpINQgnc4q%9$k8mt170MXll';
$message = 'AAABzefYdjTYBAFLNMHfLHJZCtAkTKm';

$encrypted = encrypt($message, $key, $iv);
echo $encrypted."\n";

$decrypted = decrypt($encrypted, $key, $iv);
echo $decrypted."\n";

?>

I have $key, $iv which I use to encode $message. So second line should be my decrypted message. But it’s not, see the output:

MXk9zteonXhz2zPVK7o4oUJJuXXQRlwOzTO7JbqbsA==
tq֞u�4�&�
        1P&���_pŬ�.��
_

But if you change the $message a bit it works fine. Ex. if $message is: AAABzefYdjTYBAFLNMHfLHJZCtAkTKn the output is:

fAEeJAkluHht8l6ain99YMAMHJtiUAg47XsJBECdrLA=
AAABzefYdjTYBAFLNMHfLHJZCtAkTKn

Any help in finding the solution will be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:53:07+00:00Added an answer on June 8, 2026 at 6:53 am

    I am not certain this is what’s going on here, but it seems likely.

    The variable $decoded holds the binary string after you have base64_decode()-ed it, and should therefore be the exact binary string as produced by your encrypt() function before you base64_encode()-ed it .Don’t trim() the base64 decoded value you’re calling mcrypt_decrypt() on. If there is a character that gets trimmed off, you’re decrypting a different byte string than the one you originally encrypted.

    function decrypt($decrypt, $mc_key, $iv)
    {
        $decoded = base64_decode($decrypt);
        $decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, $decoded, MCRYPT_MODE_ECB, $iv));
        // Don't wrap $decoded as trim($decoded) here-----------------^^^^^^^^^^
        return $decrypted;
    }
    

    Edit

    For completeness after comments, you should not trim() when encrypting either:

    function encrypt($encrypt, $mc_key, $iv) 
    {
        $passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, $encrypt, MCRYPT_MODE_ECB, $iv));
        // -------------------------------------------Don't trim()----^^^^^^^^^^
        $encode = base64_encode($passcrypt);
        return $encode;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Came across this code: <?php require_once 'HTTP/Session/Container/DB.php'; $s = new HTTP_Session_Container_DB('mysql://user:password@localhost/db'); ini_get('session.auto_start') or session_start();
I came across a strange problem today. I created a subclass of UIView and
today I came a across a pretty strange behaviour of an php based application
While refactoring some code, I came across this strange compile error: The constructor call
I came across a strange phenomena upon running the following code: #include <iostream> class
While writing a inflection script in php I came across a strange(at least to
I came across a strange situation today where I needed a function to not
recently i came across strange problem while uploading files to my new server. I
I came across strange behavior in Visual Studio 2010 C++ compiler. Following code compiles
I came across on a strange behavior of the following code, while playing around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.