问题
My users can subscribe to threads that send them an email with a simple unsubscribe link. This link contains an encrypted subscribeid and a verifying userid via this process:
// generate iv and create encrypted data
$iv = openssl_random_pseudo_bytes(16);
$encrypted = openssl_encrypt($data, 'AES-128-CBC', ENCRYPTION_KEY,0,$iv);
// send the iv along with the encrypted text
$ciphertext = $iv . $encrypted;
// generate a hash which can verify the data has not changed
$hash = hash_hmac('sha1', $ciphertext, ENCRYPTION_KEY)
// encode the data for email link
encoded = urlencode(base_64_encode($hash.$ciphertext))
This generates a string like:
www.site.com?id=YzU4MzAzMjljZWUyYmNmY2JmNjE5MGE0YzVhNDUzZjI0YmJmZWI3YoyqdFj6dxA/OVJOw2UN7HErYVV5dmhUVEJzVHBsUGd3aDNHbjVYbmFMa0dhUFczSmpXWnFBN0FyVGxkVml3S041VlhsSXd6TitJYld5QmdhWEFkL3hYSDFiRWdzN0wvNjFXYURiYlNreXpLQ1ZqWnhHMmdCSlZGaUVxU3ZGY3I3RW9GZkJYN3l4Vkp3YmJicg
On the server end, I verify the data and hash and verify the subscribeid is valid for the userid contained in the data and then mark the subscribe record expired.
I developed this encryption for temporary logins (logins with an expiration date) but is a 250 character string overkill for a simple unsubscribe link? The main issues seem to be that it is unsightly in urls and plain text emails. It also has the risk of the link being broken in plain text email clients.
If this were to be hacked, the most that is at risk are the subscribe records being marked as expired. Should I worry about overkill (or anything else). Is there a simpler but still secure method? I am a noob with encryption. The basic question is how much is enough?
回答1:
Simpler method is a random string of a specific length (e.g. 30 chars) stored in a table with a unique
constraint on that field. That random value has no meaning apart from the db and cannot be decrypted because there is no information in it. It only means anything when you use it in a where clause to lookup a record in that table.
来源:https://stackoverflow.com/questions/29041912/secure-unsubscribe-link-how-much-encryption-is-enough