title: AES_DECRYPT not working on linux : maybe linked to Hibernate
body: <p>I have encrypted data in my database, and I am trying to execute a request which allows me to display in phpmyadmin the values in clear.</p>
  
  <p>I use the following request : </p>
  
  <pre><code> SELECT CAST(AES_DECRYPT(`my_encrypted_colum`, UNHEX('pass_in_hexa') AS CHAR) AS clear_value 
   FROM `my_table`
  </code></pre>
  
  <p>When I use it on the dev environment (windows), it is <strong>working well</strong>. But once I use it on the pre-prod environment (linux), I get <strong>NULL</strong> for all values instead.</p>
  
  <p>I am pretty sure it has something to do with the different environments, but I cannot figure out what. I don't even know which function does not act as expected : UNHEX or AES_DECRYPT (my guess would be UNHEX)?</p>
  
  <p>Here are the config of my dev and preprod environments : </p>
  
  <p><strong>Dev</strong> : </p>
  
  <pre><code>Serveur : localhost via TCP/IP
  Type de serveur : MySQL
  Version du serveur : 5.6.15-log - MySQL Community Server (GPL)
  Version du protocole : 10
  Utilisateur : root@localhost
  Jeu de caractères du serveur : UTF-8 Unicode (utf8)
  
  Apache/2.2.25 (Win32) PHP/5.3.19
  Version du client de base de données : libmysql - mysqlnd 5.0.8-dev -     20102224 - $Id: 65fe78e70ce53d27a6cd578597722950e490b0d0 $
  Extension PHP : mysqli 
  </code></pre>
  
  <p><strong>Preprod</strong> : </p>
  
  <pre><code>Serveur: Localhost via UNIX socket
  Logiciel: MySQL
  Version du logiciel: 5.6.14 - MySQL Community Server (GPL)
  Version du protocole: 10
  Utilisateur: root@localhost
  Jeu de caractères du serveur: UTF-8 Unicode (utf8)
  
  Apache/2.2.15 (CentOS)
  Version du client de base de données: libmysql - 5.1.72
  Extension PHP: mysqli 
  </code></pre>
  
  <p><strong>EDIT</strong> : </p>
  
  <p>I have continued my researches, and it seams the methods AES_DECRYPT and UNHEX are <strong>not guilty</strong>.
  Indeed, if I directly add encrypted value in the table from phpMyAdmin as follows : </p>
  
  <pre><code> INSERT INTO `my_table` (`my_encrypted_column`) VALUES (AES_ENCRYPT('blabla', UNHEX('pass_in_hexa'))
  </code></pre>
  
  <p>Then I manage to retrieve the data <strong>correctly</strong> with the previous SELECT request.</p>
  
  <p>That means the problem must come from the way I insert the data in the first place.
  For this I use <strong>Hibernate</strong> and the nullSafeSet method.</p>
  
  <p>What is bothering me is : if there is a problem with the way I save the data, how come it is <strong>working on Windows but not on Linux</strong>?</p>
  
  <p>Below are my implementations of nullSafeSet and nullSafeGet</p>
  
  <pre><code>private static final String CIPHER_ALGORITHM = "AES";
  
  // nullSafeSet
  protected void noNullSet(PreparedStatement st, Object value, int index, SessionImplementor si) throws SQLException {
      byte[] clearText = ((String) value).getBytes(Charset.forName("UTF-8"));
  
      try {
          Cipher encryptCipher = Cipher.getInstance(CIPHER_ALGORITHM);
          encryptCipher.init(Cipher.ENCRYPT_MODE, getKey(cle));
          st.setBytes(index, encryptCipher.doFinal(clearText));
      } 
      catch (GeneralSecurityException e) {
          throw new RuntimeException("should never happen", e);
      }
  }
  
  @Override
  public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor si, Object owner) throws HibernateException, SQLException {
      byte[] bytes = rs.getBytes(names[0]);
      try {
          Cipher decryptCipher = Cipher.getInstance(CIPHER_ALGORITHM);
          decryptCipher.init(Cipher.DECRYPT_MODE, getKey(cle));
          if (bytes != null) {
              return new String(decryptCipher.doFinal(bytes), Charset.forName("UTF-8"));
          } 
          else {
              return new String();
          }
  
      } 
      catch (GeneralSecurityException e) {
          throw new RuntimeException("Mauvaise clé");
      }
  }
  
  private static SecretKeySpec getKey(String secretKey) {
      final byte[] finalKey = new byte[16];
      int i = 0;
      for (byte b : secretKey.getBytes()) {
          // XOR
          finalKey[i++ % 16] ^= b;
      }
      return new SecretKeySpec(finalKey, "AES");
  }
  </code></pre>
  
  <p>Do you have any idea what might cause the problem?</p>