2011-11-28 18 views
11

Oggi ho ricevuto un errore mentre stavo sviluppando un'applicazione basata su PHP, MySql e Zend Framework. Inoltre, sto usando phpseclib per crittografare i dati utilizzando lo AES algorithm e qui è venuto il problema. L'output dell'algoritmo AES è in una forma che a MySql non piace. Infatti, quando provo a inserire i dati nel database, ho ottenuto un'eccezione Sql. L'errore è:Mysql: errore generale: 1366 Valore stringa errato

SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xE4\xD5\xABtZM...' for column 'Name' 

ho già letto tutte le risposte pubblicate su StackOverflow e hanno anche Googled il problema, ma tutti la soluzione proposta erano già nel mio codice. Database, tabelle e tutti i colle hanno Collation utf8_general_ci. Qui sotto potete vedere il relativo codice:

  1. Application.ini per vedere come è impostato il collegamento
  2. database.php per vedere come posso recuperare la connessione al database
  3. model.php per vedere come provo per inserire i dati nel database
  4. cifrare() per vedere come io uso la classe AES per crittografare i dati
  5. definizione Table (Se sanno che tutti sono in utf8 non è sufficiente)

Application.ini

resources.db.adapter = "Pdo_Mysql" 
resources.db.params.charset = "utf8" 
resources.db.params.host = "localhost" 
resources.db.params.username = "********" 
resources.db.params.password = "********" 
resources.db.params.dbname = "dbname" 

database.php

public static function getDb() 
{ 
    if (self::$Db === NULL) 
     self::$Db = Zend_Db_Table::getDefaultAdapter(); 
    return self::$Db; 
} 

model.php

$Values = array(
    'Id' => $this->Id, 
    'Name' => $this->Name, 
    'CreationDate' => $this->CreationDate, 
); 
$RowChanged = $Db->insert('TABLENAME', $Values); 

cifrare()

public static function encrypt($Data, $EncryptionKey) 
{ 
    $AES = new Crypt_AES(); 
    $AES->setKey($EncryptionKey); 
    return $AES->encrypt($Data); 
} 

tavolo

CREATE TABLE IF NOT EXISTS `table` (
    `Id` mediumint(8) unsigned NOT NULL, 
    `Name` varchar(200) DEFAULT NULL, 
    `CreationDate` date NOT NULL, 
    PRIMARY KEY (`Id`), 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

Domanda: Come posso risolvere il problema e memorizzare i dati nel database?

+0

domanda relativa http://stackoverflow.com/questions/7461962/mysql-how-to-store-aes-encrypted-data – hafichuk

risposta

9

mi rendo conto che questo è un reference per AES_ENCRYPT per MySQL, ma sembra che potrebbe essere necessario cambiare la vostra varchar(200) a un varbinary(200) (o più) come AES sembra restituire una stringa binaria.

C'è un explanation meno chiaro sul sito MySQL.

Many encryption and compression functions return strings for which the result might contain arbitrary byte values. If you want to store these results, use a column with a VARBINARY or BLOB binary string data type. This will avoid potential problems with trailing space removal or character set conversion that would change data values, such as may occur if you use a nonbinary string data type (CHAR, VARCHAR, TEXT).

+0

1 - È sicuramente mi ha messo sulla strada giusta. Successivamente proverò il processo di crittografia/decrittografia. Se andrà bene, controllerò la tua come risposta giusta. Al momento ... molti grazie! –

+0

np, grazie alla pubblicazione di questo - ho imparato qualcosa di nuovo da solo. – hafichuk

+0

Funziona benissimo. Molte grazie ancora !!! –

Problemi correlati