Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* ========================================
- Class for encrypting and decrypting data
- using the OpenSSL and MCRYPT libraries
- Requires PHP 5.4+ and the OpenSSL and
- MCRYPT libraries be installed.
- @author: rbrotherton@gmail.com
- @license: Unlicensed
- ======================================== */
- class OpenSSL {
- protected $cipher_mcrypt = MCRYPT_RIJNDAEL_128;
- protected $cipher_openssl = 'aes-256-cbc';
- // Constructor
- public function __construct(){
- }
- // How to behave when treated like a string
- public function __toString(){
- return "OpenSSL/MCRYPT encryption object.";
- }
- public function encrypt($data, $key){
- $vector_size = mcrypt_get_iv_size($this->cipher_mcrypt, MCRYPT_MODE_CBC);
- $vector = mcrypt_create_iv($vector_size, MCRYPT_DEV_URANDOM);
- $encrypted = openssl_encrypt($data, $this->cipher_openssl, $key, 0, $vector);
- // Pass the initialization vector along with our data string so we can decode later
- return $vector . $encrypted;
- }
- public function decrypt($data, $key){
- $vector_size = mcrypt_get_iv_size($this->cipher_mcrypt, MCRYPT_MODE_CBC);
- $vector = substr($data, 0, $vector_size);
- $data = substr($data, $vector_size);
- return openssl_decrypt($data,$this->cipher_openssl, $key, 0, $vector);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement