There is a product code: 544049568640 it possible to encrypt it, for example, something like this: ZUePM2pD7zChI1 then decrypt it back to 544049568640

  • 544049568640 this kind of need for request
  • ZUePM2pD7zChI1 and this view is necessary for the user

there is still an option that the user can search for products in this form: 544049568640 then this does not need to be encrypted, maybe someone had similar situations?

  • one
    Take some base64. Or, for example, I made a modified base85 here for similar cases ru.stackoverflow.com/questions/472363/… - Mike
  • 2
    If the encoding seems to be unreliable before it, you can use any cipher like DES, AES, etc. - Mike
  • one
    Judging by the example, do you need to encode a 12-digit number into a 14-character code? I do not see a great problem to draw my own function that will do it - displaying 12 characters on the base 10 in 14 characters on the base 62 ... on the other hand, this is all clearly stored in the database - so it is many times simpler when entering a record with a 12-digit the product code in the additional field to form a random 14-character code (checking for duplication just in case), and use it. - Akina
  • one
    Add an @newProgrammer to the string before encoding any sign and after decoding, if it does not exist, then decoding is considered to be incorrect and use the original version. or after coding, add for example the letter z to the beginning of the line by the presence of which you immediately realize that this is a coded string (unless of course there can be z in a normal product code). - Mike
  • one
    For use in the URL, all the same base64 I think is preferable, fewer characters that must be encoded in the URL. Especially if + and / is replaced. In 85 encoding, there are too many special characters, it is suitable for printing into JS text, nothing needs to be escaped, but for a URL it will look ugly - Mike

1 answer 1

Mcrypt cryptographic extensions for php.

Decryption and Encryption Functions mcrypt_decrypt ()
mcrypt_encrypt ()

The code that will encrypt / decrypt will look like this:

 <?php $string = '544049568640'; $iv = '55555555'; $passphrase = '8chrsLng'; $encryptedString = encryptString($string, $passphrase, $iv); $decryptedString = decryptString($encryptedString, $passphrase, $iv); function encryptString($unencryptedText, $passphrase, $iv) { $enc = mcrypt_encrypt(MCRYPT_BLOWFISH, $passphrase, $unencryptedText, MCRYPT_MODE_CBC, $iv); return base64_encode($enc); } function decryptString($unencryptedText, $passphrase, $iv) { $enc = base64_decode($unencryptedText); $enc = mcrypt_decrypt(MCRYPT_BLOWFISH, $passphrase, $enc, MCRYPT_MODE_CBC, $iv); return rtrim($enc, "\0"); } var_dump($encryptedString); var_dump($decryptedString); 

This extension is deprecated in PHP 7.1.0 and moved to PECL in PHP 7.2.0.