Search code examples
phpmcrypt

php encryption giving URL characters


Hi I have a "/" which after encoding is, %2F. And the problem is when I add this as part of the URL and open, throws 404 since there is / in between. So my question is is there any way in php where we can 1. Encrypt and get a string which never gives any special characters like this 2. have a salt passed to the encryption.

By the way following is my encryption method:

$string='16';
$key='ec5dc8c8'
$cipher_alg = MCRYPT_TRIPLEDES;
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); 
$encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv); 
return base64_encode($encrypted_string);

Note : When I use this particular set of string and key I could generate a string which gives this problem.


Solution

  • Try this:

    return urlencode(base64_encode($encrypted_string));

    UPDATE:

    Use a standard hashing function to do your encryption. It won't return any characters that can't be URL encoded.

    Try something like this:

    $string='16';
    $key='ec5dc8c8'
    $cipher_alg = MCRYPT_TRIPLEDES;
    $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); 
    $encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv); 
    $encrypted_string = hash('sha256', "$encrypted_string");
    return urlencode(base64_encode($encrypted_string));