A simple class for managing Postgresql

#comment -page saved as >class.encp.php

<?php
function secureRandomKey(){

$str = ”;
$chars = str_shuffle(‘abcdefghijklmnopqrstuvwxyzZXCVBNMASDFGHJKLPOIUYTREWQ1234567890′);
$len = strlen($chars);

#$minchar = 48;
$maxchar = 32;#rand($minchar,$len);

for ($i = 0; $i < $maxchar; $i++)
{
$str .= $chars[mt_rand(0, $len-1)];
}

return $str;
}//generate randow key;

class SymmetricCrypt
{

// The initialization vector
private static $_msHexaIv = ‘c7098adc8d6128b5d4b4f7b2fe7f7f05c7098adc8d6128b5d4b4f7b2fe7f7f05′;

// Use the Rijndael Encryption Algorithm
private static $_msCipherAlgorithm = MCRYPT_RIJNDAEL_256;

/* Function encrypts plain-text string received as parameter
and returns the result in hexadecimal format */

public static function Encrypt($plainString,$_msSecretKey)
{
// Pack SymmetricCrypt::_msHexaIv into a binary string
$binary_iv = pack(‘H*’, self::$_msHexaIv);
// Encrypt $plainString
$binary_encrypted_string = mcrypt_encrypt(
self::$_msCipherAlgorithm,
$_msSecretKey,
$plainString,
MCRYPT_MODE_CBC,
$binary_iv);
// Convert $binary_encrypted_string to hexadecimal format
$hexa_encrypted_string = bin2hex($binary_encrypted_string);
return $hexa_encrypted_string;
}

/* Function decrypts hexadecimal string received as parameter
and returns the result in hexadecimal format */
public static function Decrypt($encryptedString,$_msSecretKey)
{
// Pack Symmetric::_msHexaIv into a binary string
$binary_iv = pack(‘H*’, self::$_msHexaIv);
// Convert string in hexadecimal to byte array
$binary_encrypted_string = pack(‘H*’, $encryptedString);
// Decrypt $binary_encrypted_string
$decrypted_string = mcrypt_decrypt(
self::$_msCipherAlgorithm,
$_msSecretKey,
$binary_encrypted_string,
MCRYPT_MODE_CBC,
$binary_iv);
return $decrypted_string;
}

function generateRandomKey(){

$key1 = secureRandomKey();
$key2 = secureRandomKey();

$finalKey1 = (substr($key1,0,16));
$finalKey2 = (substr($key2,15,16));

$finalKey  = $finalKey1.$finalKey2;

return $finalKey;

}//function generate random key

}

?>

Leave a Reply