Rocky Singh
Rocky Singh

Reputation: 15440

Sending encrypted text in Url

I have a very simple (rather stupid) question, I hope someone can clear my mind on this :)

I want to send an email to my site user once he clicks a button. This email will contain a link with the userID of a user in the link URL (as query param of a link).

Once the user clicks this email link, my server side code will parse and decrypt the userID query string key to get the user ID and perform some action on it.

I cannot use base64 encoding as it can be reversed and 'hackers' can get to know the real userID. I have to encrypt the ID but when I am using AES alogrithms for encryption, the encrypted text is not "understandable" by the browser, ie I cannot pass the encrypted userId text as a part of the URL because it contains un-encoded characters like "/" which the browser cannot by pass. One option I can think of is to base64 encode the encrypted text once I send it across via URL. Then I can bease64 decode and decyrpt it.

Is this approach better than using Uri.EscapeDataString() on the encyrpted text?

Upvotes: 2

Views: 7380

Answers (4)

Chris Kuehl
Chris Kuehl

Reputation: 4157

Use a one-way hash like SHA1 or MD5, and use JavaScript to send the values as encrypted. Then, if a hacker intercepts the request, they would only have the hashes and not the actual values. They could still send the hashes to login, though; one solution is to include a JavaScript parameter (generated via your server-side language) based on IP (but not possible for a hacker to find the formula for), and use it to salt the username and password hashes.

Then on server-side you would do (in PHP, in this case):

$ipHash = sha1("random" . $_SERVER['REMOTE_ADDR'] . "salt_here10381") // place this as a hidden element in the form and use it in the JavaScript to calculate the hash
$userHash = $_POST['userHash'];
$passwordHash = $_POST['passwordHash']
// TODO: Escape $ipHash, $userHash, $passwordHash
$results = mysqli->query("SELECT * FROM `users` WHERE SHA1(CONCAT('" . $ipHash . "', `user`)) ='$userHash' AND SHA1(CONCAT('" . $ipHash . "', `password`)) = " '$passwordHash'");

Then, if a hacker wanted to login with the hash and username they found, they would need the same IP of the user originally logging in whose credentials were intercepted.

Note that this assumes you have passwords stored in your database as plain-text, which you should never do.

For hashing with SHA1, on client-side, take a look at this.

To answer your specific question (I see I got a bit off topic, oops,) it would be acceptable to base64encode the hashes when you send them to the server. If possible, try to send it as POST data and save it in a cookie or session variable.

Upvotes: 3

Qwerky
Qwerky

Reputation: 18445

When creating the email you need to encrypt the user ID, then base64 encode it, then URL encode it. Put this as the userID param in the link.

When decrypting the email you do the same in reverse; get the userID param, URL decode it, base64 decode it then decrypt it.

Remember to use a different intitialisation vector every time you encode a user ID. You will need to put the initialisation vector in the emailed link as a URL parameter too in order to decrypt it.

Upvotes: 0

Karthik
Karthik

Reputation: 391

I think of a simple solution you try to generate a random number(make it as a key) and for the encryption use some simple technique of yourself like XOR 'ing the ASCII value of the characters in the user name with the key that you have generated .so the long random key results in a greater result.

Upvotes: 0

leebriggs
leebriggs

Reputation: 3257

You should continue to base64 encode the AES data, as at that point it is likely binary rather than a string that can be escaped. You should also check that you are using url safe base64 encoding.

Upvotes: 4

Related Questions