Speed up development with full-stack environments for every branch.

Learn More

PHP mcrypt - Class to provide 2 way encryption of data

Forked from PHP mcrypt - Basic encryption and decryption of a string.

750 Runs 4573 Views 2969 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * PHP mcrypt - Class to provide 2 way encryption of data
 */

class Crypt {

    private $secretkey = 'this is a string used as a key';

    //Encrypts a string
    public function encrypt($text) {
        $data = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->secretkey, $text, MCRYPT_MODE_ECB, 'keee');
        return base64_encode($data);
    }

    //Decrypts a string
    public function decrypt($text) {
        $text = base64_decode($text);
        return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->secretkey, $text, MCRYPT_MODE_ECB, 'keee');
    }

}

// a new proCrypt instance
$crypt = new Crypt;

// encrypt the string
$encoded = $crypt->encrypt('This is the message to be encoded');

// decrypt the string
$decoded = $crypt->decrypt($encoded);

echo "Encrypted string : " . trim($encoded) . "<br />\n";
echo "Decrypted string : " . trim($decoded) . "<br />\n";
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).