<?php
$key = "132465";
$iv = '$KJh#(}q';
function Encrypt($text)
{
return $crypttext = mcrypt_encrypt(MCRYPT_DES, $key, $text, MCRYPT_MODE_ECB,$iv);
}
function Decrypt($cryptedtext)
{
return $decrypttext = mcrypt_decrypt(MCRYPT_DES,$key,$cryptedtext,MCRYPT_MODE_ECB,$iv);
}
?>
<?php
class Crypt
{
static function Encrypt($cipher, $key, $data, $mode, $iv)
{
return mcrypt_encrypt($cipher, $key, $data, $mode, $iv);
}
static function Encrypt_Hex($cipher, $key, $data, $mode, $iv)
{
return bin2hex(Crypt::Encrypt($cipher, $key, $data, $mode, $iv));
}
static function Decrypt($cipher, $key, $data, $mode, $iv)
{
return mcrypt_decrypt($cipher, $key, $data, $mode, $iv);
}
static function Decrypt_Hex($cipher, $key, $hex, $mode, $iv)
{
$data='';
for ($i=0; $i < strlen($hex)-1; $i+=2){
$data .= chr(hexdec($hex[$i].$hex[$i+1]));
}
return Crypt::decrypt($cipher, $key, $data, $mode, $iv);
}
}
$key = "123456";
$iv = '$KJh#(}q';
$data = "data to encrypt"; // The text to encrypt
$cipher = MCRYPT_DES; // The encryption algorighm
$mode = MCRYPT_MODE_CBC; // CBC mode is for large amounts of data
$encrypted = Crypt::Encrypt_Hex($cipher, $key, $data, $mode, $iv); // will in this case return c073fa74b16201687cce2ce96a195ebc
$plain = Crypt::Decrypt_Hex($cipher, $key, $encrypted, $mode, $iv); // should return the $data
?>
For more info:
http://www.php.net/manual/en/book.mcrypt.php
Open in new window