Welcome to W3Courses
 Like Us on Facebook

Encrypt Decrypt using the Rijndael 256 Mcrypt Class in PHP

You can encrypt and decrypt data using the following Mcrypt class:

<?

class pWord
{
    var $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, $this->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, $this->mykey, $enpass, MCRYPT_MODE_ECB, $iv); //Decrypting...
        return rtrim($decryptedpass);
    }
}

//Calling the class

$mypass = new pword;

$mypass->text = "This is a test";
echo("<b>Text</b>: $mypass->text <br><br>");
 
$mypass->textenc = $mypass->linencrypt($mypass->text);
echo("<b>Encrypted</b>: $mypass->textenc <br><br>");

$mypass->textdec = $mypass->lindecrypt($mypass->textenc);
echo("<b>Decrypted</b>: $mypass->textdec <br><br>");

// OR  Use the following

$mypass = new pword;

$text = "This is another test";
echo("<b>Text</b>: $text<br><br>");
 
$textenc = $mypass->linencrypt($text);
echo("<b>Encrypted</b>: $textenc <br><br>");

$textdec = $mypass->lindecrypt($textenc);
echo("<b>Decrypted</b>: $textdec <br><br>");

?>
 

Share