gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:37:52 27 4
gpt4 key购买 nike

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

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

最佳答案

openssl_public_encrypt 函数将您可以加密的数据大小限制为 key 的长度,如果您使用填充(推荐),您将丢失额外的 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 位 key ,最大密码输入长度为 117 个字符。
对于 2048 位 key ,它是 245 个字符。

我不能 100% 确定输出长度,但一个简单的线索应该可以证实这一点,输出是 key 长度的简单函数,所以对于 2048 位 key ,我怀疑它是 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

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

关于php - 如何在 MySQL 数据库中存储 openssl_public_encrypt() 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8517188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com