Encrypt/Decrypt using mcrypt

爷,独闯天下 提交于 2019-12-06 13:50:50
  1. Don't urlencode. Unnecessary.
  2. trim for NULL bytes, not empty strings: rtrim($str, chr(0)); (Instead, you might want to save the source string length in the encrypted result too, so you won't rtrim() too much.)

Why pack('H*', $account) for $key? Also unnecessary.

Rijndael 128 uses 16 byte keys (128 bits), so make sure your key is at least that long:

$key = $account . $account

will do, but it obviously imperfect. (mcrypt will do something like that if it's too short.) If every account had its own passphrase, that would be good. (Even more so in combination with an app secret, but details.)

rtrim() with chr(0) is fine, very probably, because your source string won't have trailing NUL bytes.

I usually use these en/decrypt functions, or alike, but these have a static secret/key, so yours is better.

To send an encrypted token to the client:

$enc_token = Crypt::encrypt($token, $key);
// $enc_token might contain `/` and `+` and `=`
$url = 'page.php?token=' . urlencode($enc_token);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!