In PHP, you can use the built-in function random_bytes() to generate cryptographically secure random bytes, which can then be converted into a token.
On this page, you can find comprehensive information about encrypting and decrypting a string in PHP. Learn how to do it with the functions of openssl.