/ Published in: PHP
Encryption class wrapper for Blowfish. Can be adapted to other encryption algorithms.
Expand |
Embed | Plain Text
<?php /* * This class encapsulates Blowfish encryption and decryption. */ class Encrypter{ protected $key; protected $iv; function __construct($iv=null, $key=null){ $this->iv = $iv; $this->key = $key; } /** * Encrypts data * Returns binary data. */ function encrypt($data){ $enc = mcrypt_encrypt( MCRYPT_BLOWFISH, $this->key, $data, MCRYPT_MODE_CBC, $this->iv ); return $enc; } /** * Encrypts data and convert to hex. * Ideal for storing as text. */ function encryptToHex($data){ return $this->bin2hex($this->encrypt($data)); } /** * Takes hex input, convert from hex to binary then decrypt. */ function decryptFromHex($data){ //echo "hex \"$data\"\n"; return $this->decrypt($this->hex2bin($data)); } /** * */ function decrypt($data){ $decode = mcrypt_decrypt( MCRYPT_BLOWFISH, $this->key, $data, MCRYPT_MODE_CBC, $this->iv ); //echo "decode \"$decode\"\n"; //echo sprintf("[%s]\n", $realdata); return $realdata; } /** * Convert from binary to hex */ } /** * Convert from hex to binary */ function hex2bin($s){ } /** * loads key and iv from a file. * Key is on line 1 in hex. * IV is on line 2 in hex. */ function loadKeysFromFile($filename){ } /** * String is the key and the iv separates by a newline. * Use this when storing the key in a file. * Line 1 would be the key, line 2 is the iv. */ function loadKeysFromString($string){ $key = $keys[0]; $this->key = $key; $this->iv = $iv; } function showKeys(){ } }
You need to login to post a comment.
