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

  • SEARCH
  • Home
  • 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 274643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:38:29+00:00 2026-05-12T00:38:29+00:00

I have a code that goes something like: $cipher_alg = MCRYPT_RIJNDAEL_128; $decrypted_string = mcrypt_decrypt($cipher_alg,

  • 0

I have a code that goes something like:

$cipher_alg = MCRYPT_RIJNDAEL_128;
$decrypted_string = mcrypt_decrypt($cipher_alg, $key, 
$encrypted_string , MCRYPT_MODE_CBC, trim(hex2bin(trim($hexiv))));

I worry that in the process of decoding the mcrypt_decrypt will introduce a gratuitous whitespace or null characters at the back or front of the $decrypted_string.

So should I trim it?

Note: I could have run the code and find this out. But since I can never run enough samples to prove ( or disprove) my point, I want some concrete and theoretical answers, probably based on the inner working of the mcrypt_decrypt algorithm. Another reason I ask is that I believe this is going to help others.

Note 2: Notwithstanding with the answer below ( now deleted and only 10K users can see it), it seems that the examples here do use trimming to get the correct decrypted string.

  • 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-05-12T00:38:30+00:00Added an answer on May 12, 2026 at 12:38 am

    Actually both mcrypt_encrypt() and mcrypt_decrypt() as well as the other en-/decryption functons (like mcrypt_generic() or mdecrypt_generic()) do pad the $data parameter to a length of n * <<blocksize>>. The padding character is the NUL character (\x0 or \0) whereas the <<blocksize>> depends on the cipher and the block cipher modes used. You should have a look at Block cipher modes of operation and Padding (cryptography).

    The following is the output of mcrypt_get_block_size() for each of the available ciphers and modes on my machine. Obviously the function does not take into account that modes such as CFB, OFB and CTR do not require any special measures to handle messages whose lengths are not multiples of the block size, since they all work by XORing the plaintext with the output of the block cipher (quote from Wikipedia). CBC which is used in your example always requires that the final block is padded before encryption.

    cast-128
        cbc: 8 bytes
        cfb: 8 bytes
        ctr: 8 bytes
        ecb: 8 bytes
        ncfb: 8 bytes
        nofb: 8 bytes
        ofb: 8 bytes
        stream: not supported
    gost
        cbc: 8 bytes
        cfb: 8 bytes
        ctr: 8 bytes
        ecb: 8 bytes
        ncfb: 8 bytes
        nofb: 8 bytes
        ofb: 8 bytes
        stream: not supported
    rijndael-128
        cbc: 16 bytes
        cfb: 16 bytes
        ctr: 16 bytes
        ecb: 16 bytes
        ncfb: 16 bytes
        nofb: 16 bytes
        ofb: 16 bytes
        stream: not supported
    twofish
        cbc: 16 bytes
        cfb: 16 bytes
        ctr: 16 bytes
        ecb: 16 bytes
        ncfb: 16 bytes
        nofb: 16 bytes
        ofb: 16 bytes
        stream: not supported
    arcfour
        cbc: not supported
        cfb: not supported
        ctr: not supported
        ecb: not supported
        ncfb: not supported
        nofb: not supported
        ofb: not supported
        stream: 1 bytes
    cast-256
        cbc: 16 bytes
        cfb: 16 bytes
        ctr: 16 bytes
        ecb: 16 bytes
        ncfb: 16 bytes
        nofb: 16 bytes
        ofb: 16 bytes
        stream: not supported
    loki97
        cbc: 16 bytes
        cfb: 16 bytes
        ctr: 16 bytes
        ecb: 16 bytes
        ncfb: 16 bytes
        nofb: 16 bytes
        ofb: 16 bytes
        stream: not supported
    rijndael-192
        cbc: 24 bytes
        cfb: 24 bytes
        ctr: 24 bytes
        ecb: 24 bytes
        ncfb: 24 bytes
        nofb: 24 bytes
        ofb: 24 bytes
        stream: not supported
    saferplus
        cbc: 16 bytes
        cfb: 16 bytes
        ctr: 16 bytes
        ecb: 16 bytes
        ncfb: 16 bytes
        nofb: 16 bytes
        ofb: 16 bytes
        stream: not supported
    wake
        cbc: not supported
        cfb: not supported
        ctr: not supported
        ecb: not supported
        ncfb: not supported
        nofb: not supported
        ofb: not supported
        stream: 1 bytes
    blowfish-compat
        cbc: 8 bytes
        cfb: 8 bytes
        ctr: 8 bytes
        ecb: 8 bytes
        ncfb: 8 bytes
        nofb: 8 bytes
        ofb: 8 bytes
        stream: not supported
    des
        cbc: 8 bytes
        cfb: 8 bytes
        ctr: 8 bytes
        ecb: 8 bytes
        ncfb: 8 bytes
        nofb: 8 bytes
        ofb: 8 bytes
        stream: not supported
    rijndael-256
        cbc: 32 bytes
        cfb: 32 bytes
        ctr: 32 bytes
        ecb: 32 bytes
        ncfb: 32 bytes
        nofb: 32 bytes
        ofb: 32 bytes
        stream: not supported
    serpent
        cbc: 16 bytes
        cfb: 16 bytes
        ctr: 16 bytes
        ecb: 16 bytes
        ncfb: 16 bytes
        nofb: 16 bytes
        ofb: 16 bytes
        stream: not supported
    xtea
        cbc: 8 bytes
        cfb: 8 bytes
        ctr: 8 bytes
        ecb: 8 bytes
        ncfb: 8 bytes
        nofb: 8 bytes
        ofb: 8 bytes
        stream: not supported
    blowfish
        cbc: 8 bytes
        cfb: 8 bytes
        ctr: 8 bytes
        ecb: 8 bytes
        ncfb: 8 bytes
        nofb: 8 bytes
        ofb: 8 bytes
        stream: not supported
    enigma
        cbc: not supported
        cfb: not supported
        ctr: not supported
        ecb: not supported
        ncfb: not supported
        nofb: not supported
        ofb: not supported
        stream: 1 bytes
    rc2
        cbc: 8 bytes
        cfb: 8 bytes
        ctr: 8 bytes
        ecb: 8 bytes
        ncfb: 8 bytes
        nofb: 8 bytes
        ofb: 8 bytes
        stream: not supported
    tripledes
        cbc: 8 bytes
        cfb: 8 bytes
        ctr: 8 bytes
        ecb: 8 bytes
        ncfb: 8 bytes
        nofb: 8 bytes
        ofb: 8 bytes
        stream: not supported
    

    Therefore you have to rtrim() the output of the decryption functions to get the original string if your cipher operates on fixed length blocks:

    $output = rtrim($decrypted, "\0");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 169k
  • Answers 169k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Hmm... Could you possibly put the code in a Helper? May 12, 2026 at 1:57 pm
  • Editorial Team
    Editorial Team added an answer Divide by 10, the modulo is your next digit, repeat… May 12, 2026 at 1:57 pm
  • Editorial Team
    Editorial Team added an answer You're not supposed to copy libtool into your program's directory… May 12, 2026 at 1:57 pm

Related Questions

I have some code that I'm trying to rewrite. The code is designed to
I have some code that is used to programmatically create a Document to send
I have a dataset obtained from MySQL that goes like this: Array ( [0]
I have a function resizePreview() that will resize an image in a jQuery dialog

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.