0

I am using base64 for encryption and decryption.

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.

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)

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;
}
?>
10
  • 4
    No you're not using base64 for encryption; you're using base64 for encoding.... you're using a weird 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; } for "encrypting" Commented Sep 25, 2014 at 10:05
  • 1
    I am new in PHP.. can you please elaborate this?? @MarkBaker if I am not wrong you mean that I should use mk5. Commented Sep 25, 2014 at 10:07
  • mk5?!? Do you mean md5? No, md5 is a hashing algorithm. It's one-way (you can't get the cow back from the beefburger), and flawed Commented Sep 25, 2014 at 10:08
  • Well you could always use PHP's built-in encryption functions rathe rthan a bad homebrew.... but this works correctly as long as your input id to decrypt is correct Commented Sep 25, 2014 at 10:14
  • @MarkBaker, now I am shifted to md5 (thanks for your suggestion). Here is my code $key="check"; $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $variableValue, MCRYPT_MODE_CBC, md5(md5($key)))); decryption code is as follow $key="check"; $variableValue ="skype" $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($variableValue), MCRYPT_MODE_CBC, md5(md5($key))), "\0"); echo $decrypted ; Now input skype (encryption value =%05¢%20_¤) output after decryption (X¡xEîl0^~=‹&@y—ß^¥„Òê£}΋;JåHà $%$%$) Commented Sep 25, 2014 at 10:36

1 Answer 1

4

Basically...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.