-
-
Save avshatalov48/970428cab117b910c0dfe9d7e888c1a2 to your computer and use it in GitHub Desktop.
способ зашифровать/дешифровать данные БЕЗ md5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$key = 'password to (en/de)crypt'; | |
$string = ' string to be encrypted '; // данные для шифровки/дешифровки | |
/////////////// Шифрование ////////////////// | |
$iv = mcrypt_create_iv( | |
mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), | |
MCRYPT_DEV_URANDOM | |
); | |
$encrypted = base64_encode( | |
$iv . | |
mcrypt_encrypt( | |
MCRYPT_RIJNDAEL_128, | |
hash('sha256', $key, true), | |
$string, | |
MCRYPT_MODE_CBC, | |
$iv | |
) | |
); | |
//////Дешифрование//////////////// | |
$data = base64_decode($encrypted); | |
$iv = substr($data, 0, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)); | |
$decrypted = rtrim( | |
mcrypt_decrypt( | |
MCRYPT_RIJNDAEL_128, | |
hash('sha256', $key, true), | |
substr($data, mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC)), | |
MCRYPT_MODE_CBC, | |
$iv | |
), | |
"\0" | |
); | |
//////////Результат///////////// | |
echo 'Encrypted:' . "\n"; | |
var_dump($encrypted); // "m1DSXVlAKJnLm7k3WrVd51omGL/05JJrPluBonO9W+9ohkNuw8rWdJW6NeLNc688=" | |
echo "\n"; | |
echo 'Decrypted:' . "\n"; | |
var_dump($decrypted); // " string to be encrypted " | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment