2011-06-27
- class CryptM {
-
private $input = null;
-
private $output = null;
-
private $key = null;
-
//initated cypher with constant - strongest encryption type
-
private $cypher = MCRYPT_RIJNDAEL_256;
-
//initiated mode with constant
-
private $mode = MCRYPT_MODE_CFB;
-
private $cypherObject = null;
-
private $iv = null;
-
-
public function CryptM(){
-
if(!$this->checkCypher($this->cypher)){
-
throw new Exception('cypher not supported('.$this->cypher.')');
-
}
-
-
if(!$this->checkMode($this->mode)){
-
throw new Exception('mode not supported('.$this->mode.')');
-
}
-
-
/*Usage notes
-
//CryptM encoding class for PHP
-
//COPYRIGHT icurtain.co.uk - mike lang, 2011
-
main class usage is intended as follows
-
$c = new CryptM();
-
$result = $c->quickEncrypt('secret things','password');
-
$result = $c->quickDecrypt($result,'password');
-
-
//most functionality is documented and explained - returning null either means it was empty to start with or the wrong password was entered
-
//this is implemented by a base64 check - the encoding methodology is to attach the vector to the start of the cypher - which someone on some
-
//form said was bad practice, however, no one said why as it can be made public without significantly weakening the encryption
-
//any further queries read the documentation on MCRYPT
-
-
Object can either be created and used OO style
-
$c = new CryptM();
-
$c->setInput('blah');
-
$c->setKey('key');
-
$c->setMode('cfb');
-
$c->setCypher('des');
-
$c->encrypt();
-
$c->getOutput(); //same with decrypt
-
*/
-
-
}
-
-
private function checkCypher($cypher){
-
foreach(mcrypt_list_algorithms() as $m){
-
if($cypher == $m){
-
return(true);
-
}
-
}
-
return($false);
-
}
-
-
private function checkMode($mode){
-
foreach(mcrypt_list_modes() as $m){
-
if($mode == $m){
-
return(true);
-
}
-
}
-
return($false);
-
}
-
-
public function getInput(){
-
return $this->input;
-
}
-
-
public function setInput($input){
-
$this->input = $input;
-
}
-
-
public function getOutput(){
-
return $this->output;
-
}
-
-
public function setOutput($output){
-
$this->output = $output;
-
}
-
-
public function getKey(){
-
return $this->key;
-
}
-
-
public function setKey($key){
-
$this->key = $key;
-
}
-
-
-
public function getCypher(){
-
return $this->cypher;
-
}
-
-
public function setCypher($cypher){
-
//This is probably very naughty and quite bad practice but what the frick, we create a constant containing the correct value and copy it in the
-
//private class variable
-
if($this->checkCypher($cypher)){
-
define(CYPHER, $cypher);
-
$this->cypher = CYPHER;
-
return(true);
-
}else{
-
return(false);
-
}
-
-
}
-
-
public function getMode(){
-
return $this->mode;
-
}
-
-
public function setMode($mode){
-
if($this->checkMode($mode)){
-
define(MODE, $mode);
-
$this->mode = MODE;
-
return(true);
-
}else{
-
return(false);
-
}
-
}
-
-
public function listInfo(){
-
echo '<br />ALGORITHMS:<br />';
-
foreach(mcrypt_list_algorithms() as $m){
-
echo ''.$m.'<br />';
-
}
-
echo '<br />MODES:<br />';
-
foreach(mcrypt_list_modes() as $m){
-
echo ''.$m.'<br />';
-
}
-
-
}
-
-
-
private function testCypherObject($cypherObject){
-
echo 'CYPHER OBJECT<br />';
-
echo mcrypt_enc_get_algorithms_name($cypherObject).'<br />';
-
echo mcrypt_enc_get_block_size($cypherObject).'<br />';
-
foreach(mcrypt_enc_get_supported_key_sizes($cypherObject) as $k){
-
echo $k.'<br />';
-
}
-
}
-
-
public function encrypt(){
-
if(sizeof($this->key)<1){
-
return (false);
-
}
-
//$x = mcrypt_module_open($this->getCypher(), '', $this->getMode(), '');
-
//$x = $this->getCypherObject();
-
//create encryption vector and vector size
-
mcrypt_generic_init($this->getCypherObject(), $this->getKey(), $this->getInitiationVector());
-
$this->setOutput(mcrypt_generic($this->getCypherObject(), $this->getInput()));
-
$this->resetCypherObject();
-
}
-
-
public function decrypt(){
-
$this->setOutput(mcrypt_decrypt($this->getCypher(), $this->getKey(), $this->getInput(), $this->getMode(), $this->getInitiationVector()));
-
}
-
-
private function getCypherObject(){
-
//2nd and 4th paramaters are the locations of the cyphers and modes - should be defined in php.ini
-
if($this->cypherObject==null){
-
$this->cypherObject = mcrypt_module_open($this->getCypher(), '', $this->getMode(), '');
-
}
-
//$this->testCypherObject($this->cypherObject);
-
return($this->cypherObject);
-
}
-
-
private function resetCypherObject(){
-
mcrypt_generic_deinit($this->cypherObject);
-
mcrypt_module_close($this->cypherObject);
-
$this->cypherObject = null;
-
}
-
-
private function getInitiationVector(){
-
if($this->iv == null){
-
$size = mcrypt_get_iv_size($this->getCypher(), $this->getMode());
-
$this->iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
-
}
-
return($this->iv);
-
}
-
-
private function resetIV(){
-
$this->iv = null;
-
}
-
-
private function quickReset(){
-
//as the quick en/decrypt functions are quite prescriptive in how they process data they only work
-
//with this cypher/mode combo so they
-
$this->cypher = MCRYPT_RIJNDAEL_256;
-
$this->mode = MCRYPT_MODE_CFB;
-
}
-
-
public function quickEncrypt($input,$key){
-
//this function assumes a lot - and tags the IV onto the start of the outputted data - ergo its not standard and wont work unless u string
-
//split it and process it accordingly - we also base 64 encode/decode so that we can do a half-baked test on whether we have returned valid data
-
//technically this makes the cypher weaker to a dictionary attack but this is PHP.. its only ever going to be half baked encryption
-
$this->quickReset();
-
-
$iv = $this->getInitiationVector();
-
mcrypt_generic_init($this->getCypherObject(), $key, $iv);
-
//concatinate IV with encrypted base64_cypher of the input
-
$e = $iv.mcrypt_generic($this->getCypherObject(), base64_encode($input));
-
$this->resetCypherObject();
-
return($e);
-
}
-
-
public function quickDecrypt($input,$key){
-
//$iv = substr($input,strlen($input)-32,32);
-
//$e = substr($input,0,strlen($input)-32);
-
$this->quickReset();
-
$iv = substr($input,0,32);
-
$e = substr($input,32,strlen($input));
-
//base64 is strict and will return null if the char string is not base64 encoded - null = wrong password
-
if($result = base64_decode(mcrypt_decrypt($this->getCypher(), $key, $e, $this->getMode(), $iv), true)){
-
return($result);
-
}return(null);
-
}
-
-
}