How do I make openssl_encrypt pad the input to the required block size?

前端 未结 1 1424
一生所求
一生所求 2020-12-11 07:17

My code works if I manually pad my string to the length of 32.
My question is: Is there a way to make the openSSL pad the data, or do I always have to do it for it?

相关标签:
1条回答
  • 2020-12-11 07:39

    As Luke Park said, instead of explicitly telling openssl_encrypt to use OPENSSL_ZERO_PADDING, simply remove that option from the parameter and it will default to the PKCS #7 padding scheme (fills the rest of the block with 0x0n where n is the number of bytes necessary; + 16 0x00 if the block is already complete). Note: PKCS #5 as referenced by Luke and PKCS #7 are effectively identical in this scenario.

    From PHP docs:

    Without using OPENSSL_ZERO_PADDING, you will automatically get PKCS#7 padding.

    So you should be calling:

    openssl_encrypt("my baba is over the ocean", 'AES-256-CBC', $MY_SECRET_KEY, OPENSSL_RAW_DATA, $MY_IV);
    
    0 讨论(0)
提交回复
热议问题