Tag Archives: openssl_decrypt

Two Way Encryption or Hashing using Key

This is using PHP and openssl_decrypt/openssl_encrypt

Encrypting the string

$key = "xxxxxxxxxxx"; //11 characters
$ivlen = openssl_cipher_iv_length("aes-256-cbc-hmac-sha256");
$iv = openssl_random_pseudo_bytes($ivlen);
$hash = openssl_encrypt(STRING TO HASH,"aes-256-cbc-hmac-sha256",$key,0,$iv);
$iv = bin2hex($iv); // iv generated is in binary - converted to HEX for passing through SESSION or POST or URL

Decrypting back the string

$key = "xxxxxxxxxxx";
$hash = HASH FROM ENCRYPTION;
$iv = IV FROM ENCRYPTION STEP; //note this is in HEX and needs to be converted back to BIN 
$iv = hex2bin($iv); //convert the IV in HEX to BIN

$decryptedString = openssl_decrypt($hash,"aes-256-cbc-hmac-sha256",$key,0,$iv);

Feel free to explore other algorithms