PHP: Encrypt/Decrypt Short String

php

Solution

You can make `base64_encode` web safe:

function base64url_encode($plainText)
{
    return strtr(base64_encode($plainText), '+/=', '-_,');
}

function base64url_decode($b64Text)
{
    return base64_decode(strtr($b64Text, '-_,' '+/='));
}

Or use hexadecimal encoding:

bin2hex($plainText);

hex2bin($hexText);

Problem

I need to encrypt and decrypt short strings (Ex. 'product1234'). I have used mcrypt_encrypt and mcrypt_decrypt with various ciphers. The problem is that invariably it throws in extended characters into resulting string, which causes some issues with certain aspects of my application code that I cannot control. So, the question is whether there is either a cipher that reduces the list of characters that are used in the encrypted string (i.e. leaving out things such as '+', '\', or '/').

Original source