Encryption and decryption in php

Question:

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

  • 544049568640 this view is needed for request
  • ZUePM2pD7zChI1 and this view is needed for the user

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

Answer:

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 was deprecated in PHP 7.1.0 and moved to PECL in PHP 7.2.0.

Scroll to Top