As the question states, I need a way to encrypt a string (i.e. 'x=27&y=3&z=123456' into 'hUIgBG5664y65H2UIB') so that it can be passed via an html image source, like so:
<img src="returnpicture.php?stuff=hUIgBG5664y65H2UIB"/>
returnpicture.php will decrypt that back into 'x=27&y=3&z=123456' and parse it into three variables which will be used to pick an image and return it.
Problem is, everywhere I look, all I can find is stuff about hashing and encrypting super-sensitive information like credit cards and passwords. This info is not sensitive at all, I just don't want users to be able to tamper with it. Therefore, it shouldn't be excessively long. Also, the encryption (I'm guessing) must be alphanumeric, so as not to mess up the url with & or =. I'm doing this in php.
It's for a sort of game. The user shouldn't be able to mess with the variables, 'cause they'll see things they shouldn't yet.
For general understanding
When you include certain key-value pairs in your request url, PHP will load these values (accordingly) into the
$_GET
superglobal.Will result in
What you seem to be trying to do is to supply multiple key-value pairs within a key-value pair:
Simply be aware that PHP will not interpret key-value pairs in
$_GET['stuff']
.Encoding/Decoding
Note: This is one possible solution out of many. Find the one that suits you most and apply it.
You can use base64_encode() to encode it, and base64_decode() to decode it.
Example:
Usage of
rawurlencode()
assures proper url safe encoding of the base64-encoded string.In your returnpicture.php you can use:
to get back your original string.
However, if you actually plan on sending a get-request like string (with variable assignments, such as
?x=12&y=13
and so on, then you need to apply further techniques to get that string parsed.See this question for details on how it can be done