views:

59

answers:

3

I have an array of IDs (e.g. array(1, 2, 10, 34, 100, 101)) that represents files ids of pictures in database.

I would like to send a link to customer by email such that when he press this link he will see the pictures, but I don't want customer to see these files ids!

Thus, I thought to encrypt the array of pictures ids with some key that customer doesn't know, and when customer clicks the link my PHP script will get the encrypted string, decrypt it back to the pictures ids, and send customer a page with the pictures.

Are there any PHP built in functions that can do the encryption/decryption with custom key for me ?

Thanks !!

+1  A: 

PHP has the mcrypt extension. It can do this.

Note that in the method you describe, the customer will see the picture IDs when you send him the page.

Borealid
You are right. I didn't thought about that. Do you have any idea how not to expose pictures ids ?
Misha Moroshko
@Misha Moroshko: You could have a second PHP script which returns the appropriate picture data, given the encrypted form. Give the user a page with embedded images pointing to the second script. Then the IDs are never seen.
Borealid
+1  A: 

If it were me implementing this I'd just give the customer a random hash, then relate that hash to particular IDs in a database table a bit like:

link_hash VARCHAR(40) NOT NULL,
link_id INT(11) NOT NULL

(one or more rows per link_hash to allow multiple IDs)

As Borealid mentioned in his answer, you'll also need to ensure that when they view the page they won't see the IDs in the picture URLs either. This will be a separate challenge.

thomasrutter
OP's method has the advantage of requiring constant storage space; yours requires O(N) storage for N=number of pictures.
Borealid
"As Borealid mentioned in his answer, you'll also need to ensure that when they view the page they won't see the IDs in the picture URLs either. This will be a separate challenge." -> well that can be solved by <img src="picture-serve.php?uid={$linkhash}"
niggles
True, but then that 'uid' hash then becomes as good as a picture ID, so the OP may then want to prevent people seeing/share than ID as well.
thomasrutter
A possible solution to which would be to expire those per-picture hashes after a certain amount of time.
thomasrutter
+2  A: 

How about giving a name to your pictures in the db that is not the same as the id?

When a user uploads a file you give the file a random name like the current timestamp and md5. The you store the name in your database and give it a id. That way you get your sequential ids and you get filenames that are very hard to guess. That and prevent visitors from displaying the content of the images folder and you are pretty set.

Iznogood
This is the right idea - but don't bother with timestamps or hashes, just generate a random 128 bit number and use the hex representation of that. The chances of guessing a correct number are infintesimal - and equivalent to guessing a correct encryption key.
caf