在数据库中存储/读取密码

时间:2013-09-30 11:52:13

标签: php mysql database encryption passwords

我正在尝试编写一个Web应用程序,允许登录用户将不同应用程序的密码存储到MySQL数据库中。但是为了防止MySQL管理员直接从数据库中读取这些密码,我想让这些密码在发送到数据库之前不可读。 然后,当用户希望查看他存储的密码时,Web应用程序会解密存储的密码并在屏幕上显示它们。

我正在尝试设计一种加密这些密码以便在db中存储的方法,然后在从db中读取时解密它们。

所以,例如: - 用户希望存储新密码:Abc123! - Web应用程序然后将给定的密码转换为'乱码':234fohj234] j8924](或类似的东西)并将其存储到数据库中。 - 当用户打开Web应用程序查看存储的密码时,他会看到正确的密码:Abc123! - 但是当MySQL管理员使用像PHPMyAdmin这样的程序来查看/维护数据库时,他只会看到“乱码”密码,而不是真正的密码。

PHP(或MySQL)是否为这样的东西提供了内置函数? 或者是否有人有任何关于创建功能的提示来实现这一目标?

1 个答案:

答案 0 :(得分:0)

PHP提供MCrypt库以使用双向加密。最大的问题是存储密钥的位置,但这是另一个问题。您可以提供的最佳保护是,当您根本不存储密钥(用于加密)时,让用户每次使用您的服务时都输入密钥。缺点是,忘记密码功能是不可能的。

注意不要使用ECB模式进行加密,相同的密码将始终产生相同的密文,而是使用具有随机IV向量的其他模式。由于PHP手册中有使用ECB模式的示例,我添加了一个小例子,它使用IV向量并将其存储在结果中。

/**
 * Encrypts data with the TWOFISH algorithm. The IV vector will be
 * included in the resulting binary string.
 * @param string $data Data to encrypt. Trailing \0 characters will get lost.
 * @param string $key This key will be used to encrypt the data. The key
 *   will be hashed to a binary representation before it is used.
 * @return string Returns the encrypted data in form of a binary string.
 */
function encryptTwofish($data, $key)
{
  if (!defined('MCRYPT_DEV_URANDOM')) throw new Exception('The MCRYPT_DEV_URANDOM source is required (PHP 5.3).');
  if (!defined('MCRYPT_TWOFISH')) throw new Exception('The MCRYPT_TWOFISH algorithm is required (PHP 5.3).');

  // The cbc mode is preferable over the ecb mode
  $td = mcrypt_module_open(MCRYPT_TWOFISH, '', MCRYPT_MODE_CBC, '');

  // Twofish accepts a key of 32 bytes. Because usually longer strings
  // with only readable characters are passed, we build a binary string.
  $binaryKey = hash('sha256', $key, true);

  // Create initialization vector of 16 bytes
  $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_URANDOM);

  mcrypt_generic_init($td, $binaryKey, $iv);
  $encryptedData = mcrypt_generic($td, $data);
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);

  // Combine iv and encrypted text
  return $iv . $encryptedData;
}

/**
 * Decrypts data, formerly encrypted with @see encryptTwofish.
 * @param string $encryptedData Binary string with encrypted data.
 * @param string $key This key will be used to decrypt the data.
 * @return string Returns the original decrypted data.
 */
function decryptTwofish($encryptedData, $key)
{
  if (!defined('MCRYPT_TWOFISH')) throw new Exception('The MCRYPT_TWOFISH algorithm is required (PHP 5.3).');

  $td = mcrypt_module_open(MCRYPT_TWOFISH, '', MCRYPT_MODE_CBC, '');

  // Extract initialization vector from encrypted data
  $ivSize = mcrypt_enc_get_iv_size($td);
  $iv = substr($encryptedData, 0, $ivSize);
  $encryptedData = substr($encryptedData, $ivSize);

  $binaryKey = hash('sha256', $key, true);

  mcrypt_generic_init($td, $binaryKey, $iv);
  $decryptedData = mdecrypt_generic($td, $encryptedData);
  mcrypt_generic_deinit($td);
  mcrypt_module_close($td);

  // Original data was padded with 0-characters to block-size
  return rtrim($decryptedData, "\0");
}
相关问题