openssl_encrypt and decrypt

Posted under » PHP on 11 Aug 2020

Encryption is the practice of scrambling information in a way that only someone with a corresponding key can unscramble and read it. Unlike hashing, encryption is a two-way function. When you encrypt something, you’re doing so with the intention of decrypting it later. SHA-256 is the most secure known encryption algorithm (cipher).

The basic command for openssl_encrypt

openssl_encrypt ($data, $method, $password, 0, $iv)

Encrypts given data with given method and key, returns a base64 encoded string

Data is the string to be encrypted.

Method. The cipher method. For a list of available cipher methods, use openssl_get_cipher_methods(). eg. AES-256-CBC

password is the key

Options is a bitwise disjunction of the flags OPENSSL_RAW_DATA and OPENSSL_ZERO_PADDING. Just select 0 if unsure.

iv. A non-NULL Initialization Vector. I call it salt.

If you encrypt like

$encrypt_method = "AES-256-CBC";
$key = 'admin';
$iv = 'sodium';
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);

Then when you decrypt it must be like

$encrypt_method = "AES-256-CBC";
$key = 'admin';
$iv = 'sodium';
$string = openssl_decrypt($output, $encrypt_method, $key, 0, $iv);

You can also use SHA for comparing files.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data