I am trying to generate a token using JCryptCipherCrypto and I wanted to generate a string which will contain a-z, A-Z, 0-9 and length must be of 16 characters.

I have done this:

$crypto = new JCryptCipherCrypto;
$key = $crypto->generateKey();

Its method is not working or maybe I am doing something wrong :(

Can anyone tell me a better way to generate a token string or explain to me what I am missing?

  • Your server is probably missing mcrypt extension. Install / allow it and everything should work. Check here for more information.
    – Rene Korss
    Jun 20 '16 at 7:11
  • I have mycrypt extension enabled but still it is giving me result like this - VVz�E�$%����]6
    – Joomler
    Jun 20 '16 at 10:29

This result is exactly what you want. Now you have a key to encrypt data. And later you can decrypt it with the same key.

// Test string
$str = 'Lorem ipsum...';

// Get key
$crypto = new JCryptCipherCrypto;
$key = $crypto->generateKey();

// Crypt it. Nobody can read it without key
$crypted = $crypto->encrypt($str, $key);

// Decrypt crypted string
$decrypted = $crypto->decrypt($crypted, $key);
echo $decrypted;


Lorem ipsum...

For generating tokens I have used this method

function generateToken($length)
    $token = JUserHelper::genRandomPassword($length);

    return $token;

Main advantage of this method is that I can generate random string up to 32 char.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.