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>");
?>
Related Articles
- Add an Automatic / Dynamic Copyrighted Year Message in PHP
- Php Mail Function with Html Header
- How to check if an email address exists without sending an email?
- Pass Array from Multipart/Form-data Post Method or Query String with Foreach Function in PHP
- Use in_array function to check if a value exists in an array using PHP
- Loop through an Array using Foreach Function in PHP
- Delete File or an Image from the Server using PHP
- Encrypt Decrypt using the Rijndael 256 Mcrypt Function in PHP
