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
80.3k18 gold badges143 silver badges168 bronze badges
asked Apr 21, 2012 at 6:21
1
  • At a glance, you might try converting the integer value of your deciphered character back into a character literal (chr(ord($string[$i]) - 23))? Commented Apr 21, 2012 at 6:26

3 Answers 3

5

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.

answered Apr 21, 2012 at 6:24
Sign up to request clarification or add additional context in comments.

2 Comments

That's not really practical which is why I've chosen not to do it.
well, it would be the proper soloution! but if you really don't want to do it have a look at my answer...
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

answered Apr 21, 2012 at 6:29

Comments

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);
?>
answered Apr 21, 2012 at 10:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.