php – 如何在MySQL数据库中存储openssl_public_encrypt()输出?

我需要在
PHP中存储MySQL中加密但可恢复的(通过管理员)密码. AFAIK,最直接的方法是使用openssl_public_encrypt(),但我不确定需要什么列类型.我可以根据密钥的大小和输入对加密输出的最大长度做出任何可靠的判断吗?

或者我被迫使用一个巨大的领域(例如BLOB),并希望它一直有效?

最佳答案 openssl_public_encrypt函数将可以加密的数据大小限制为密钥的长度,如果使用填充(推荐),则会丢失额外的11个字节.

However, the PKCS#1 standard, which OpenSSL uses, specifies a padding scheme (so you can encrypt smaller quantities without losing security), and that padding scheme takes a minimum of 11 bytes (it will be longer if the value you’re encrypting is smaller). So the highest number of bits you can encrypt with a 1024-bit key is 936 bits because of this (unless you disable the padding by adding the OPENSSL_NO_PADDING flag, in which case you can go up to 1023-1024 bits). With a 2048-bit key it’s 1960 bits instead.

当然,你永远不应该禁用填充,因为这将使相同的密码加密到相同的值.

因此对于1024位密钥,最大密码输入长度为117个字符.
对于2048位密钥,它是245个字符.

我不是100%确定输出长度,但是一个简单的跟踪应该确认这一点,输出是一个简单的keylength函数,所以对于一个2048位的密钥,我怀疑它是256字节.

您应该使用具有所需长度的二进制字符串来存储密码.
出于速度原因,最好在场地上使用有限长度的索引.
不要使用blob(!),因为这会减慢速度,没有任何好处.

CREATE TABLE user
  id unsigned integer auto_increment primary key,
  username varchar(50) not null,
  passRSA binary(256),      <<-- doublecheck the length.
  index ipass(passRSA(10))  <<-- only indexes the first 10 bytes for speed reasons. 
) ENGINE = InnoDB  

向索引添加额外的字节只会减慢速度并增加索引文件,但没有任何好处.

点赞