Where are my glasses?
Posts: 21
138 credits Members referred : 0
« Reply #4 on: Jul 20, 2007, 03:22:07 AM »
Yes, PHP has a good support for en- and decoding mechanism. Here's a class I've written to perform en- and decryption of strings (The class does also support en-/decoding of files but I stripped because it's not needed in this case):
Code:
<?php class EnDecryption { function __construct($algorithm = 'ofb') { $this->td = mcrypt_module_open('rijndael-256', '', $algorithm, ''); }
function __destruct() { @mcrypt_generic_deinit($this->td); mcrypt_module_close($this->td); }
public function GenerateIv() { $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($this->td), MCRYPT_RAND); $iv = substr(md5(time().$iv), 0, mcrypt_enc_get_iv_size($this->td)); return $iv; }
public function EncryptString($data, $key, &$iv = '') { if (Empty($iv)) $iv = $this->GenerateIv();
Where are my glasses?
Posts: 21
138 credits Members referred : 0
« Reply #6 on: Jul 20, 2007, 03:43:44 PM »
<?php
//Attention: You need to have mcrypt enabled in the php.ini!
if (!extension_loaded('mcrypt')) { if (strtoupper(substr(PHP_OS,0,3)) == 'WIN') { if (!@dl('php_mcrypt.dll')) { die('Could not load php_mcrypt.dll'); } } else { if (!@dl('mcrypt.so')) { die('Could not load mcrypt.so'); } } }
$string = 'Hello world!'; $key = 'webdigity.com';
//First you need to create a new instance of the class. $class = new EnDecryption;
//To generate an IV, you might use GenerateIV but you can leave the following line because EncryptString() //creates an IV if the third parameter was empty $iv = $class->GenerateIv();
//To encrypt the string you need to pass EncryptString with the string to encode and your key //Notice: If you didn't generate an IV and the variable $iv is empty, EncryptString will generate an IV for you // The third parameter (IV) is being passed by reference that means the third parameter's variable // will be assigned a generated IV. $encrypted = $class->EncryptString($string, $key, $iv);
//$encrypted does now contain the encrypted string. Let's decode it. $decoded = $class->DecryptString($encrypted, $key, $iv);
//A final comparision will show if everything worked as expected if ($string == $decoded) { echo 'Everything seems to work fine.'; } else { echo 'En-/Decoding failed.'; }
?>
I didn't test this code, so there might be some errors.
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 8104
41581 credits Members referred : 3
« Reply #7 on: Jul 20, 2007, 03:51:32 PM »
Instead of this :
Code:
<?php if (!extension_loaded('mcrypt')) { if (strtoupper(substr(PHP_OS,0,3)) == 'WIN') { if (!@dl('php_mcrypt.dll')) { die('Could not load php_mcrypt.dll'); } } else { if (!@dl('mcrypt.so')) { die('Could not load mcrypt.so'); } } } ?>
you can use this : (just a less code version)
Code:
<?php if (!extension_loaded('mcrypt')) { if (!@dl(strtoupper(substr(PHP_OS,0,3)) == 'WIN' ? 'php_mcrypt.dll' : 'mcrypt.so')) die('Could not load mcrypt'); } ?>
<?php if (!extension_loaded('mcrypt')) { if (!@dl(strtoupper(substr(PHP_OS,0,3)) == 'WIN' ? 'php_mcrypt.dll' : 'mcrypt.so')) die('Could not load mcrypt'); } ?>
Good idea. I didn't use this possibility because this type of if-clauses are slower than "ordinary" if clauses. Loading differences >= 1 second(s) are noticeable if you call it about 1.000.000 times.
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 8104
41581 credits Members referred : 3
« Reply #10 on: Jul 20, 2007, 07:50:17 PM »
No, those ifs are faster than the common if () else statements. I don't remember the site I read it, but there are benchmarks that point to that.