Select Your Language

Infrastructure and Management

Cloud Computing

Storage

Runtimes

Integration and Automation

  • Comments
  • AES Encryption/Decryption not giving a expect result for specific bytes

    Posted on

    AES encryption/decryption issue for specific input lengths (23 vs 24 bytes) in PHP
    I’m using a custom AES implementation in PHP:
    PHP

    $Cipher = new AESCipher(AES::AES192);
    

    Problem
    Two similar inputs behave differently:
    CvwCvw-2-2@{}#%~[]=,-! → working (23 bytes)
    CTPView-2-2@{}#%~[]=,-
    ! → not working (24 bytes)
    Encryption works, but decryption fails for the second input.

    What I observed
    AES uses 16-byte blocks encryption not each character encryption. So, After the 16-byte blocks separation remaining bytes are filled with padding bytes for the 16-byte blocks.
    • 23 bytes → padding = 9 bytes
    • 24 bytes → padding = 8 bytes
    So only the padding differs, but results are inconsistent.
    What I tried (no success)
    • Switched AES-192 → AES-256
    • Used base64_encode / decode
    • Used rawurlencode / rawurldecode
    • Trimmed input / removed hidden characters
    • Verified INI storage and retrieval
    Question
    Why would a custom AES implementation fail for certain input sizes (e.g., 24 bytes) but work for others, even though padding should handle both cases?
    Is this due to incorrect padding or internal block processing?

    Thanks in Advance.

    by

    points

    Responses

    Red Hat LinkedIn YouTube Facebook X, formerly Twitter

    Quick Links

    Help

    Site Info

    Related Sites

    © 2026 Red Hat