1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<?php /** * abCrypt utilizes openssl to encrypt and decrypt textstrings * * This project started as a way to encrypt user informaiton which is stored in the database. * Now it can also be used to use * */ /** * abCrypt is a class for encrypting and decrypting textstrings using openssl * * @param string $encryption_key The encryption in HEX */ class abCrypt { /** @var string $key Hex encoded binary key for encryption and decryption */ public $key = ""; /** @var string $encrypt_method Method to use for encryption */ public $encrypt_method = 'aes-156-cbc'; /** * Construct our object and set encryption key, if exists. * * @param string $encryption_key Users binary encryption key in HEX encoding */ function __construct () { $this->key = "put here key value"; } public function encrypt ( $string ) { $new_iv = openssl_random_pseudo_bytes ( openssl_cipher_iv_length ( $this->encrypt_method ) ); if ( $encrypted = base64_encode ( openssl_encrypt ( $string, $this->encrypt_method, $this->key, 0, $new_iv ) ) ) { return base64_encode($new_iv).':'.$encrypted; } else { return false; } } public function decrypt ( $string ) { $parts = explode(':', $string ); $iv = base64_decode($parts[0]); $encrypted = $parts[1]; if ( $decrypted = openssl_decrypt ( base64_decode ( $encrypted ), $this->encrypt_method, $this->key, 0, $iv ) ) { return $decrypted; } else { return false; } } } |
Leave a reply