簡體   English   中英

存儲密碼/從數據庫讀取密碼

[英]store / read passwords into / from database

我正在嘗試編寫一個Web應用程序,該應用程序允許登錄的用戶將不同應用程序的密碼存儲到MySQL數據庫中。 但是,為了防止MySQL管理員直接從數據庫讀取這些密碼,我希望在將這些密碼發送到數據庫之前使其不可讀。 然后,當用戶希望查看其存儲的密碼時,Web應用程序將對存儲的密碼進行解密並將其顯示在屏幕上。

我正在嘗試找出一種方法來加密這些密碼以存儲在數據庫中,然后在從數據庫讀取時解密它們。

因此,例如:-用戶希望存儲一個新密碼:Abc123! -然后,Web應用程序將給定的密碼轉換為“亂碼”:234fohj234] [j8924](或類似名稱)並將其存儲到db中。 -當用戶打開Web應用程序查看其存儲的密碼時,他會看到正確的密碼:Abc123! -但是,當MySQL管理員使用PHPMyAdmin之類的程序查看/維護數據庫時,他只會看到“亂碼”密碼,而不是真正的密碼。

PHP(或MySQL)是否為此類功能提供內置功能? 還是有人對創建函數來實現此目標有任何提示?

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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM