ciao a tutti,
di recente mi sono trovato ad usare intensivamente mcrypt in un certo punto di un applicativo dove ho necessità di criptare e decriptare le informazioni sulla carta di credito dei clienti...
il problema principale è che su frequenti richieste sembra che php non regga molto (quando alla fine i dati sarebbero pochi) e la generazione dell'output arriva a tempi decisamente non accettabili (dai 3 secondi ai 30 secondi). Allo stesso tempo, a volte l'output viene generato istantamente, considerando che a parte decriptare i dati nel database non deve fare altro...
il codice incriminato è questo
Codice PHP:
class CreditCard extends mysql
{
private $ccId;
private $ccOwner;
private $ccType;
private $ccNumber;
private $ccExpDate;
private $encrypted = 0;
private $cipher;
private $keyString;
private $iv;
public function __construct($id=0,$details = 0)
{
parent::__construct();
$this->ccId = $id;
if ($this->ccId != 0)
$this->getCcData($details);
}
public function getCcData($details)
{
$fields = ($details)?"*":"cc_type";
$data = mysql_fetch_assoc($this->db_query("SELECT ".$fields." FROM thebox_customers_cc WHERE cc_id = '".$this->ccId."'"));
if ($details)
{
$this->ccOperationsInit();
$this->ccOwner = $data["cc_owner"];
$this->ccNumber = $data["cc_number"];
$this->ccExpDate = $data["cc_exp_date"];
}
$this->ccType = $data["cc_type"];
}
public function getExpDate($how = 0)
{
if ($how == 0)
{
$date = split("-",$this->ccExpDate);
return $date[1]."/".$date[0];
}
else
return $this->ccExpDate;
}
public function getCcType()
{
return $this->ccType;
}
public function ccOperationsInit()
{
$this->keyString = "unastringamoltolunga";
$size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
$this->iv = mcrypt_create_iv($size, MCRYPT_DEV_RANDOM);
$this->cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','ecb','');
}
public function getDecryptedField($field)
{
mcrypt_generic_init($this->cipher, $this->keyString, $this->iv);
$tmp = mdecrypt_generic($this->cipher,base64_decode($this->$field));
$tmp = addcslashes($tmp, "\0..\37!@\177..\377");
$tmp = str_replace("\\000","",$tmp);
mcrypt_generic_deinit($this->cipher);
return $tmp;
}
public function getEncryptedField($field)
{
mcrypt_generic_init($this->cipher, $this->keyString, $this->iv);
$tmp = base64_encode(mcrypt_generic($this->cipher,$this->$field));
mcrypt_generic_deinit($this->cipher);
return $tmp;
}
public function saveCcData($customerId,$ccOwner,$ccType,$ccNumber,$ccDate)
{
$this->ccOwner = $ccOwner;
$this->ccType = $ccType;
$this->ccNumber = $ccNumber;
$this->ccExpDate = $ccDate . "-01";
$query_cc = "INSERT INTO
thebox_customers_cc
VALUES
('',
'".$customerId."',
'".$this->getEncryptedField("ccOwner")."',
'".$this->ccType."',
'".$this->getEncryptedField("ccNumber")."',
'".$this->ccExpDate."')";
$this->db_query($query_cc,1);
$this->ccId = mysql_insert_id();
}
# getters specifici v0.2
public function id() {
return $this->ccId;
}
public function encryptedOwner() {
return $this->ccOwner;
}
public function owner() {
mcrypt_generic_init($this->cipher, $this->keyString, $this->iv);
return mdecrypt_generic($this->cipher,base64_decode($this->ccOwner));
}
public function type() {
return $this->ccType;
}
public function encryptedNumber() {
return $this->ccNumber;
}
public function unshadowNumber() {
mcrypt_generic_init($this->cipher, $this->keyString, $this->iv);
return mdecrypt_generic($this->cipher,base64_decode($this->ccNumber));
}
public function number() {
mcrypt_generic_init($this->cipher, $this->keyString, $this->iv);
$unshadow = mdecrypt_generic($this->cipher,base64_decode($this->ccNumber));
return "**** **** **** ".substr($unshadow,-4);
}
public function expiration() {
list($y,$m) = explode('-',substr($this->ccExpDate,0,7));
return "$m/$y";
}
}
qualche suggerimento?