Skip to content
Advertisement

How to use OPENSSL_ZERO_PADDING in php?

I am trying to Use the Padding Mode: Zeros in AES-256-ECB encryption

but when i use OPENSSL_ZERO_PADDING it does not return anything.

this is the code :

JavaScript

But when i use OPENSSL_RAW_DATA instead of OPENSSL_ZERO_PADDING it returns the encrypted string

Why isnt the OPENSSL_ZERO_PADDING working ? how can i fix this ?

Advertisement

Answer

What do you know, I’m Sam too!

It looks like OPENSSL_NO_PADDING won’t work if the input data is not a multiple of the blocksize. You can fix this by padding the plaintext yourself:

JavaScript

This will get your what you’re looking for, but I think the best option for you (if you’re not absolutely required to pad the string), is to use the 5th parameter of openssl_encrypt and pass an IV like the following (note the switch back to OPENSSL_RAW_DATA):

JavaScript

There’s great summary of why you should use an iv here: What is an openssl iv, and why do I need a key and an iv?

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement