Welcome to W3Courses
 Like Us on Facebook

Encrypt Decrypt using the Rijndael 256 Mcrypt Function in PHP

You can encrypt and decrypt data using the Mcrypt function below:


$mykey = "THISisMyKey";

function linencrypt($pass)
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); //get vector size on ECB mode
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); //Creating the vector
        $cryptedpass = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $mykey, $pass, MCRYPT_MODE_ECB, $iv); //Encrypting using MCRYPT_RIJNDAEL_256 algorithm
        return $cryptedpass;

function lindecrypt($enpass)
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $decryptedpass = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $mykey, $enpass, MCRYPT_MODE_ECB, $iv); //Decrypting...
        return rtrim($decryptedpass);

// Calling the functions

$Str_Test="This is a test";
echo("<b>Text</b>: $Str_Test <br><br>");

$Str_Test2 = linencrypt($Str_Test);
echo("<b>Encrypted</b>: $Str_Test2 <br><br>");

$Str_Test3 = lindecrypt($Str_Test2);
echo("<b>Decrypted</b>: $Str_Test3 <br>");