How to Decrypt a String in PHP?
In this post, we build-in function to decrypt large files. openssl_decrypt() can be used to encrypt strings in php.
1. Code:
<?php function decryptString($string, $key = null){ $_method = "AES-256-CBC"; $_digest = "sha512"; $_keyLength = "256"; $_iteration = "1000"; $_key = "Soltuts.com"; if (empty($key)) $key = $_key; $key = mb_convert_encoding($key, "UTF-8"); $decoded = base64_decode($string); $salt = substr($decoded, 0, 16); $iv = substr($decoded, 16, 16); $encrypted = substr($decoded, 32); $hash = openssl_pbkdf2($key, $salt, $_keyLength, $_iteration, $_digest); $decrypted = openssl_decrypt($encrypted, $_method, $hash, OPENSSL_RAW_DATA, $iv); return $decrypted; } $string = "amQi1iV7IjGKQ55/6aIzBhFY82eL6ReVxs47W+bhLAjG7rPa7U1dYe437UhJitO7TvG+CZL0oNH/N0qMHnw86T2ZAVMVEmu506xg6MzyKpLgKUHNozBbmJ0By2XDuKkHaZb0rZ06rfEmjurDzwgDtQ=="; echo decryptString($string); ?>
2. Example:
Input:
$string = "amQi1iV7IjGKQ55/6aIzBhFY82eL6ReVxs47W+bhLAjG7rPa7U1dYe437UhJitO7TvG+CZL0oNH/N0qMHnw86T2ZAVMVEmu506xg6MzyKpLgKUHNozBbmJ0By2XDuKkHaZb0rZ06rfEmjurDzwgDtQ==";
Output:
Soltuts.com is providing PHP tutorials, tips, tricks and code snippets