Srikanth Koneru
Srikanth Koneru

Reputation: 264

How to generate cryptographic key in the form of a binary string 16 bytes long in PHP

I am trying to store a string in encrypted form in mysql DB, so looking for two way encryption, I found a guide about openssl_encrypt / openssl_decrypt

$ciphertext = openssl_encrypt($plaintext,  'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
$plaintext  = openssl_decrypt($ciphertext, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);

That guide doesn't explain how to generate cryptographic key in the form of a binary string 16 bytes long for $key and crypto-secure random binary string 16 bytes long for $iv.

Any help would be appreciated.

Upvotes: 0

Views: 1311

Answers (1)

Matt
Matt

Reputation: 2869

$key = openssl_random_pseudo_bytes(16); and $iv = openssl_random_pseudo_bytes(16);

Should generate you a random 16 byte / 128-bit key and iv.

Upvotes: 1

Related Questions