0

I'm trying to find a way to encrypt via php and decrypt via javascript/ajax

This isn't meant to be a robust solution but just a lightweight remedy to discourage the casual user from right clicking and looking at the source to see the correct answer in plain view. The answer will be revealed via ajax.

I was trying to translate the encoding of the string function I found below to php but it's not right.

http://www.yaldex.com/FSPassProtect/CharacterEncoder.htm

The code I have so far is this...

$string = "Ordinarily, license fees are in the nature of the exercise of police";

for($i=0;$i<strlen($string);$i+=1){
    $output = $output . (ord($string[$i]) - 23);
}

echo $output;

but when I past the generated code into the webpage listed above to decrypt it it's not correct.

Any help would be appreciated.

Mahmoud Gamal
  • 72,639
  • 16
  • 129
  • 156
SteveF
  • 281
  • 3
  • 11
  • At a glance, you might try converting the integer value of your deciphered character back into a character literal (`chr(ord($string[$i]) - 23)`)? – rjz Apr 21 '12 at 06:26

3 Answers3

6

Please refrain from making your code hard to read on purpose. If you don't want the user to know the answer, don't give the answer up until you need to check for it.

The answer should be on the server side, away from the user, and once the user clicks an answer and submits, then and only then should you fetch the answer and check against it if the user was right.

Madara's Ghost
  • 158,961
  • 49
  • 244
  • 292
1

note that i prefer @Truth's answer but if you really don't want to do it like that read on ;)

on PHP side you could use base64_encode() and on javascript side use a custom implemenation like this to decode it

Community
  • 1
  • 1
Andreas Linden
  • 11,975
  • 7
  • 46
  • 65
0

why you pick the answer along the question? If you want to encrypt your code use base_encode()

<?php
$str = 'This is an encoded string';
echo base64_encode($str);
?>

For decryption use below one

<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';//encoded string
echo base64_decode($str);
?>
arun
  • 3,637
  • 3
  • 27
  • 53