How to create two way encode/decode methods using use-specific key - PHP?

前端 未结 1 1791
无人共我
无人共我 2020-12-29 09:52

I need two functions/methods, one to encode, one to decode. This is not for storing passwords. Each user will have a specific key/salt to encode th

相关标签:
1条回答
  • 2020-12-29 10:30
    $myVarIWantToEncodeAndDecode
    

    Define key (salt, broth etc..): $key = "#&$sdfdfs789fs7d";

    To encode:

    $encoded = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $myVarIWantToEncodeAndDecode, MCRYPT_MODE_CBC, md5(md5($key))));
    

    To decode:

    $decoded = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encoded), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
    

    Note: mcrypt_decrypt has been DEPRECATED as of PHP 7.1.0. Relying on this function is highly discouraged.

    0 讨论(0)
提交回复
热议问题