Php encode string and vice versa

I have several objects (objects), each of which has an identifier (unique) and a name.
When I want to display one of them, I have a URL, for example, www.domain.com/view/key:xxx.
A key is just the identifier of an object encoded with base64_encode, so it is not directly from the URL, what is its identifier.

What I'm trying to do now (due to project specifications) is that the key contains only numbers and letters (base64_encode provides the result, for example eyJpZCI6IjM2In0= or eyJpZCI6IjM2In0%3D after url encoding).

Is there a simple alternative to this? This is not a problem with a high degree of protection - there are many ways to identify an identifier - I just have to have a key that contains only letters and numbers that are generated by the identifier of the object (possibly in combination with its name), which can be decoded to return the identifier to me .

All of the various encoding methods I have found may contain special characters.
Any help here? thanks in advance

+4
source share
2 answers

This answer does not actually use encryption, but since your question has also been labeled with encoding ...

With PHP 5, you can use bin2hex :

 $s = base64_decode('eyJpZCI6IjM2In0='); echo bin2hex($s); 

Conclusion:

 7b226964223a223336227d 

To decode:

 $s = hex2bin($data); 

Or:

 $s = pack('H*', $data); 

Btw, if the id parameter is sensitive, you might consider checking its protection as an alternative to full encryption.


Forgot to mention how you can make base64 secure url:

 function base64_url_encode($input) { return strtr(base64_encode($input), '+/=', '-_,'); } function base64_url_decode($input) { return base64_decode(strtr($input, '-_,', '+/=')); } 
+6
source

There are many PHP encoding / decoding functions. Here you can find here and here .

Or just get rid of = at the end of base64_encode and add it to the PHP code for base64_decode to find the identifier.

0
source

All Articles