当您需要实际密码文本时如何存储密码

How to store a password when you need the actual password text

我的数据库以 MD5 格式存储用户的登录密码,因此当他们登录时,它会将他们输入的密码转换为 MD5 与数据库中的 MD5 密码进行比较。

我正在实现应用程序可以使用 IMAP 连接到他们的电子邮件帐户的功能。所以我需要将用户的电子邮件帐户密码存储在数据库中,但据我所知,我不能使用 MD5,因为我实际上需要将他们的密码作为纯文本结束,以用于通过 IMAP 连接进行连接。

有什么解决方案可以将他们的密码安全地存储在数据库中,但又能够检索它并在脚本中将其转换为纯文本?

首先,MD5 不是一种安全的密码存储方式。看看 password_hash function and bcrypt for a more modern and secure solution. See also: How do you use bcrypt for hashing passwords in PHP?

至于 IMAP 密码,您 不需要 哈希,因为它们是 "one-way"。你需要一个加密算法。

更好的选择之一是使用 openssl_encrypt() 和存储在服务器上的安全密钥。这是 Laravel 举例的方法。

以这个为例:https://github.com/laravel/framework/blob/5.2/src/Illuminate/Encryption/Encrypter.php#L62

/**
 * Encrypt the given value.
 *
 * @param  string  $value
 * @return string
 *
 * @throws \Illuminate\Contracts\Encryption\EncryptException
 */
public function encrypt($value)
{
    $iv = random_bytes($this->getIvSize());
    $value = \openssl_encrypt(serialize($value), $this->cipher, $this->key, 0, $iv);
    if ($value === false) {
        throw new EncryptException('Could not encrypt the data.');
    }
    // Once we have the encrypted value we will go ahead base64_encode the input
    // vector and create the MAC for the encrypted value so we can verify its
    // authenticity. Then, we'll JSON encode the data in a "payload" array.
    $mac = $this->hash($iv = base64_encode($iv), $value);
    $json = json_encode(compact('iv', 'value', 'mac'));
    if (! is_string($json)) {
        throw new EncryptException('Could not encrypt the data.');
    }
    return base64_encode($json);
}
/**
 * Decrypt the given value.
 *
 * @param  string  $payload
 * @return string
 *
 * @throws \Illuminate\Contracts\Encryption\DecryptException
 */
public function decrypt($payload)
{
    $payload = $this->getJsonPayload($payload);
    $iv = base64_decode($payload['iv']);
    $decrypted = \openssl_decrypt($payload['value'], $this->cipher, $this->key, 0, $iv);
    if ($decrypted === false) {
        throw new DecryptException('Could not decrypt the data.');
    }
    return unserialize($decrypted);
}
/**
 * Get the IV size for the cipher.
 *
 * @return int
 */
protected function getIvSize()
{
    return 16;
}

显然,Laravel 代码依赖于 Laravel 的其他部分,但作为示例,这就足够了。获得加密值后,您可以将其存储在数据库中。

请记住,安全性与最弱的安全性一样强link——因此请确保您的数据库凭据是安全的,并且您的数据库用户拥有应用程序正常运行所需的最低权限。