schwim wrote:First, when they register, email address gets strtolowered before being encrypted. In another cell in the row, I would store the number of characters in the address as well as first character, last char before @ and tld, so thisisme@here.com would get an entry of 17tecom. When someone attempted to log in, it would do the same to their email address, check the row for a match of the count and character string. If it found one, then it would do the authentication check.
Even to me, this sounds convoluted, but I think I can make it work pretty well for a login check.
Ah, my bad, I forgot about the IV: you would not be able to encrypt the email address supplied at login because you would not know which IV to use. However, your idea of storing those information in plaintext is bad: you would be revealing information that should remain secret, possibly allowing a known plaintext attack.
Perhaps you could store a cryptographic hash of the plaintext email in the database, similiar to how passwords are stored, except that you use a site-wide salt (some people fancifully call it a "pepper") only that is stored in the same way as your secret key. This way, you can hash the email address supplied at login, then retrieve all the encrypted email addresses with hashes that correspond to the computed hash, and decrypt each one for comparison with the email address supplied. Typically, there will only be zero or one such result, but hash collisions are possible so you need to account for that (and so you would index that column, but not with a unique index).
In theory, not using a salt specific to each email address reduces the security, but as the attacker would need the pepper to begin with (i.e., although we normally assume the attacker knows the algorithm, here we reasonably assume the attacker does not know the full algorithm since the pepper is effectively a critical part of the algorithm), this is not a concern since an attacker who has the pepper probably has the secret key, in which case the hash becomes unnecessary as the attacker can just decrypt the encrypted email addresses.