title: data encryption in PHP using base64 with key
body: <p>I am using base64 for encryption and decryption.</p>
  <p>But for some values, encrypted data is not decrypting properly and add special characters.
  Using current key, word 'skype' is not encrypting and decrypting correctly instead special characters appear on decrypting.</p>
  <p>Can anyone please tell me, what the problem is?
  (code is simple available on Google but I have checked blogs and forum, can't find any such thing related to this issue, which means problem is in my code)</p>
  <h2>encrypt.php</h2>
  <pre><code>&lt;?php
  $id= $_GET['id'];
  $encrypted = encrypt($id, &quot;check&quot;);
  echo $encrypted ;
  function encrypt($string, $key) 
  {
     $result = '';
     for($i=0; $i&lt;strlen($string); $i++) {
       $char = substr($string, $i, 1);
       $keychar = substr($key, ($i % strlen($key))-1, 1);
       $char = chr(ord($char)+ord($keychar));
       $result.=$char;
     }
     return base64_encode($result);
  }
  ?&gt;
  </code></pre>
  <h2>decrypt.php</h2>
  <pre><code>&lt;?php
  $id= $_GET['id'];
  $decrypted = decrypt($id, &quot;check&quot;);
  echo $decrypted ;
  function decrypt($string, $key) 
  {
     $result = '';
     $string = base64_decode($string);
     for($i=0; $i&lt;strlen($string); $i++) {
        $char = substr($string, $i, 1);
        $keychar = substr($key, ($i % strlen($key))-1, 1);
        $char = chr(ord($char)-ord($keychar));
        $result.=$char;
     }
     return $result;
  }
  ?&gt;
  </code></pre>