title: PHP encryption and decryption with mySQL
body: <p>I have problems when encrypt the information in mySQL by using PHP. I am using below example and finally I can encrypt the content but when I am decrypt in other page, some strange words will shown. </p>
  
  <p>Please help</p>
  
  <p><strong>I am using below function to encrypt</strong></p>
  
  <pre><code>&lt;?php
  header("Content-Type:text/html; charset=utf-8");
  
  $key = md5('test');
  
  function encrypt($string, $key) {
       $string = rtrim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$key,$string,MCRYPT_MODE_ECB)));
       return $string;
  }
  
  //connection
  ....
  //connection
  
  mysqli_set_charset($con, 'utf8');
  
  if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
  $name = mysqli_real_escape_string($con,$_POST['name']);
  
  $sql="INSERT INTO db (name) VALUES ('".encrypt($name,$key)."')";
  
  if (!mysqli_query($con,$sql)) {
    die('Error: ' . mysqli_error($con));
  }
  ?&gt;
  </code></pre>
  
  <p><strong>And using below function to decrypt</strong></p>
  
  <pre><code>&lt;?php
  header("Content-Type:text/html; charset=utf-8");
  
  $key = md5('test');
  
  function decrypt($string, $key) {
       $string = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256,$key,base64_encode($string),MCRYPT_MODE_ECB));
       return $string;
  }
  
  //connection
  ....
  //connection
  
  mysqli_set_charset($con, 'utf8');
  
  if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
          $query = "SELECT * FROM db";
          $result = mysqli_query($con, $query);
  
          while($row = mysqli_fetch_array($result)) { 
              echo $row['name'] ;
              echo decrypt($row['name'],$key) ;
          }
  
  
  ?&gt;
  </code></pre>
  
  <p>Input: <code>TEST</code></p>
  
  <p><strong>And the result of decrypt:</strong></p>
  
  <p><code>K704rW4crOuvpDZe/yZ2ums0btlo8hvpeyJr91V+Ycg=�x�_L��kR�^О�dMFH��7*�%Ίֶ���M_&lt;��w���W�^ ��3M�"��Rk�</code></p>