41 lines
1.3 KiB
PHP
41 lines
1.3 KiB
PHP
<?php
|
|
// File name: encrypt.inc.php
|
|
// Description: Extended PHP subroutine to encrypt/decrypt messages
|
|
// Date: 2003-08-19
|
|
// Author: imacat <imacat@pristine.com.tw>
|
|
// Copyright: Copyright (C) 2003-2007 Pristine Communications
|
|
|
|
// Settings
|
|
// This should be secret - need to be improved
|
|
define("ENCRYPT_KEY", base64_decode("MDJlMDI4MGYyZTZmN2I5ZDNlYTcyMzZiOGI5MzJlZmQ="));
|
|
|
|
// encrypt: Encrypt a message
|
|
function encrypt($s)
|
|
{
|
|
$s = strlen($s) . " " . $s;
|
|
// Pad with spaces
|
|
$s = str_pad($s, 32 * ceil(strlen($s) / 32));
|
|
// Obtain the IV
|
|
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
|
|
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
|
|
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, ENCRYPT_KEY, $s, MCRYPT_MODE_ECB, $iv));
|
|
}
|
|
|
|
// decrypt: Decrypt a message
|
|
function decrypt($s)
|
|
{
|
|
// Obtain the IV
|
|
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
|
|
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
|
|
$s = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, ENCRYPT_KEY, base64_decode($s), MCRYPT_MODE_ECB, $iv);
|
|
// Not returning a good formatted decoded text
|
|
if (!preg_match("/^(\d+) /", $s, $m)) {
|
|
return null;
|
|
}
|
|
$len = $m[1];
|
|
$s = preg_replace("/^(\d+) /", "", $s);
|
|
return substr($s, 0, $len);
|
|
}
|
|
|
|
?>
|