Ad Code

Data encryption in PHP using base64 with key

I am using base64 for encryption and decryption.

encrypt.php

<?php
$id= $_GET['id'];
$encrypted = encrypt($id, "check");
echo $encrypted ;


function encrypt($string, $key){
   $result = '';
   for($i=0; $i<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);
}
?>


decrypt.php

<?php
$id= $_GET['id'];
$decrypted = decrypt($id, "check");
echo $decrypted ;


function decrypt($string, $key) {
   $result = '';
   $string = base64_decode($string);
   for($i=0; $i<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;
}
?>

 

Post a Comment

0 Comments