Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to use openssl encrypt and openssl decrypt in php?


Asked by Braden Swanson on Dec 09, 2021 PHP



The php manual is currently lacking documentation for the “ openssl_encrypt ” and “ openssl_decrypt ” functions, so it took me awhile to piece together what I needed to do to get these functions working as a replacement for mcrypt, which has been unmaintained since 2003.
Just so,
The openssl_encrypt () PHP function can encrypt a data with a encryption key. On the other hand, the openssl_decrypt () function can decrypt the encrypted data using a decrypted key.
And, You should use the EVP_* functions to encrypt and decrypt. See EVP Symmetric Encryption and Decryption on the OpenSSL wiki. In fact, you should probably be using authenticated encryption because it provides both confidentiality and authenticity.
Likewise,
In PHP, Encryption and Decryption of a string is possible using one of the Cryptography Extensions called OpenSSL function for encrypt and decrypt. openssl_encrypt() Function: The openssl_encrypt() function is used to encrypt the data.
Additionally,
The -k option to openssl enc is a passphrase of any length from which an actual 256 bits encryption key will be derived. That is also the key that the PHP openssl_decrypt () function needs. This encryption key is 256 bits because you have chosen aes-256.